SoFunction
Updated on 2025-04-09

Steps to set PYTHONPATH environment variables in Linux/Windows

What is PYTHONPATH?

PYTHONPATHis 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 correctlyPYTHONPATHEnvironment 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 systemPYTHONPATHA concise guide to  .

How to set PYTHONPATH?

The following is set up in Linux systemPYTHONPATHSteps:

  • Open the terminal.
  • Use a text editor to open your shell configuration file. Depending on your shell and system, this might be.bashrc.bash_profileor.profiledocument. For example, if you are using bash, you can edit.bashrcdocument:
vi ~/.bashrc
  • In the open configuration file, add the following line to set or updatePYTHONPATH
export PYTHONPATH="${PYTHONPATH}:/path/to/your/module1:/path/to/your/module2"
  • Here/path/to/your/module1and/path/to/your/module2Should be replaced with what you want to add toPYTHONPATHThe 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_profileor.profile, then use:
source ~/.bash_profile
  • or
source ~/.profile

Example

Suppose you have two module directories/home/user/my_project/libsand/home/user/other_project/libs, you want to add them toPYTHONPATHIn, 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 onePYTHONPATHVariable, without overwriting the original settings.

Tips

  • use${PYTHONPATH}Make sure you don't lose any existing onesPYTHONPATHset up.
  • If you are using another shell (such as zsh or fish), the configuration file may be.zshrcor
  • ChangePYTHONPATHAfter that, the newly launched terminal session or script will use the new settings.
    Through the above steps, you can easily managePYTHONPATHso 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

  1. 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.
  2. Click "Advanced System Settings"
    • In the System Properties window, find and click the Advanced tab.
    • In the Advanced tab, click the Environment Variables button.
  3. 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 calledPYTHONPATHvariable (if not, click "New" to create one).
    • If foundPYTHONPATH, double-click it to edit; if you need to create a new one, click "New", and then enterPYTHONPATHAs a variable name.
  4. 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
  5. Click "OK" to save the changes
    • You need to click the OK button of each open dialog box to save the changes.
  6. Restart Explorer or restart your computer
    • In order for the environment variable to take effect, you may need to restart File Explorer (pressCtrl + Shift + EscOpen Task Manager, right-click "File Explorer" and select "Restart") or restart the computer.

Example

Suppose you have two module directoriesC:\Users\YourUsername\myproject\libsandC:\Users\YourUsername\otherproject\libs, you want to add them to PYTHONPATH, you can follow these steps:

  1. Open the Environment Variables window.
  2. Find or createPYTHONPATHVariable.
  3. Set the variable value toC:\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!