- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
How to set Python environment variable PYTHONPATH on Linux?
To set the PYTHONPATH environment variable on Linux, follow these steps −
Open a terminal window on your Linux system.
Determine the path to your Python module or package. For example, suppose you have a Python module named mymodule located in the /home/user/myproject folder.
Set the PYTHONPATH environment variable to the path of your module or package using the following command −
This command sets the PYTHONPATH environment variable to /home/user/myproject and also includes the previous value of PYTHONPATH in case it was already set.
Note that the path should be separated by a colon (:) on Linux.
Verify that the PYTHONPATH environment variable has been set correctly using the following command −
This should display the path you set earlier, along with any previous paths that were included in PYTHONPATH.
Let us consider few more examples of setting the PYTHONPATH environment variable on Linux −
Set PYTHONPATH to a single path −
Set PYTHONPATH to multiple paths −
Set PYTHONPATH to include the current directory −
Set PYTHONPATH to include the current directory and a subdirectory −
This sets the PYTHONPATH environment variable to include the current directory (.) and a subdirectory named subdir located in the current directory.
Note that the PYTHONPATH environment variable only affects the current shell session. If you want to set it permanently, you will need to add the export command to a startup script such as .bashrc or .bash_profile.
Open a terminal window
Determine the location of the folder containing the Python module or package that you want to add to the PYTHONPATH environment variable. For example, let's say you have a folder called my_module located in your home directory (~/my_module).
Export the PYTHONPATH environment variable to include the folder containing the module or package, using the export command. For example, to add the ~/my_module folder to the PYTHONPATH environment variable, you can run the following command −
The $PYTHONPATH variable is used to append the new folder to the existing value of PYTHONPATH, so that any previously set paths are not overwritten. The colon (:) is used to separate the new path from the existing paths.
It must be noted that this command will only set the PYTHONPATH environment variable for the current terminal session. To make this setting permanent, you will need to add it to your shell's configuration file (e.g., ~/.bashrc for Bash).
Verify that the PYTHONPATH environment variable has been set correctly. You can do this by running the following command −
This should display the current value of the PYTHONPATH environment variable, including the folder you just added.
Adding multiple folders to PYTHONPATH
This will add both the ~/my_module and ~/my_other_module folders to the PYTHONPATH environment variable.
Adding a folder with a space in its path name −
Note the use of double quotes to enclose the path name containing spaces.
Adding a folder relative to the current directory −
This will add the my_module folder located in the current directory to the PYTHONPATH environment variable.
Setting PYTHONPATH to a specific folder only −
This will set the PYTHONPATH environment variable to only contain the my_module folder located at /path/to/.
By setting the PYTHONPATH environment variable, you can ensure that Python can find and import the modules and packages you need for your projects, even if they are located outside of the default search paths.
Adding a package that requires a specific version of Python −
If you have multiple versions of Python installed on your system and you want to use a package that requires a specific version, you can add the package's location to the PYTHONPATH environment variable. This will allow you to use the package with the specific version of Python required.
For example, if you have Python 3.6 installed and a package that requires Python 3.7, you can add the package's location to the PYTHONPATH environment variable for Python 3.6. Then, when you run Python 3.6, it will be able to find and import the package.
It must be noted that this approach is not a substitute for installing packages with the correct version of Python using a package manager like pip. Instead, it is a workaround for situations where you need to use a package that is not available for the version of Python you have installed.
These examples demonstrate the flexibility of the PYTHONPATH environment variable and how it can be used to customize the search path for Python modules and packages. By setting PYTHONPATH correctly, you can avoid errors related to module imports and ensure that your Python scripts and applications can access the modules and packages they need.
- Related Articles
- How to set Python environment variable PYTHONPATH on Linux?
- How to set python environment variable PYTHONPATH on Windows?
- How to set python environment variable PYTHONPATH on Mac?
- What is PYTHONPATH environment variable in Python?
- How to set the GOPATH environment variable on Ubuntu?
- How to substitute an environment variable using sed on Linux?
- How to configuring Java Environment on Linux?
- How to set up your python development environment on AWS?
- How to set your python path on Linux?
- Delete expoted environment Variable in Linux shell
- How to set the environmental variable LD_LIBRARY_PATH in Linux?
- How do we set the Java environment variable in cmd.exe?
- Access environment variable values in Python
- Setting MySQL Environment Variables on Linux
- How to get environment variable value using PowerShell?
- How to set JAVA_HOME environment variables on Windows OS in Java?\n