Python Forum
Virtual environment base folder is not add to sys.path on Linux
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Virtual environment base folder is not add to sys.path on Linux
#1
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with creating folder and "virtual environment" AudunNilsen 1 171 Mar-21-2024, 04:41 AM
Last Post: deanhystad
  Create dual folder on different path/drive based on the date agmoraojr 2 378 Jan-21-2024, 10:02 AM
Last Post: snippsat
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 466 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  Installing python packages in a virtual environment Led_Zeppelin 1 712 Aug-15-2023, 08:18 PM
Last Post: deanhystad
  Problem with virtual environment standenman 2 946 Feb-23-2023, 07:09 PM
Last Post: standenman
Question Virtual Environment (using VS Code) Ashcora 4 12,522 Feb-15-2023, 07:17 PM
Last Post: snippsat
  How do I link the virtual environment of that project to the 3.9.2 version of python? Bryant11 1 1,332 Feb-26-2022, 11:15 AM
Last Post: Larz60+
  Project structure with a virtual environment gdbengo 1 1,391 Jan-26-2022, 03:22 PM
Last Post: snippsat
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,391 Dec-18-2021, 09:32 PM
Last Post: Larz60+
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,150 Sep-09-2021, 01:25 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020