- 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
What is PYTHONPATH environment variable in Python?
In Python, PYTHONPATH is an environment variable that specifies a list of directories to search for Python modules when importing them. When you import a module in Python, Python looks for the module in the directories specified in sys.path, which is a list of directories that includes the current working directory and directories specified in PYTHONPATH.
PYTHONPATH is an environment variable which you can set to add additional directories where python will look for modules and packages. For most installations, you should not set these variables since they are not needed for Python to run. Python knows where to find its standard library.
The only reason to set PYTHONPATH is to maintain directories of custom Python libraries that you do not want to install in the global default location (i.e., the site-packages directory).
Examples of how to use PYTHONPATH −
Set PYTHONPATH in the terminal
You can set PYTHONPATH in the terminal before running a Python script. Here's an example −
$ export PYTHONPATH=/path/to/my/modules/ $ python my_script.py
Set PYTHONPATH in the script
You can set PYTHONPATH in your Python script using the os module. Here's an example −
import os import sys sys.path.insert(0, os.path.abspath("/path/to/my/modules/"))
Append to PYTHONPATH
You can append a directory to PYTHONPATH in your Python script using the os module. Here's an example −
import os import sys sys.path.append(os.path.abspath("/path/to/my/modules/"))
Use a .pth file
You can create a .pth file in a directory that contains the directories to be added to PYTHONPATH. Here's an example −
$ cat /path/to/my/modules/my_modules.pth $/path/to/my/modules/
Use a virtual environment
When you create a virtual environment with venv or virtualenv, it automatically sets PYTHONPATH to include the virtual environment's site-packages directory.Here's an example −
$ python -m venv my_virtualenv $ source my_virtualenv/bin/activate
Use a .env file
You can use a .env file to set environment variables, including PYTHONPATH, for your project. First, install the python-dotenv package using
pip install python-dotenv
Then, create a .env file in the root directory of your project and add the following line to set PYTHONPATH −
Finally, load the environment variables from the .env file in your Python script −
from dotenv import load_dotenv import os load_dotenv() sys.path.insert(0, os.getenv("PYTHONPATH"))
Use a package directory structure
You can create a package directory structure where each subdirectory represents a package, and the subdirectories are automatically added to PYTHONPATH. Here's an example:
And import my_submodule like this:
PYTHONPATH is an environment variable that specifies a list of directories to search for Python modules when importing them. This can be useful when you have custom Python libraries that you do not want to install in the global default location. There are several ways to set PYTHONPATH, including setting it in the terminal, setting it in your Python script using the os module, appending to it using the os module, using a .pth file, using a virtual environment, and using a .env file. By using PYTHONPATH effectively, you can organize your Python code more efficiently and make it easier to maintain.
In conclusion, these are some ways to use PYTHONPATH to specify directories to search for Python modules. The method you choose depends on your needs and preferences, but using a virtual environment or a package directory structure are commonly used methods.
- Related Articles
- What is PYTHONPATH environment variable in Python?
- 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 JAVA_HOME variable in Java Environment?
- What is an Environment Variable in Postman?
- Access environment variable values in Python
- Delete expoted environment Variable in Linux shell
- What is environment ?
- How to get environment variable value using PowerShell?
- What is maven in Java environment?
- How do we set the Java environment variable in cmd.exe?
- How to get the value of environment variable in Postman?
- What are environment variables available in Python CGI Programming?
- How to set the GOPATH environment variable on Ubuntu?
- What is the computing environment?