PYTHONPATH
directory
environment variables
Python programming
setup configuration

Permanently add a directory to PYTHONPATH?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

You can permanently add a directory to Python's import path by setting PYTHONPATH, creating a .pth file in site-packages, or installing your code properly as a package. The best choice depends on whether the path should apply to one shell user, one virtual environment, or an actual distributable project.

Understand What PYTHONPATH Does

Python builds sys.path from several sources, including:

  • the script directory
  • standard library locations
  • installed site-packages
  • the PYTHONPATH environment variable

If you set PYTHONPATH, Python adds those directories to the module search path at startup.

A quick check:

python
1import sys
2
3for entry in sys.path:
4    print(entry)

This is useful when debugging whether your change actually took effect.

Permanently Set PYTHONPATH In Your Shell

On Linux or macOS with bash, add this to ~/.bashrc or ~/.bash_profile:

bash
export PYTHONPATH="/Users/markqian/my_libs:$PYTHONPATH"

For zsh, put it in ~/.zshrc:

bash
export PYTHONPATH="/Users/markqian/my_libs:$PYTHONPATH"

Then reload the shell:

bash
source ~/.zshrc

This makes the directory available in future shell sessions for that user.

On Windows, Set An Environment Variable

In PowerShell for the current user:

powershell
1[Environment]::SetEnvironmentVariable(
2    "PYTHONPATH",
3    "C:\Users\markqian\my_libs",
4    "User"
5)

Open a new terminal after changing it so the new environment variable is picked up.

If you need multiple directories, separate them with the platform's path separator. On Windows that is usually a semicolon.

A Better Option For Many Cases: Use A .pth File

If you want the path added for a particular Python installation or virtual environment, a .pth file inside site-packages is often cleaner than a global shell variable.

Find the site-packages location:

python
import site
print(site.getsitepackages())

Then create a file such as custom_paths.pth containing one directory per line:

text
/Users/markqian/my_libs

Python reads .pth files during startup and appends those directories automatically.

This is especially useful inside virtual environments because the change stays scoped to that environment.

The Best Long-Term Answer: Install The Package

If the directory contains code you actually maintain as a project, the cleanest solution is usually not PYTHONPATH at all. Install it properly.

Editable install example:

bash
pip install -e /Users/markqian/projects/my_package

This gives Python a normal package import path and avoids mysterious environment-variable coupling.

For real projects, package installation is more portable and less fragile than telling every machine to inject a source directory globally.

Choose The Right Scope

A practical rule:

  • use PYTHONPATH for simple user-level environment tweaks
  • use .pth files for interpreter- or venv-specific path additions
  • use pip install -e for real projects under development

That keeps the import path aligned with the actual ownership and lifecycle of the code.

Avoid Overusing Global PYTHONPATH

A permanent global PYTHONPATH can create confusing behavior:

  • imports work on one machine but not another
  • unrelated projects accidentally see the same extra modules
  • debugging import precedence gets harder

That is why many teams prefer virtual environments and package installation over a permanent machine-wide PYTHONPATH.

It is not that PYTHONPATH is wrong. It is just easy to outgrow.

Common Pitfalls

The biggest mistake is setting PYTHONPATH and expecting an already open terminal or IDE to notice immediately. Many tools need to be restarted.

Another mistake is using the wrong separator for multiple paths. Unix-like shells use colons, while Windows commonly uses semicolons.

People also forget that a global path tweak can leak into unrelated projects. What feels convenient at first can become an import-debugging headache later.

Finally, if the directory is actually a project you own, do not ignore packaging. pip install -e is often the more maintainable answer.

Summary

  • You can permanently add a directory through PYTHONPATH, a .pth file, or proper package installation.
  • Shell configuration works for user-level path changes.
  • '.pth files are useful for one Python environment or virtual environment.'
  • Editable installs are usually the best long-term solution for real projects.
  • Choose the path mechanism that matches the scope and maintenance needs of the code.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.