How to add path to python path
How to add path to python path
Edited ( July 21, 2021 ) Edit
Examples of how to add a new path to your PYTHONPATH to import your own python modules or packages:
Introduction
For example, I have on my local computer a reperestory called «github_projects» (located in the following path «/Users/John/github_projects») where I stored all my own python modules that I develop:
However, if I try to import a python module from another reperestory on my computer I will get the following error message:
We get this error message here because python doesn’t know where to find the python module project_01. A simple solution to check that is to look at sys.path:
returns for example in my case:
can also check your PYTHONPATH using os:
Another solution to check your PYTHONPATH is to enter directly
in your terminal (not in your python interpreter!).
Adding a new path to your PYTHONPATH
To add a new path to your PYTHONPATH it is going to depend on the your shell (I used hereafter bash shell ). To get your shell just enter
returns for example
To temporary add a new path in your PYTHONPATH:
then if you check
it should have now:
and you can now start python (in the same window that you entered «export PYTHONPATH=»/Users/John/github_projects») and try to import your module:
Another solution to make that more permanently just open the
file and add the following line:
It will then add automatically «/Users/John/github_projects» to your PYTHONPATH each time you open a new terminal window.
Reloading your own python module
Note: another interessting tool is to be able to reload your python (> 3.4) module:
So you don’t need to restart python each time you make some change in your python module for example «project_01». You just need to reload it:
References
Benjamin H.G. Marchant
How to add Python Path to Windows 10 PATH
Python is a powerful programming language that is scalable and uses code that is readable and clear for all types of projects. Python is also available across a number of operating systems, making it a popular choice for developers. If you are using Python on your Windows operating system, you may need to adjust your System Environment Variables to simplify utilizing Python on your server.В In this article, we will demonstrate how to add Python path to windows 10.
Method 1
The latest Python installer for Windows can set the System Environment Variable Path automatically if selected during the installation process.
Simply select the «Add Python 3.5 to PATH» checkbox.
Method 2
This method involves modifying the path of an existing Python installation. To verify if the path setting is correct or current, follow these steps.
Step 1.
Open an administrative command prompt by going to
Start > Windows System > Command Prompt.
Next, right-click on the command prompt icon, choose More, then choose “run as administrator«.
Step 2.
Once the Type in the python command, and then press Enter. If the System Variable Path is correctly set, you should receive output similar to what is shown below.
When using Powershell, the output will look like this.
If an error is seen indicating that the Python command is unknown, but we have confirmed that Python is installed and can be launched from within its directory, we will need to add the Python path to the System Environment Variables. Continue to Step 3 to accomplish this.
Step 3.
To begin, type in Win+R to open a run dialog box, Then type in sysdm.cpl and hit enter. This will open the System Properties dialog box.
Click on the Advanced tab at the top and then on the Environment Variables button at the bottom.
Step 4.
This will open both the User and System Variables dialog box.
Step 5.
Now, we will create a new User Variable. Click on the New button.
A new window will open. Now we will enter the following data in the open fields.
Step 6.
Next, find the Path entry in the System variable section, select it and then click the Edit button.
A new window will open. We will create a New entry that matches the Path variable we created in step 5 with the addition of «\Scripts» to the end of the entry like so.(C:\Users\*yourusername*\AppData\Local\Programs\Python\Python38\Scripts\
Step 7.
Click the OK button on all three open windows to save the entries.
We can now verify that we have completed this task successfully by opening a new administrative command prompt windows and typing in “python” (without the quotes).
We will receive a response similar to what is shown above. By following these quick steps, you’ll be able to access Python from any command prompt.
Python is one of the most popular programming languages existing today.
Our talented Support Team is full of experienced System Administrators and technicians who have intimate knowledge of multiple web hosting technologies, especially those discussed in this article.
Should you have thoughts or questions, we are always available to assist 24 hours a day, 7 days a week 365 days a year. We pride ourselves on being The Most Helpful Humans In Hostingв„ў!
If you are a Fully Managed VPS server, Cloud Dedicated, VMWare Private Cloud, Private Parent server or a Dedicated server owner and you are uncomfortable with performing any of the steps outlined, we can be reached via phone @800.580.4985, a chat or support ticket to assisting you with this process.
Related Articles:
About the Author: David Singer
I am a g33k, Linux blogger, developer, student, and former Tech Writer for Liquidweb.com. My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation.
Join our mailing list to receive news, tips, strategies, and inspiration you need to grow your business
Python import, sys.path, and PYTHONPATH Tutorial
Introduction
The import statement is usually the first thing you see at the top of any Python file. We use it all the time, yet it is still a bit mysterious to many people. This tutorial will walk through how import works and how to view and modify the directories used for importing.
If you want to learn how to import a module by using a string variable name to reference the module, check out my tutorial on Import Python Module by String Name
Also check out my Python Virtual Environments Tutorial to learn more about isolated Python environments.
Modules versus packages
First, let’s clarify the difference between modules and packages. They are very closely related, and often confused. They both serve the same purpose which is to organize code, but they each provide slightly different ways of doing that.
A module can be thought of as a self-contained package, and a package is like a module that is separated out across multiple files. It really depends on how you want to organize your code and how large your project is. I always start with a module and turn it in to a package if needed later.
How import works
The import keyword in Python is used to load other Python source code files in to the current interpreter session. This is how you re-use code and share it among multiple files or different projects.
Import versus from
Different ways to import and execute os.path.join() :
As you can see, you can import the whole package, a specific module within a package, a specific function from within a module. The * wildcard means load all modules and functions. I do not recommend using the wildcard because it is too ambiguous. It is better to explicitly list each import so you can identify where it came from. A good IDE like PyCharm will help you manage these easily.
When you call import in the Python interpreter searches through a set of directories for the name provided. The list of directories that it searches is stored in sys.path and can be modified during run-time. To modify the paths before starting Python, you can modify the PYTHONPATH environment variable. Both sys.path and PYTHONPATH are covered more below.
Import by string
This method is not commonly used, and is only useful in special circumstances. For example, if you are building a plugin system where you want to load every file in a directory as a module based on the filepath string.
How __init__ and __main__ work
We will look at the difference between a module and a package in a moment, but the main idea is this:
In a package
Here is an example structure of a package:
In a module
When a module is imported, it runs the whole file, loading any functions defined.
Take this example. Create a file named my_module.py with the following contents:
Try out a few different things to understand how it works:
Manage import paths
sys.path
PYTHONPATH
Here is an example of setting the environment variable in Windows and listing the paths in Python:
And in Linux and Mac you can do the equivalent like this:
The site module
You can also direclty invoke the site module to get a list of default paths:
PYTHONHOME
This is particularly relevant if you embedded Python in to a C application and it is trying to determine the path of Python using the PYTHONHOME environment variable.
Just for reference, here is a quick example of how you would build a C application with Python embedded in it.
How to add Python to PATH variable in Windows
PATH variable
The PATH variable is a list of directories where each directory contains the executable file for a command.
When a command is entered into the Windows command prompt, the prompt searches in the PATH variable for an executable file with the same name as the command; in the case that the required file is not found, it responds with an error message that states that the specified command was not recognized.
One way to overcome this error is to write the complete directory of the executable file instead of only entering the command name. However, this is not a very user-friendly approach.
Another possible, and easier, way to avoid this error is to add the executable file’s directory to the PATH variable. Oftentimes, this needs to be done when installing Python.
The complete path of python.exe can be added by:
Right-clicking This PC and going to Properties.
Clicking on the Advanced system settings in the menu on the left.
Clicking on the Environment Variables button on the bottom right.
In the System variables section, selecting the Path variable and clicking on Edit. The next screen will show all the directories that are currently a part of the PATH variable.
Clicking on New and entering Python’s install directory.
Usually you can find the python installed binary in this path location: C:\Users\Educative\AppData\Local\Programs\Python\Python39
Using PYTHONPATH¶
The PYTHONPATH variable has a value that is a string with a list of directories that Python should add to the sys.path directory list.
The main use of PYTHONPATH is when we are developing some code that we want to be able to import from Python, but that we have not yet made into an installable Python package (see: making a Python package ).
Returning to the example module and script in Where does Python look for modules? :
At the moment, on my machine, PYTHONPATH is empty:
Before we set PYTHONPATH correctly, a_script.py will fail with:
Now I set the PYTHONPATH environment variable value to be the path to the code directory:
Setting PYTHONPATH more permanently¶
You probably don’t want to have to set PYTHONPATH every time you start up a terminal and run a Python script.
Luckily, we can make the PYTHONPATH value be set for any terminal session, by setting the environment variable default.
For example, let’s say I wanted add the directory /Users/my_user/code to the PYTHONPATH:
If you are on a Mac¶
/.bash_profile in your text editor – e.g. atom
Add the following line to the end:
Start Terminal.app again, to read in the new settings, and type this:
If you are on Linux¶
Open your favorite terminal program;
/.bashrc in your text editor – e.g. atom
Add the following line to the end:
Close your terminal application;
Start your terminal application again, to read in the new settings, and type this:
If you are on Windows¶
Got to the Windows menu, right-click on “Computer” and select “Properties”:
From the computer properties dialog, select “Advanced system settings” on the left:
From the advanced system settings dialog, choose the “Environment variables” button:
In the Environment variables dialog, click the “New” button in the top half of the dialog, to make a new user variable:
Give the variable name as PYTHONPATH and the value is the path to the code directory. Choose OK and OK again to save this variable.
Now open a cmd Window (Windows key, then type cmd and press Return). Type:
to confirm the environment variable is correctly set.
If you want your IPython sessions to see this new PYTHONPATH variable, you’ll have to restart your terminal and restart IPython so that it picks up PYTHONPATH from the environment settings.
Источники информации:
- http://www.liquidweb.com/kb/how-do-i-set-system-variable-path-for-python-on-windows/
- http://www.devdungeon.com/content/python-import-syspath-and-pythonpath-tutorial
- http://www.educative.io/answers/how-to-add-python-to-path-variable-in-windows
- http://bic-berkeley.github.io/psych-214-fall-2016/using_pythonpath.html