Of course! Understanding the Python path on Ubuntu is crucial for managing your development environment. Here’s a comprehensive guide covering the basics, common locations, and how to manage it.

What is the Python Path?
The Python Path (more accurately, the sys.path) is a list of directories that Python searches when you try to import a module or package. When you write import mymodule, Python looks for mymodule.py (or a package named mymodule) in every directory listed in sys.path.
The Default Python Paths on Ubuntu
Ubuntu comes with Python pre-installed. The default installation locations are standardized.
System Python (Managed by APT)
This is the Python version installed by the system's package manager (apt). It's highly recommended not to install packages into this Python environment, as it can break system tools that depend on it.
- Executables:
/usr/bin/python3/usr/bin/python(often a symlink topython3)
- Standard Library:
/usr/lib/python3.x/(wherexis your minor version, e.g.,python3.10)
- Third-Party Packages (installed via
apt):/usr/lib/python3.x/dist-packages/
- Site-packages (for user-installed packages - see below):
/usr/local/lib/python3.x/dist-packages/
User Python (Recommended for Development)
This is the Python version you should use for your own projects. It's installed into your user's home directory and doesn't interfere with the system Python.

- Executables (if installed via
make altinstall):~/bin/python3.x(e.g.,~/bin/python3.10)
- Standard Library:
~/lib/python3.x/
- Third-Party Packages (installed via
pip):~/lib/python3.x/site-packages/
How to View the Current Python Path
You can see the directories Python is currently searching in two ways:
Method 1: Using the Python Interpreter
This is the most direct way. Run the following command in your terminal:
python3 -c "import sys; print('\n'.join(sys.path))"
Example Output:
/usr/lib/python3.10
/usr/lib/python3.10/lib-dynload
/usr/local/lib/python3.10/dist-packages
/home/your_user/.local/lib/python3.10/site-packages
- The first two are the system standard library and dynamic modules.
- The next two are where packages installed with
sudo aptandpip(withoutsudo) will go, respectively.
Method 2: From Within a Python Script
Create a file named see_path.py:

import sys
print("Python looks for modules in these directories:")
for path in sys.path:
print(f" - {path}")
Run it with:
python3 see_path.py
How to Manage the Python Path
There are several ways to add a directory to the Python path, each with different use cases.
Method 1: PYTHONPATH Environment Variable (Temporary)
This is the most common way to add a path for a single session or script. It's temporary and doesn't affect the system.
-
Add a directory to
PYTHONPATHfor the current terminal session:export PYTHONPATH="/path/to/your/module:$PYTHONPATH"
- Important: Place
"$PYTHONPATH"at the end to append your new path to the existing one. If you put it first ($PYTHONPATH:/path/to/your/module), you risk hiding system modules.
- Important: Place
-
Now, run your Python script. It will be able to find modules in
/path/to/your/module. -
To make it permanent for your user, add the line to your shell's configuration file:
- For Bash:
~/.bashrcor~/.bash_profile - For Zsh:
~/.zshrc
Open the file with a text editor (e.g.,
nano ~/.bashrc) and add the line:export PYTHONPATH="/path/to/your/module:$PYTHONPATH"
Then, either restart your terminal or run
source ~/.bashrcto apply the changes. - For Bash:
Method 2: The .pth File (Semi-Permanent)
You can add a special file with a .pth extension to one of your site-packages directories. Python will automatically read this file on startup and add the listed directories to its path.
-
Find your user's
site-packagesdirectory:python3 -c "import site; print(site.getsitepackages())"
This will likely output something like
['/usr/local/lib/python3.10/dist-packages', '/home/your_user/.local/lib/python3.10/site-packages']. You should use the one in your home directory (/home/your_user/...). -
Create a
.pthfile in that directory:nano ~/.local/lib/python3.10/site-packages/my_custom_paths.pth
-
Add the path to your module inside the file (one path per line):
/path/to/your/module /another/path/to/a/module -
Save and close the file. Python will now include these directories in its path for any script run with
python3.
Method 3: Installing in "Editable" Mode (Best for Development)
If you are developing a Python package, this is the best practice. It links your project directory directly into Python's site-packages, so any changes you make are immediately reflected without needing to reinstall.
- Navigate to your project's root directory (which should have a
setup.pyorpyproject.tomlfile). - Install it in editable mode using
pip:pip3 install -e .
The
-eor--editableflag is key here.
Now, your project is "installed," and you can import its modules from anywhere, even while you're actively changing the code.
Common Problems and Solutions
Problem: ModuleNotFoundError: No module named 'requests'
This is the most common error. It means Python cannot find the requests library in any of its sys.path directories.
Solution:
-
Check if it's installed:
pip3 list | grep requests
If it's not listed, you need to install it.
-
Install it using
pip:- For your user (recommended):
pip3 install requests - For the system (use with caution):
sudo pip3 install requestsorsudo apt install python3-requests
- For your user (recommended):
-
Check the installation location:
pip3 show requests
Look at the
Location:line. It should be a directory in yoursys.path(e.g.,~/.local/lib/python3.10/site-packages).
Problem: Using the Wrong Python Version
You might have multiple Python versions (e.g., system python3.10 and a self-compiled python3.11). Your script might be trying to import a module installed for the wrong version.
Solution:
-
Always be explicit about which Python interpreter you are using.
- Use
python3.11instead of justpython3. - Use
pip3.11instead of justpip3.
- Use
-
Use Virtual Environments (The Best Practice)
Virtual environments are isolated Python environments that solve almost all path-related problems. They create a self-contained directory with its own Python executable and
site-packages.How to use them:
-
Install
venv:sudo apt install python3-venv
-
Create a virtual environment for your project:
cd /path/to/your/project python3 -m venv .venv
This creates a
.venvfolder containing a private Python installation. -
Activate the environment:
source .venv/bin/activate
Your terminal prompt will change to show
(.venv), indicating the environment is active. Now,pythonandpippoint to the versions inside.venv. -
Install packages: They will now be installed only in this environment.
pip install requests
-
Deactivate when you're done:
deactivate
-
This is the standard, recommended workflow for Python development on Ubuntu and any other OS.
