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
  Windows, Linux and virtual environments Calab 2 545 Mar-07-2025, 10:43 PM
Last Post: snippsat
  I cannot create a virtual environment on visual studio code using python Willem_Aucamp316 2 3,971 Nov-27-2024, 02:20 PM
Last Post: menator01
  How to enable Matplotlib within virtual environment? tetris_theMovie 1 1,158 Oct-11-2024, 09:56 AM
Last Post: christie2
  Need help setting up trusted root ca in virtual environment dstempfley 0 1,287 May-31-2024, 03:14 PM
Last Post: dstempfley
  Help with creating folder and "virtual environment" AudunNilsen 1 1,052 Mar-21-2024, 04:41 AM
Last Post: deanhystad
  Create dual folder on different path/drive based on the date agmoraojr 2 1,398 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 1,530 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  Installing python packages in a virtual environment Led_Zeppelin 1 1,677 Aug-15-2023, 08:18 PM
Last Post: deanhystad
  Problem with virtual environment standenman 2 2,334 Feb-23-2023, 07:09 PM
Last Post: standenman
Question Virtual Environment (using VS Code) Ashcora 4 73,201 Feb-15-2023, 07:17 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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