The Python path, also known as PYTHONPATH
, is a list of directories where Python looks for modules to import. By default, this list includes the current working directory and several system-specific directories. However, you may need to add additional directories to this list if you want to use custom modules or packages.
In this tutorial, we will cover how to permanently modify the PYTHONPATH
environment variable on different operating systems, including Unix-based systems (such as Linux and macOS) and Windows.
Understanding the Python Path
Before modifying the PYTHONPATH
, it’s essential to understand how it works. The sys.path
list in Python contains all the directories where Python looks for modules to import. You can append new directories to this list using sys.path.append()
. However, these changes are only temporary and will be lost when you close your Python interpreter.
To make permanent changes, you need to modify the PYTHONPATH
environment variable on your system.
Modifying the PYTHONPATH on Unix-based Systems
On Unix-based systems, such as Linux and macOS, you can modify the PYTHONPATH
environment variable by adding a line to your shell configuration file. The exact steps may vary depending on your shell and operating system.
For example, if you’re using the bash shell, you can add the following line to your ~/.bashrc
or ~/.bash_profile
file:
export PYTHONPATH="${PYTHONPATH}:/my/other/path"
Replace /my/other/path
with the actual path you want to add. This will append the new directory to the existing PYTHONPATH
.
Alternatively, you can use a path configuration file, which is a more elegant solution. To find out where Python searches for this information, run the following command:
python -m site --user-site
This will print the directory where you need to create a .pth
file containing the paths you want to add.
For example:
SITEDIR=$(python -m site --user-site)
mkdir -p "$SITEDIR"
echo "/my/other/path" > "$SITEDIR/mypath.pth"
This will create a new .pth
file in the user site directory with the specified path.
Modifying the PYTHONPATH on Windows
On Windows, you can modify the PYTHONPATH
environment variable by adding a new file to the site-packages
directory of your Python installation. Here are the steps:
- Open the
Lib/site-packages
directory of your Python installation. - Create an empty file with a
.pth
extension (e.g.,mypath.pth
). - Add the required path to the file, one per line.
For example:
C:\my\other\path
D:\another\path
After adding the new paths, you should be able to import modules from these directories in your Python scripts.
Best Practices
When modifying the PYTHONPATH
, keep the following best practices in mind:
- Avoid hardcoding absolute paths. Instead, use relative paths or environment variables.
- Use a consistent naming convention for your
.pth
files. - Keep the number of directories in your
PYTHONPATH
to a minimum to avoid performance issues.
By following these steps and best practices, you can easily modify the PYTHONPATH
on different operating systems and make your custom modules and packages available for import in Python.