What is PYTHONPATH?
PYTHONPATH
is an environment variable that tells the Python interpreter to find the module to import in which directories. This is very useful for custom modules that are not included in the standard directory.
Setting PYTHONPATH environment variable in Linux system
In the Python development environment, set up correctlyPYTHONPATH
Environment variables are essential to ensure that the Python interpreter can find and import custom modules. Here is how to set up or update in Linux systemPYTHONPATH
A concise guide to .
How to set PYTHONPATH?
The following is set up in Linux systemPYTHONPATH
Steps:
- Open the terminal.
- Use a text editor to open your shell configuration file. Depending on your shell and system, this might be
.bashrc
、.bash_profile
or.profile
document. For example, if you are using bash, you can edit.bashrc
document:
vi ~/.bashrc
- In the open configuration file, add the following line to set or update
PYTHONPATH
:
export PYTHONPATH="${PYTHONPATH}:/path/to/your/module1:/path/to/your/module2"
- Here
/path/to/your/module1
and/path/to/your/module2
Should be replaced with what you want to add toPYTHONPATH
The actual path in . - Save and close the file.
- In order for the changes to take effect immediately, you need to reload the configuration file. It can be done with the following command:
source ~/.bashrc
- Or, if you edited it
.bash_profile
or.profile
, then use:
source ~/.bash_profile
- or
source ~/.profile
Example
Suppose you have two module directories/home/user/my_project/libs
and/home/user/other_project/libs
, you want to add them toPYTHONPATH
In, you can do this (the paths are separated by colons):
export PYTHONPATH="${PYTHONPATH}:/home/user/my_project/libs:/home/user/other_project/libs"
This line of code will append a new path to the existing onePYTHONPATH
Variable, without overwriting the original settings.
Tips
- use
${PYTHONPATH}
Make sure you don't lose any existing onesPYTHONPATH
set up. - If you are using another shell (such as zsh or fish), the configuration file may be
.zshrc
or。
- Change
PYTHONPATH
After that, the newly launched terminal session or script will use the new settings.
Through the above steps, you can easily managePYTHONPATH
so that the Python interpreter can correctly find your module.
The steps to set the PYTHONPATH environment variable in a Windows system are different from those in a Linux system. Here is a guide to setting up PYTHONPATH in Windows:
Setting PYTHONPATH in Windows system
operate
-
Open the System Properties dialog box:
- You can open system properties by right-clicking on the "This Computer" or "My Computer" icon and selecting "Properties".
- Alternatively, you can search for "System" in the Start menu and click on it.
-
Click "Advanced System Settings":
- In the System Properties window, find and click the Advanced tab.
- In the Advanced tab, click the Environment Variables button.
-
Edit system variables:
- In the Environment Variables window, you will see two parts below: System Variables and User Variables.
- In the "System Variables" section, find the name called
PYTHONPATH
variable (if not, click "New" to create one). - If found
PYTHONPATH
, double-click it to edit; if you need to create a new one, click "New", and then enterPYTHONPATH
As a variable name.
-
Set or update variable values:
- In the Variable Value field, enter your module path. If there are values before, make sure not to overwrite them, but use a semicolon (
;
) separates each path. - For example:
C:\path\to\your\module1;C:\path\to\your\module2
- In the Variable Value field, enter your module path. If there are values before, make sure not to overwrite them, but use a semicolon (
-
Click "OK" to save the changes:
- You need to click the OK button of each open dialog box to save the changes.
-
Restart Explorer or restart your computer:
- In order for the environment variable to take effect, you may need to restart File Explorer (press
Ctrl + Shift + Esc
Open Task Manager, right-click "File Explorer" and select "Restart") or restart the computer.
- In order for the environment variable to take effect, you may need to restart File Explorer (press
Example
Suppose you have two module directoriesC:\Users\YourUsername\myproject\libs
andC:\Users\YourUsername\otherproject\libs
, you want to add them to PYTHONPATH, you can follow these steps:
- Open the Environment Variables window.
- Find or create
PYTHONPATH
Variable. - Set the variable value to
C:\Users\YourUsername\myproject\libs;C:\Users\YourUsername\otherproject\libs
。
Through the above steps, you can set the PYTHONPATH environment variable in your Windows system.
This is the article about the operation steps of setting PYTHONPATH environment variables in Linux/Windows systems. For more related content on setting PYTHONPATH in Linux/Windows, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!