Python Forum

Full Version: Virtual environment base folder is not add to sys.path on Linux
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I have a couple of questions about virtual environments. I use venv to create my environments. When I do that in Windows and I print sys.path using the environment's python executable, I get the base folder of the environment as one of the included paths. However, when I do the same in Linux, the base folder is not included in sys.path.

This means that, if I have the following project structure:
Output:
my_app |_ my_package |_ __init__.py |_ my_module.py |_ __init__.py |_ main.py
...and I use this code from my_app.main import my_func inside the my_module.py file, the code will execute properly under Windows, but I will get ModuleNotFoundError if I run it under Linux.

The way I solve this issue is by adding a *.pth file, that includes the path to the base folder, in the site-packages folder of the virtual environment.

Here are my questions:

1) Why does venv behave differently, in this case, between Windows and Linux?
2) Is the solution I am using good or is there a better way of doing it that doesn't involve me having to append the path in every single *.py file?

Thank you very much.