On MacOS, Python's sitecustomize.py
is not called in the virtual environment's site-packages
#5937
Replies: 3 comments
-
@woodruffw @branchvincent Would one of you mind weighing in here? |
Beta Was this translation helpful? Give feedback.
-
Hi @flexatone, thanks for the details.
Could you say a bit more about the behavior you're seeing on Linux, and with which Python/how it was installed? If I had to guess what's happening, I suspect you're not seeing that code execute because Homebrew has its own For example, here's the In other words: Python is working as expected (and as documented) because it is importing and running In terms of working around this: you might be able to flip |
Beta Was this translation helpful? Give feedback.
-
Many thanks, @woodruffw , for your detailed response. I had not considered that only the first-encountered sitecustomize.py is loaded, and had incorrectly assumed that sitecustomize.py needed to explicitly reside in a site-packages directory. Thanks also for your suggestions of workarounds. I have found another: a .pth file can include a single line import statement that is executed on site initialization. By including a pth file that imports another module, I can implement sitecustomize.py-like behavior, specific to a virtual environment. |
Beta Was this translation helpful? Give feedback.
-
Output of
brew config
Output of
brew doctor
Description of issue
When creating a virtual environment with a
python3
sourced from Homebrew, Python'ssite
reports only one site-package directory and that the user site is not enabled:By Python documentation, a
sitecustomize.py
module placed in the site-packages directory should be loaded at the start of Python:https://docs.python.org/3/library/site.html#module-sitecustomize
However, when placing a
sitecustomize.py
in/Users/ariza/.env313-test/lib/python3.13/site-packages
, the code in that file is never executed. This is different than the behavior in Linux.I see from starting python with
python3 -v
that something is happening with/opt/homebrew/Cellar/python@3.13/3.13.1/Frameworks/Python.framework/Versions/3.13/lib/python3.13/sitecustomize.py
Does this mean that, unlike on Linux, virtual environments created with Homebrew Python will never call
sitecustomize.py
in the site'ssite-packages
? If so, this seems contrary to the expectations of whensitecustomize.py
is called in the Python documentation.If this is correct, is there anyway around this? Is there anyway to have a virtual environment based on a Homebrew Python call
sitecustomize.py
in the virtual environment'ssite-packages
?Beta Was this translation helpful? Give feedback.
All reactions