Mr Curved Limos
Mr Curved Limos

Reputation: 27

How do I force Python to find packages inside my venv first?

I'm having a problem making Python use installed packages in a venv virtual environment. I'm working in Windows with Python >= 3.9.

My problem is that the path to the venv site-packages directory gets appended to the end of the sys.path, so that the locally installed site-packages are the last place Python looks. When I start the venv and interrogate the system path, I see the following list in this order:

As a concrete example, with no PYTHONPATH defined:

C:\Users\<userid>\<project>> env\Scripts\activate

(env) C:\Users\<userid>\<project>> python
Python 3.9.0 (tags/v3.9.0:9cf6752, Oct  5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> for path in sys.path:
...     print(path)
...

C:\Users\<userid>\AppData\Local\Programs\Python\Python39\python39.zip
C:\Users\<userid>\AppData\Local\Programs\Python\Python39\DLLs
C:\Users\<userid>\AppData\Local\Programs\Python\Python39\lib
C:\Users\<userid>\AppData\Local\Programs\Python\Python39
C:\Users\<userid>\<project>\env
C:\Users\<userid>\<project>\env\lib\site-packages
>>>

The predictable result is that any package installed in the base Python installation (e.g., maybe some random version of numpy) gets imported before the packages in the venv (e.g. a specific version of numpy that I need in that package). This seems to violate the spirit of a virtual environment, and it doesn't work for what I'm doing. I need the packages in the venv to be found and imported first. It would be really nice if I could add arbitrary paths to the list, but so far I haven't found any way to change the defaults.

I've tried adding the line include-system-site-packages = false in my project's pyvenv.cfg, but it has no effect on the sys.path when I start the venv. I've tried creating a <project>.pth file in AppData\Local\Programs\Python\Python39\lib as recommended in How do you set your pythonpath in an already-created virtualenv?, but it also has no effect.

What am I missing? How can I force Python to find packages in my venv before looking at anything installed outside the venv? And how do I add my own paths?

Upvotes: -1

Views: 58

Answers (0)

Related Questions