If you’re a coder or programmer, you probably spend a decent amount of time using the command prompt to execute programs or compile code. In order to complete those tasks, you most likely have to use a command from a library or software package installed (like Python) on your system.
By default, most of these programs will add their own custom shortcuts to the Windows environment variables. The most used environment variable in Windows is probably the PATH variable. It basically allows you to run any executables that are located inside the paths specified in the variable at the command prompt without having to give the full path to the executable.
How to set up Python path? When you installed anaconda2, did you add any PYTHONPATH directives to your startup file(s). If you are just looking for python interpreter and want it to include in your script then just find the python binary path by the command which python and use that path.
In this article, I’ll show you how you can add more paths to the Windows PATH variable in case you want to run executables from your own custom directories. It’s worth noting that the procedure below is for Windows 10, but it’s almost exactly the same for Windows 7 also.
Add Directories to PATH Variable
To get started, right-click on the Computer or This PC icon on the desktop and select Properties. If you don’t have that icon on your desktop already, you can add any missing desktop icons easily.
On the System dialog page, you’ll see an Advanced system settings link on the left-hand side.
This will bring up the System Properties dialog, which should already be open to the Advanced tab. Go ahead and click on the Environment Variables button at the very bottom.
On the Environment Variables dialog, you’ll see two sets of variables: one for user variables and the other for system variables. Both lists have the PATH variable, so you have to decide which one to edit.
If you only need the commands for your own user account, then edit the user variable. If you need it to work across the computer system regardless of which user is logged in, then edit the system variable. Click on Path and then click on Edit.
On the Edit environment variable dialog, you’ll see a list of all the paths that are currently in the PATH variable. As you can see, Node.js and Git already added their paths so that I can run Git commands and Node.js commands from anywhere while in the command prompt.
To add a new path, simply click on New and it’ll add a new line to the bottom of the list. If you know the path, simply type it in or copy and paste it. If you prefer, you can also click Browse and then navigate to the desired path.
To edit any path, simply select it and then click on the Edit button. You can also delete paths using the Delete button. Note that you can also move items up and down on the list. When you type a command at the command prompt, Windows has to search through each directory stored in the PATH variable to see if that executable exists or not. If you want your executable to be found faster, just move that path up to the top of the list.
This can also come in handy if you have multiple versions of the same command in different paths and need to have one run instead of the other. The one that shows up higher in the list will be run when you type in the command.
Lastly, if you click on Edit text, it will load a dialog where you can edit the Path variable using the old interface where all the paths are listed in one text box.
That’s all there is to it! If you want to learn more about environment variables, make sure to check out my post on how to create your own custom environment variables. Enjoy!
Hi i have a file structure like this
I want to import in foo2.py foo1.py. I tried but it doesn`t work on Ubuntu:
3 Answers
How python finds its modules
Strictly taken, a module is a single python file, while a package is a folder containing python files, accompanied by a (can be empty) file named __init__.py
, to tell python it is a package to import modules from. In both cases, modules need their .py
extension, but importing them is done without (see further below).
By default, Python looks for its modules and packages in $PYTHONPATH
.
To find out what is included in $PYTHONPATH, run the following code in python (3):
How to add a directory
Occasionally
From within a python file, you can add path(s) occasionally to the default path by adding the following lines in the head section of your python application or script:
For example:
if I have a folder: /home/myname/pythonfiles
, and I want to import the file module_1.py
, located in that directory, I add this to the head section of my code:
And I can simply import the file module_1.py
by:
When I create a package and want to import module(s) from the package, I need to create a folder in $PYTHONPATH
, containing the modules, accompanied by a (can be empty) file called __init__.py
For example:
To import from a package (folder) called my_package
in /home/myname/pythonfiles
, add the /home/myname/pythonfiles
path to your $PYTHONPATH
, like in example 1, and import the module called module_2.py
(inside the package folder) simply with: `
Adding directories to $PYTHONPATH
permanently:
Add the following line to your ~/.profile
file.
Subdirectories
From within a package, subdirectories are not included just like that; you need to 'chain' the directories. To import a module module_3.py
, inside folder subfolder
inside folder packagename
:
Given the fact that all subfolders in the package include their own __init__.py
file.
![Add python to path git bash Add python to path git bash](https://vgkits.org/blog/wp-content/uploads/2018/05/windows-add-python-path.jpg)
When a module is in the same directory as the script or application
There is no need to insert the path to a module when it is in the same directory as the script or application, it is automatically added.
Example:
If I have a folder, containing script.py
and module.py
, I can simply import the module by:
The correct way to use relative import is:
And you also need a __init__.py
in all your folders.
NB: This answer is outdated for the latest version of python 3, but is here for earlier version. For more information read the comments and https://www.python.org/dev/peps/pep-0420/
Essentially every folder with python code you want to add to the syspath needs an __init__.py file. These __init__.py files can be empty.
don.joeydon.joey![Anaconda Anaconda](https://docs.python.org/3/_images/win_installer.png)