pythonvirtualenv

How do I add a path to PYTHONPATH in virtualenv


I am trying to add a path to the PYTHONPATH environment variable, that would be only visible from a particular virtualenv environment.

I tried SET PYTHONPATH=... under a virtualenv command prompt, but that sets the variable for the whole environment.

How do I achieve that?


Solution

  • You can usually avoid having to do anything with PYTHONPATH by using .pth files. Just put a file with a .pth extension (any basename works) in your virtualenv's site-packages folder, e.g. lib/python3.13/site-packages or (on Windows) lib\site-packages. The file should contain just a path to the directory containing your package; it can be either an absolute path or one relative to the .pth file.