Closed s-vitaliy closed 2 years ago
Is it possible to fix this issue or create workaround to make virtualenv work correctly in our case?
Feel free to put in a PR to fix it. :+1:
@gaborbernat, can you help me to find ideas how to find the reason, why this happening?
Sorry, I do not have time for that at this point in time.
@gaborbernat, ok, thank you.
Hello, we are having the same issue. It only happens with python2.7 installed from python.org. Seems like virtualenv is not creating its own copy of python and pip instead using the global one installed under /Library/Frameworks/Python.framework/Versions/2.7/.
Overriding [global] target helps to install packages under virtualenv but then python won't find any packages even we include virtualenv folder in sys path.
@gaborbernat If you can guide me with some tips, I'd love to contribute with a PR to solve this issue.
Step one would be to run the test suite against that python and see what breaks 🤔 and then hunt the problems down one by one.
I tried however there are too many errors and going so deep, I think I'm not making any progress. Here is the log for who picks this up.
ERROR tests/unit/create/test_creator.py::test_create_no_seed[old_virtualenv-cpython2-mac-framework-symlinks-isolated] - RuntimeError: failed to query /U...
ERROR tests/unit/create/test_creator.py::test_create_no_seed[old_virtualenv-cpython2-mac-framework-copies-global] - RuntimeError: failed to query /Users...
ERROR tests/unit/create/test_creator.py::test_create_no_seed[old_virtualenv-cpython2-mac-framework-copies-isolated] - RuntimeError: failed to query /Use...
ERROR tests/unit/activation/test_powershell.py::test_powershell[no_prompt] - RuntimeError: failed to query /Users/yusufertekin/Sites/virtualenv/.tox/py2...
ERROR tests/integration/test_zipapp.py::test_zipapp_create[pip] - RuntimeError: could not find a python to build zipapp
ERROR tests/integration/test_zipapp.py::test_zipapp_create[app-data] - RuntimeError: could not find a python to build zipapp
ERROR tests/integration/test_zipapp.py::test_zipapp_help - RuntimeError: could not find a python to build zipapp
FAILED tests/unit/activation/test_csh.py::test_csh[no_prompt] - AssertionError: /Users/yusufertekin/Sites/virtualenv/.tox/py27/bin/python
FAILED tests/unit/activation/test_bash.py::test_bash[with_prompt] - AssertionError: /Users/yusufertekin/Sites/virtualenv/.tox/py27/bin/python
FAILED tests/unit/activation/test_bash.py::test_bash[no_prompt] - AssertionError: /Users/yusufertekin/Sites/virtualenv/.tox/py27/bin/python
FAILED tests/unit/discovery/py_info/test_py_info.py::test_py_info_cached_symlink - assert 4 == (1 + 1)
FAILED tests/unit/activation/test_csh.py::test_csh[with_prompt] - AssertionError: /Users/yusufertekin/Sites/virtualenv/.tox/py27/bin/python
FAILED tests/unit/discovery/py_info/test_py_info_exe_based_of.py::test_discover_ok[python-2.7-64-bin] - RuntimeError: failed to detect cpython2.7.18-64|...
FAILED tests/unit/discovery/py_info/test_py_info_exe_based_of.py::test_discover_ok[python-2-bin] - RuntimeError: failed to detect cpython2.7.18-64|cpyth...
FAILED tests/unit/discovery/py_info/test_py_info_exe_based_of.py::test_discover_ok[python-2.7-bin] - RuntimeError: failed to detect cpython2.7.18-64|cpy...
FAILED tests/unit/discovery/py_info/test_py_info_exe_based_of.py::test_discover_ok[CPython-2.7.18-bin] - RuntimeError: failed to detect cpython2.7.18-64...
FAILED tests/unit/discovery/py_info/test_py_info_exe_based_of.py::test_discover_ok[CPython-2.7.18-64-bin] - RuntimeError: failed to detect cpython2.7.18...
FAILED tests/unit/discovery/py_info/test_py_info_exe_based_of.py::test_discover_ok[CPython-2.7-bin] - RuntimeError: failed to detect cpython2.7.18-64|cp...
FAILED tests/unit/discovery/py_info/test_py_info_exe_based_of.py::test_discover_ok[CPython-2-bin] - RuntimeError: failed to detect cpython2.7.18-64|cpyt...
FAILED tests/unit/discovery/py_info/test_py_info_exe_based_of.py::test_discover_ok[CPython-2-64-bin] - RuntimeError: failed to detect cpython2.7.18-64|c...
FAILED tests/unit/discovery/py_info/test_py_info_exe_based_of.py::test_discover_ok[python-2.7.18-64-bin] - RuntimeError: failed to detect cpython2.7.18-...
FAILED tests/unit/discovery/py_info/test_py_info_exe_based_of.py::test_discover_ok[python-2.7.18-bin] - RuntimeError: failed to detect cpython2.7.18-64|...
FAILED tests/unit/discovery/py_info/test_py_info_exe_based_of.py::test_discover_ok[python-2-64-bin] - RuntimeError: failed to detect cpython2.7.18-64|cp...
FAILED tests/unit/discovery/py_info/test_py_info_exe_based_of.py::test_discover_ok[CPython-2.7-64-bin] - RuntimeError: failed to detect cpython2.7.18-64...
FAILED tests/unit/create/test_creator.py::test_pth_in_site_vs_PYTHONPATH - RuntimeError: failed to query /Users/yusufertekin/Sites/virtualenv/.tox/py27/...
FAILED tests/unit/create/test_creator.py::test_get_site_packages - RuntimeError: failed to query /Users/yusufertekin/Sites/virtualenv/.tox/py27/bin/pyth...
FAILED tests/unit/create/test_creator.py::test_create_vcs_ignore_override - RuntimeError: failed to query /Users/yusufertekin/Sites/virtualenv/.tox/py27...
FAILED tests/unit/create/test_creator.py::test_os_path_sep_not_allowed - assert 'SystemExit: ' in 'RuntimeError: failed to query /Users/yusufertekin/Sit...
FAILED tests/unit/create/test_creator.py::test_zip_importer_can_import_setuptools - RuntimeError: failed to query /Users/yusufertekin/Sites/virtualenv/....
FAILED tests/unit/create/test_creator.py::test_getsitepackages_system_site - RuntimeError: failed to query /Users/yusufertekin/Sites/virtualenv/.tox/py2...
FAILED tests/unit/create/test_creator.py::test_create_distutils_cfg[cpython2-mac-framework] - RuntimeError: failed to query /Users/yusufertekin/Sites/vi...
FAILED tests/unit/create/test_creator.py::test_py_pyc_missing[True-False] - RuntimeError: failed to query /Users/yusufertekin/Sites/virtualenv/.tox/py27...
FAILED tests/unit/create/test_creator.py::test_no_preimport_threading - RuntimeError: failed to query /Users/yusufertekin/Sites/virtualenv/.tox/py27/bin...
FAILED tests/unit/create/test_creator.py::test_python_path[on] - RuntimeError: failed to query /Users/yusufertekin/Sites/virtualenv/.tox/py27/bin/python...
FAILED tests/unit/create/test_creator.py::test_create_clear_resets[no_clear-cpython2-mac-framework] - RuntimeError: failed to query /Users/yusufertekin/...
FAILED tests/unit/create/test_creator.py::test_create_no_seed[root-cpython2-mac-framework-symlinks-isolated] - RuntimeError: failed to query /Users/yusu...
FAILED tests/unit/create/test_creator.py::test_py_pyc_missing[False-False] - RuntimeError: failed to query /Users/yusufertekin/Sites/virtualenv/.tox/py2...
FAILED tests/unit/create/test_creator.py::test_debug_bad_virtualenv - RuntimeError: failed to query /Users/yusufertekin/Sites/virtualenv/.tox/py27/bin/p...
FAILED tests/unit/create/test_creator.py::test_create_no_seed[root-cpython2-mac-framework-copies-isolated] - RuntimeError: failed to query /Users/yusufe...
FAILED tests/unit/create/test_creator.py::test_create_no_seed[root-cpython2-mac-framework-symlinks-global] - RuntimeError: failed to query /Users/yusufe...
FAILED tests/unit/create/test_creator.py::test_create_vcs_ignore_exists_override - RuntimeError: failed to query /Users/yusufertekin/Sites/virtualenv/.t...
FAILED tests/unit/create/test_creator.py::test_create_vcs_ignore_exists - RuntimeError: failed to query /Users/yusufertekin/Sites/virtualenv/.tox/py27/b...
FAILED tests/unit/create/test_creator.py::test_destination_not_write_able - assert 'SystemExit: ' in 'RuntimeError: failed to query /Users/yusufertekin/...
FAILED tests/unit/create/test_creator.py::test_python_path[off] - RuntimeError: failed to query /Users/yusufertekin/Sites/virtualenv/.tox/py27/bin/pytho...
FAILED tests/unit/create/test_creator.py::test_prompt_set[None-cpython2-mac-framework] - RuntimeError: failed to query /Users/yusufertekin/Sites/virtual...
FAILED tests/unit/create/test_creator.py::test_create_clear_resets[clear-cpython2-mac-framework] - RuntimeError: failed to query /Users/yusufertekin/Sit...
FAILED tests/unit/create/test_creator.py::test_create_no_seed[root-cpython2-mac-framework-copies-global] - RuntimeError: failed to query /Users/yusufert...
FAILED tests/unit/create/test_creator.py::test_destination_exists_file - assert 'SystemExit: ' in 'RuntimeError: failed to query /Users/yusufertekin/Sit...
FAILED tests/unit/create/test_creator.py::test_create_long_path - RuntimeError: failed to query /Users/yusufertekin/Sites/virtualenv/.tox/py27/bin/pytho...
FAILED tests/unit/create/test_creator.py::test_py_pyc_missing[True-True] - RuntimeError: failed to query /Users/yusufertekin/Sites/virtualenv/.tox/py27/...
FAILED tests/unit/create/test_creator.py::test_prompt_set[magic-cpython2-mac-framework] - RuntimeError: failed to query /Users/yusufertekin/Sites/virtua...
FAILED tests/unit/create/test_creator.py::test_py_pyc_missing[False-True] - RuntimeError: failed to query /Users/yusufertekin/Sites/virtualenv/.tox/py27...
FAILED tests/unit/discovery/test_discovery.py::test_discovery_fallback_ok - RuntimeError: failed to query /Users/yusufertekin/Sites/virtualenv/.tox/py27...
FAILED tests/unit/discovery/test_discovery.py::test_relative_path - RuntimeError: failed to query /Users/yusufertekin/Sites/virtualenv/.tox/py27/bin/pyt...
FAILED tests/unit/discovery/test_discovery.py::test_discovery_via_path[upper] - assert None is not None
FAILED tests/unit/discovery/test_discovery.py::test_discovery_via_path[mixed] - assert None is not None
FAILED tests/unit/discovery/test_discovery.py::test_discovery_via_path[lower] - assert None is not None
FAILED tests/unit/create/via_global_ref/test_build_c_ext.py::test_can_build_c_extensions[cpython2-mac-framework] - RuntimeError: failed to query /Users/...
FAILED tests/unit/seed/wheels/test_periodic_update.py::test_download_manual_ignores_pre_release - IOError: [Errno 2] No such file or directory: '/Librar...
FAILED tests/unit/seed/wheels/test_periodic_update.py::test_download_stop_with_embed - IOError: [Errno 2] No such file or directory: '/Library/Framework...
FAILED tests/unit/seed/wheels/test_periodic_update.py::test_do_update_skip_already_done - IOError: [Errno 2] No such file or directory: '/Library/Framew...
FAILED tests/unit/seed/wheels/test_periodic_update.py::test_download_manual_stop_after_one_download - IOError: [Errno 2] No such file or directory: '/Li...
FAILED tests/unit/seed/wheels/test_periodic_update.py::test_download_periodic_stop_at_first_usable_with_previous_minor - IOError: [Errno 2] No such file...
FAILED tests/unit/seed/wheels/test_periodic_update.py::test_pick_periodic_update - RuntimeError: failed to query /Users/yusufertekin/Sites/virtualenv/.t...
FAILED tests/unit/seed/wheels/test_periodic_update.py::test_download_periodic_stop_at_first_usable - IOError: [Errno 2] No such file or directory: '/Lib...
FAILED tests/unit/config/test_ini.py::test_ini_can_be_overwritten_by_flag - RuntimeError: failed to query /Users/yusufertekin/Sites/virtualenv/.tox/py27...
FAILED tests/unit/seed/embed/test_bootstrap_link_via_app_data.py::test_base_bootstrap_link_via_app_data_no[setuptools] - RuntimeError: failed to query /...
FAILED tests/unit/seed/embed/test_bootstrap_link_via_app_data.py::test_seed_link_via_app_data[True] - RuntimeError: failed to query /Users/yusufertekin/...
FAILED tests/unit/seed/embed/test_bootstrap_link_via_app_data.py::test_base_bootstrap_link_via_app_data_no[pip] - RuntimeError: failed to query /Users/y...
FAILED tests/unit/seed/embed/test_bootstrap_link_via_app_data.py::test_seed_link_via_app_data[False] - RuntimeError: failed to query /Users/yusufertekin...
FAILED tests/unit/seed/embed/test_bootstrap_link_via_app_data.py::test_base_bootstrap_link_via_app_data_no[wheel] - RuntimeError: failed to query /Users...
FAILED tests/unit/seed/embed/test_pip_invoke.py::test_base_bootstrap_via_pip_invoke[pip] - IOError: [Errno 2] No such file or directory: '/Library/Frame...
FAILED tests/unit/seed/embed/test_pip_invoke.py::test_base_bootstrap_via_pip_invoke[wheel] - IOError: [Errno 2] No such file or directory: '/Library/Fra...
FAILED tests/unit/seed/embed/test_pip_invoke.py::test_base_bootstrap_via_pip_invoke[] - IOError: [Errno 2] No such file or directory: '/Library/Framewor...
FAILED tests/unit/seed/embed/test_pip_invoke.py::test_base_bootstrap_via_pip_invoke[setuptools] - IOError: [Errno 2] No such file or directory: '/Librar...
FAILED tests/unit/create/test_creator.py::test_cross_major - RuntimeError: failed to query /usr/local/bin/python3 with code 2 err: "/Library/Frameworks/...
FAILED tests/integration/test_run_int.py::test_app_data_pinning - RuntimeError: failed to query /Users/yusufertekin/Sites/virtualenv/.tox/py27/bin/pytho...
========================================= 70 failed, 202 passed, 18 skipped, 2 warnings, 7 error in 62.43 seconds =========================================
For what it is worth it might look like a lot but there's a high degree of duplication in between there. So you might just need to fix two or three to fix it. I recommend picking one test and focus on fixing that, once it's fixed run the suite again and start over 👍
@gaborbernat I have some other deadlines to meet and to be honest, making 0 progress in 4 hours kinda scared me :) However, I suspect two things
@s-vitaliy I found temporal solution.
pip install -U virtualenv
python2 -m virtualenv venv
lipo -remove arm64e venv/bin/python -o venv/bin/python
lipo
manipulates Mach-O universal binary to disable arm64e (Apple Silicon) and venv/bin/python
will always run with Rosetta 2.
@isac322 The given workaround dint work for me
fatal error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo: input file (env/bin/python) must be a fat file when the -remove option is specified
Do I need to do something here to get this working?
@isac322 Thank you for looking into it. I wanted to let you know that the same problem happens no matter if an M1 or intel-based chip is used. It started to happen specifically after the Monterey upgrade. Also, I'm getting the same error on lipo command.
Hello @gaborbernat, I found the problem but I'm not sure how to solve it. I have a hack fix. I'll share it below. What happens is;
On sys documentation for python 2.7, it states that this is set on compile-time. So I believe unless any explicit intervention seems like there is no way to set this because we basically copy python compiled files into the virtualenv via global self do.
We could have updated it using the custom site.py created for python2 however, that's not loaded due to sys.prefix not pointing the virtualenv directory where custom site.py placed.
What I have done is, I created sitecustomizate.py under /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ and added this
import sys
import os
env_path = os.getenv('VIRTUAL_ENV')
if env_path:
sys.path = filter(
lambda val: val != '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages',
sys.path
) # Remove all occurrences of '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages'
sys.prefix = env_path # Set prefix as virtualenv home
sys.path.append(env_path + '/lib/python2.7/site-packages')
I don't have time at the moment to continue working on this. There must be a much better way to solve this. If you can guide me, I'd love to create a PR to contribute.
Copying custom site.py content under sitecustomize.py not remapping paths also due to reloading global site.py, global site-packages gets added to the PATH even though it becomes the last.
There are so many tests failing one main reason is what I describe above another is we are using Tox to run tests which uses virtualenv :) to create virtual environment for tests. Due to this bug, tox actually uses the global space to install dependencies and also virtualenv custom build. That's why I was seeing, global virtualenv module magically got broken or removed.
UPDATE: I've found out that some of the dependencies still gets installed under global site-packages, i.e. numpy Also tests not working, tox cannot create package. So this is still not a workaround :(
I'm not sure how Monterey upgrade affected this prefix problem. I found a StackOverflow post stating
"If the file lib/python
Maybe this has changed, therefore, path manipulation by the site.py not working anymore.
@gaborbernat Sorry to bug you. Adding sitecustomize.py not actually solve the problem because there are many packages directly importing site.py of python source.
Because I haven’t figured out why after Monterey upgrade custom site.py is not loading before global python site.py, I had to update site.py of python source code to work smarter like python3 site.py does and be able to read pyvenv.cfg file if present. I'm attaching it here. site_py.txt
With this change, no need to use fixed path for macos. So had to change _executables method under src/virtualenv/create/via_global_ref/builtin/cpython/mac_os.py
@classmethod
def _executables(cls, interpreter):
for _, targets, must, when in super(CPythonmacOsFramework, cls)._executables(interpreter):
# Make sure we use the embedded interpreter inside the framework, even if sys.executable points to the
# stub executable in ${sys.prefix}/bin.
# See http://groups.google.com/group/python-virtualenv/browse_thread/thread/17cab2f85da75951
host_exe = Path(interpreter.system_executable)
major, minor = interpreter.version_info.major, interpreter.version_info.minor
targets = OrderedDict(
(i, None) for i in ["python", "python{}".format(major), "python{}.{}".format(major, minor), host_exe.name]
)
must = RefMust.COPY if interpreter.version_info.major == 2 else RefMust.NA
yield host_exe, list(targets.keys()), must, RefWhen.ANY
Most of the magic on Python 2 happens here https://github.com/pypa/virtualenv/blob/main/src/virtualenv/create/via_global_ref/builtin/python2/site.py#L10-L24 :thinking: so the question is why is this file not doing what you want.
@gaborbernat because it's not getting executed. I tried to manually update site.py and mimic this file functionality into python original site.py but even then it didn't work as expected. Two reasons,
sys.prefix should be altered by executing this file I've pointed too. By design of how python binary works on V2 it should get executed. If Apple patched/altered that behaviour then all hope is lost and there's no way to get it working.
I found a StackOverflow post stating "If the file lib/python/dyn-load is found in that directory or any of its parent directories, that directory is set to be to be sys.exec_prefix on Linux or Mac. If the file lib/python/os.py is is found in the directory or any of its subdirectories, that directory is set to be sys.prefix on Linux, Mac, and Windows, with sys.exec_prefix set to the same value as sys.prefix on Windows. "
So I was assuming it'd get executed as you described but it's not. I try with Intel mac, also with an intel mac not upgraded Monterey, it works fine. Only with Monterey, it's not getting executed and sys.prefix points to global Python. Thank you for confirming.
Yeah, you can find more details here https://youtu.be/o1Vue9CWRxU https://gaborbernat.github.io/euro_python_2019/#/
Thank you these are great. I appreciate.
JFTR, as Apple dropped Python 2 from the base installation few days ago with the 12.3 update, the old workaround (that I used too because I was too lazy to investigate) just disappeared and there's no viable way to run Python 2 on macOS.
The problem is not specific to python.org, I've tested it against Homebrew and asdf and they all show the same behavior.
I wonder if this is the sign of the universe to drop Python 2 from all my FOSS projects. 🤪
I wonder if this is the sign of the universe to drop Python 2 from all my FOSS projects. 🤪
Seems like it. virtualenv
goal here was to support Python for 18 months after its EOL. And that seems to pass so anything that works now is chance driven and community supported only. The next release of virtualenv
will likely drop support for Python 2.
Hopefully someone figures out how to get virtualenv properly working on macOS 12.3.
Previously, my virtual environments were working flawlessly on 12.2.1 (intel Mac). After 12.3 removed the native python and I re-installed from python.org, I could not get virtualenvs to work as they did on 12.2.1. As the original poster mentions, python installs new software into the global python2 environment, instead of only in the virtualenv.
It looks like virtualenv's trick of adding a symlink to Python.framework/Versions/2.7/Python
in the virtualenv root and rewriting the dependent dylib list of my/venv/bin/python*
with the path of this symlink no longer works in 12.3.
I suspect macOS 12.3 affected the behaviour of the following block in getpath.c: https://github.com/python/cpython/blob/v2.7.18/Modules/getpath.c#L445 where sys.prefix
is derived, and as a result Python ends up skipping over the virtual environment. In macOS < 12.3, NSLibraryNameForModule()
probably returned the path to the /path/to/my/venv/.Python
symlink that virtualenv creates in the in the virtual environment root, and so that's what Python based sys.prefix
on. But in 12.3, NSLibraryNameForModule()
returns the resolved path, e.g., /Library/Frameworks/Python.framework/Versions/2.7/Python
. (I don't have a 12.2 system to test against so this is mostly guesswork.)
As a workaround:
$ cp /Library/Frameworks/Python.framework/Versions/2.7/Python /path/to/my/venv/Python
$ ln -sf Python /path/to/my/venv/.Python
$ ln -sf /Library/Frameworks/Python.framework/Versions/2.7/Resources /path/to/my/venv/Resources
Your virtualenv structure would look something like:
lrwxr-xr-x .Python@ -> Python
-rw-r--r-- .gitignore
-r-xr-xr-x Python*
lrwxr-xr-x Resources@ -> /Library/Frameworks/Python.framework/Versions/2.7/Resources
drwxr-xr-x bin/
drwxr-xr-x include/
drwxr-xr-x lib/
-rw-r--r-- pyvenv.cfg
Keeping the Python dylib name the same and adding the Resources
symlink seems to avoid breaking the code signature. (M1 macs may be stricter and require re-signing.)
Another workaround is to use the --copies
flag
python2.7 -m virtualenv --copies /path/to/my/venv
Tested with Python.org's 2.7.18 and virtualenv==20.15.1
on macOS 12.4.
Issue
Hello, we faced an issuie with virtualenv when it is used in python, downloaded form python.org: https://www.python.org/download/releases/2.7/ (Mac Installer disk image)
If virtualenv is created by virtualenv, installed in this package, pip ignores virtualenv and always installs packages into system site-packages: testbot@mac12-1005 ~ % ./broken_venv/bin/python -m pip install pytest \<cut output> testbot@mac12-1005 ~ % ./broken_venv/bin/python -m pip show pytest \<cut output> Location: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
If virtuelenv is created by built-in python (/usr/bin/python), virtualenv works correctly:
testbot@mac12-1005 ~ % ./correct_venv/bin/python -m pip install pytest \<cut output> testbot@mac12-1005 ~ % ./correct_venv/bin/python -m pip show pytest \<cut output> Location: /Users/testbot/correct_venv/lib/python2.7/site-packages
For some reason, new site-packages in broken_venv does not contain site-packages of new virtualenv:
Also I noticed that broken virtualenv does not detect base_prefix: "base_prefix": null and correct virtualenv contains "base_prefix": "/System/Library/Frameworks/Python.framework/Versions/2.7" (see detailed of bouth commands below)
We use test framework that need to be configured to access mac os UI for UI testing automation, so we cannot use directly /usr/bin/python and need to install pyton via dmg image.
Is it possible to fix this issue or create workaround to make virtualenv work correctly in our case?
Environment
OS:
pip list
of the host python wherevirtualenv
is installed:Output of the virtual environment creation
Incorrect virutualenv creation log Make sure to run the creation with
-vvv --with-traceback
:Correct virtualenv creation log