Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

conda commands as Python3 subprocesses inside a virtualenv fail under macOS #1704

Closed
nsoranzo opened this issue Mar 10, 2020 · 15 comments · Fixed by #1749
Closed

conda commands as Python3 subprocesses inside a virtualenv fail under macOS #1704

nsoranzo opened this issue Mar 10, 2020 · 15 comments · Fixed by #1749

Comments

@nsoranzo
Copy link
Contributor

nsoranzo commented Mar 10, 2020

Issue

Describe what's the expected behaviour and what you're observing.

Running any conda command as a Python3 subprocess inside of a virtualenv fails under macOS with ModuleNotFoundError: No module named 'conda'. This works fine under Linux or when using Python2 or virtualenv <20.
The same works fine also before activating and after deactivating the virtualenv.

Tested on the latest virtualenv 20.0.9.

See https://travis-ci.org/nsoranzo/bioblend/builds/660456892 for how to reproduce on TravisCI and example of success and failure of the same commands under Linux and macOS respectively.

Environment

Provide at least:

  • OS: macOS

  • pip list of the host python where virtualenv is installed:

    Package    Version
    ---------- -------
    GDAL       2.4.2  
    mercurial  5.2    
    numpy      1.17.4 
    pip        19.3.1 
    protobuf   3.11.1 
    setuptools 41.6.0 
    six        1.13.0 
    wheel      0.33.6 

Output of the virtual environment creation

Make sure to run the creation with -vvv --with-traceback:

152 setup logging to NOTSET [DEBUG report:43]
152 created app data folder /Users/travis/Library/Application Support/virtualenv [DEBUG app_data:52]
165 find interpreter for spec PythonSpec(implementation=CPython, major=3) [INFO builtin:44]
166 proposed PythonInfo(spec=CPython3.7.5.final.0-64, exe=/usr/local/opt/python/bin/python3.7, platform=darwin, version='3.7.5 (default, Nov  1 2019, 02:16:38) \n[Clang 10.0.0 (clang-1000.11.45.5)]', encoding_fs_io=utf-8-UTF-8) [INFO builtin:50]
166 accepted PythonInfo(spec=CPython3.7.5.final.0-64, exe=/usr/local/opt/python/bin/python3.7, platform=darwin, version='3.7.5 (default, Nov  1 2019, 02:16:38) \n[Clang 10.0.0 (clang-1000.11.45.5)]', encoding_fs_io=utf-8-UTF-8) [DEBUG builtin:52]
173 filesystem is not case-sensitive [DEBUG info:28]
278 create virtual environment via CPython3Posix(dest=/Users/travis/build/nsoranzo/bioblend/.venv, clear=False, global=False) [INFO session:52]
278 create folder /Users/travis/build/nsoranzo/bioblend/.venv/bin [DEBUG _sync:21]
278 create folder /Users/travis/build/nsoranzo/bioblend/.venv/lib/python3.7/site-packages [DEBUG _sync:21]
279 write /Users/travis/build/nsoranzo/bioblend/.venv/pyvenv.cfg [DEBUG pyenv_cfg:34]
279 	home = /usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7 [DEBUG pyenv_cfg:38]
279 	implementation = CPython [DEBUG pyenv_cfg:38]
279 	version_info = 3.7.5.final.0 [DEBUG pyenv_cfg:38]
279 	virtualenv = 20.0.9 [DEBUG pyenv_cfg:38]
280 	include-system-site-packages = false [DEBUG pyenv_cfg:38]
280 	base-prefix = /usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7 [DEBUG pyenv_cfg:38]
280 	base-exec-prefix = /usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7 [DEBUG pyenv_cfg:38]
280 	base-executable = /usr/local/opt/python/bin/python3.7 [DEBUG pyenv_cfg:38]
280 symlink /usr/local/opt/python/bin/python3.7 to /Users/travis/build/nsoranzo/bioblend/.venv/bin/python [DEBUG _sync:40]
282 create /Users/travis/build/nsoranzo/bioblend/.venv/lib/python3.7/site-packages/_virtualenv.py [DEBUG api:60]
283 create virtualenv import hook file /Users/travis/build/nsoranzo/bioblend/.venv/lib/python3.7/site-packages/_virtualenv.pth [DEBUG api:63]
283 ============================== target debug ============================== [DEBUG session:54]
284 debug via /Users/travis/build/nsoranzo/bioblend/.venv/bin/python /usr/local/lib/python3.7/site-packages/virtualenv/create/debug.py [DEBUG creator:182]
283 {
  "sys": {
    "executable": "/Users/travis/build/nsoranzo/bioblend/.venv/bin/python",
    "_base_executable": "/Users/travis/build/nsoranzo/bioblend/.venv/bin/python",
    "prefix": "/Users/travis/build/nsoranzo/bioblend/.venv",
    "base_prefix": "/usr/local/opt/python/bin/../Frameworks/Python.framework/Versions/3.7",
    "real_prefix": null,
    "exec_prefix": "/Users/travis/build/nsoranzo/bioblend/.venv",
    "base_exec_prefix": "/usr/local/opt/python/bin/../Frameworks/Python.framework/Versions/3.7",
    "path": [
      "/usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/lib/python37.zip",
      "/usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/lib/python3.7",
      "/usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload",
      "/Users/travis/build/nsoranzo/bioblend/.venv/lib/python3.7/site-packages"
    ],
    "meta_path": [
      "<class '_virtualenv._Finder'>",
      "<class '_frozen_importlib.BuiltinImporter'>",
      "<class '_frozen_importlib.FrozenImporter'>",
      "<class '_frozen_importlib_external.PathFinder'>"
    ],
    "fs_encoding": "utf-8",
    "io_encoding": "UTF-8"
  },
  "version": "3.7.5 (default, Nov  1 2019, 02:16:38) \n[Clang 10.0.0 (clang-1000.11.45.5)]",
  "os": "<module 'os' from '/usr/local/opt/python/bin/../Frameworks/Python.framework/Versions/3.7/lib/python3.7/os.py'>",
  "site": "<module 'site' from '/usr/local/opt/python/bin/../Frameworks/Python.framework/Versions/3.7/lib/python3.7/site.py'>",
  "datetime": "<module 'datetime' from '/usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/lib/python3.7/datetime.py'>",
  "math": "<module 'math' from '/usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload/math.cpython-37m-darwin.so'>",
  "json": "<module 'json' from '/usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/__init__.py'>"
} [DEBUG session:55]
354 add seed packages via FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, via=copy, app_data_dir=/Users/travis/Library/Application Support/virtualenv/seed-app-data/v1) [INFO session:59]
355 Attempting to acquire lock 4344232784 on /Users/travis/Library/Application Support/virtualenv/seed-app-data/v1/3.7/wheels.lock [DEBUG filelock:270]
356 Lock 4344232784 acquired on /Users/travis/Library/Application Support/virtualenv/seed-app-data/v1/3.7/wheels.lock [INFO filelock:274]
357 get bundled wheel /usr/local/lib/python3.7/site-packages/virtualenv/seed/embed/wheels/pip-20.0.2-py2.py3-none-any.whl [DEBUG acquire:51]
357 get bundled wheel /usr/local/lib/python3.7/site-packages/virtualenv/seed/embed/wheels/setuptools-45.3.0-py3-none-any.whl [DEBUG acquire:51]
358 get bundled wheel /usr/local/lib/python3.7/site-packages/virtualenv/seed/embed/wheels/wheel-0.34.2-py2.py3-none-any.whl [DEBUG acquire:51]
373 install wheel from wheel /Users/travis/Library/Application Support/virtualenv/seed-app-data/v1/3.7/wheels/wheel-0.34.2-py2.py3-none-any.whl via CopyPipInstall [DEBUG via_app_data:46]
373 build install image to /Users/travis/Library/Application Support/virtualenv/seed-app-data/v1/3.7/image/CopyPipInstall/wheel-0.34.2-py2.py3-none-any of wheel-0.34.2-py2.py3-none-any.whl [DEBUG base:57]
374 install setuptools from wheel /Users/travis/Library/Application Support/virtualenv/seed-app-data/v1/3.7/wheels/setuptools-45.3.0-py3-none-any.whl via CopyPipInstall [DEBUG via_app_data:46]
375 build install image to /Users/travis/Library/Application Support/virtualenv/seed-app-data/v1/3.7/image/CopyPipInstall/setuptools-45.3.0-py3-none-any of setuptools-45.3.0-py3-none-any.whl [DEBUG base:57]
375 install pip from wheel /Users/travis/Library/Application Support/virtualenv/seed-app-data/v1/3.7/wheels/pip-20.0.2-py2.py3-none-any.whl via CopyPipInstall [DEBUG via_app_data:46]
376 build install image to /Users/travis/Library/Application Support/virtualenv/seed-app-data/v1/3.7/image/CopyPipInstall/pip-20.0.2-py2.py3-none-any of pip-20.0.2-py2.py3-none-any.whl [DEBUG base:57]
418 changing mode of /var/folders/nz/vv4_9tw56nv9k3tkvyszvwg80000gn/T/tmp4niwmfd9/wheel-3.7 to 755 [INFO util:566]
419 changing mode of /var/folders/nz/vv4_9tw56nv9k3tkvyszvwg80000gn/T/tmp4niwmfd9/wheel to 755 [INFO util:566]
421 changing mode of /var/folders/nz/vv4_9tw56nv9k3tkvyszvwg80000gn/T/tmp4niwmfd9/wheel3 to 755 [INFO util:566]
432 copy directory /Users/travis/Library/Application Support/virtualenv/seed-app-data/v1/3.7/image/CopyPipInstall/wheel-0.34.2-py2.py3-none-any/wheel to /Users/travis/build/nsoranzo/bioblend/.venv/lib/python3.7/site-packages/wheel [DEBUG _sync:48]
466 copy directory /Users/travis/Library/Application Support/virtualenv/seed-app-data/v1/3.7/image/CopyPipInstall/wheel-0.34.2-py2.py3-none-any/wheel-0.34.2.dist-info to /Users/travis/build/nsoranzo/bioblend/.venv/lib/python3.7/site-packages/wheel-0.34.2.dist-info [DEBUG _sync:48]
474 copy /Users/travis/Library/Application Support/virtualenv/seed-app-data/v1/3.7/image/CopyPipInstall/wheel-0.34.2-py2.py3-none-any/wheel-0.34.2.virtualenv to /Users/travis/build/nsoranzo/bioblend/.venv/lib/python3.7/site-packages/wheel-0.34.2.virtualenv [DEBUG _sync:48]
475 changing mode of /Users/travis/build/nsoranzo/bioblend/.venv/bin/wheel-3.7 to 755 [INFO util:566]
475 changing mode of /Users/travis/build/nsoranzo/bioblend/.venv/bin/wheel to 755 [INFO util:566]
476 changing mode of /Users/travis/build/nsoranzo/bioblend/.venv/bin/wheel3 to 755 [INFO util:566]
476 generated console scripts wheel3 wheel-3.7 wheel [DEBUG base:53]
542 changing mode of /var/folders/nz/vv4_9tw56nv9k3tkvyszvwg80000gn/T/tmpdfedv6sv/easy_install to 755 [INFO util:566]
543 changing mode of /var/folders/nz/vv4_9tw56nv9k3tkvyszvwg80000gn/T/tmpdfedv6sv/easy_install3 to 755 [INFO util:566]
544 changing mode of /var/folders/nz/vv4_9tw56nv9k3tkvyszvwg80000gn/T/tmpdfedv6sv/easy_install-3.7 to 755 [INFO util:566]
563 copy /Users/travis/Library/Application Support/virtualenv/seed-app-data/v1/3.7/image/CopyPipInstall/setuptools-45.3.0-py3-none-any/easy_install.py to /Users/travis/build/nsoranzo/bioblend/.venv/lib/python3.7/site-packages/easy_install.py [DEBUG _sync:48]
564 copy directory /Users/travis/Library/Application Support/virtualenv/seed-app-data/v1/3.7/image/CopyPipInstall/setuptools-45.3.0-py3-none-any/pkg_resources to /Users/travis/build/nsoranzo/bioblend/.venv/lib/python3.7/site-packages/pkg_resources [DEBUG _sync:48]
584 copy directory /Users/travis/Library/Application Support/virtualenv/seed-app-data/v1/3.7/image/CopyPipInstall/setuptools-45.3.0-py3-none-any/setuptools to /Users/travis/build/nsoranzo/bioblend/.venv/lib/python3.7/site-packages/setuptools [DEBUG _sync:48]
655 copy directory /Users/travis/Library/Application Support/virtualenv/seed-app-data/v1/3.7/image/CopyPipInstall/setuptools-45.3.0-py3-none-any/setuptools-45.3.0.dist-info to /Users/travis/build/nsoranzo/bioblend/.venv/lib/python3.7/site-packages/setuptools-45.3.0.dist-info [DEBUG _sync:48]
667 copy /Users/travis/Library/Application Support/virtualenv/seed-app-data/v1/3.7/image/CopyPipInstall/setuptools-45.3.0-py3-none-any/setuptools-45.3.0.virtualenv to /Users/travis/build/nsoranzo/bioblend/.venv/lib/python3.7/site-packages/setuptools-45.3.0.virtualenv [DEBUG _sync:48]
668 changing mode of /Users/travis/build/nsoranzo/bioblend/.venv/bin/easy_install to 755 [INFO util:566]
669 changing mode of /Users/travis/build/nsoranzo/bioblend/.venv/bin/easy_install3 to 755 [INFO util:566]
670 changing mode of /Users/travis/build/nsoranzo/bioblend/.venv/bin/easy_install-3.7 to 755 [INFO util:566]
671 generated console scripts easy_install3 easy_install-3.7 easy_install [DEBUG base:53]
746 changing mode of /var/folders/nz/vv4_9tw56nv9k3tkvyszvwg80000gn/T/tmp9zqbno9l/pip to 755 [INFO util:566]
746 changing mode of /var/folders/nz/vv4_9tw56nv9k3tkvyszvwg80000gn/T/tmp9zqbno9l/pip3 to 755 [INFO util:566]
747 changing mode of /var/folders/nz/vv4_9tw56nv9k3tkvyszvwg80000gn/T/tmp9zqbno9l/pip-3.7 to 755 [INFO util:566]
781 copy directory /Users/travis/Library/Application Support/virtualenv/seed-app-data/v1/3.7/image/CopyPipInstall/pip-20.0.2-py2.py3-none-any/pip to /Users/travis/build/nsoranzo/bioblend/.venv/lib/python3.7/site-packages/pip [DEBUG _sync:48]
969 copy directory /Users/travis/Library/Application Support/virtualenv/seed-app-data/v1/3.7/image/CopyPipInstall/pip-20.0.2-py2.py3-none-any/pip-20.0.2.dist-info to /Users/travis/build/nsoranzo/bioblend/.venv/lib/python3.7/site-packages/pip-20.0.2.dist-info [DEBUG _sync:48]
972 copy /Users/travis/Library/Application Support/virtualenv/seed-app-data/v1/3.7/image/CopyPipInstall/pip-20.0.2-py2.py3-none-any/pip-20.0.2.virtualenv to /Users/travis/build/nsoranzo/bioblend/.venv/lib/python3.7/site-packages/pip-20.0.2.virtualenv [DEBUG _sync:48]
973 changing mode of /Users/travis/build/nsoranzo/bioblend/.venv/bin/pip to 755 [INFO util:566]
974 changing mode of /Users/travis/build/nsoranzo/bioblend/.venv/bin/pip3 to 755 [INFO util:566]
975 changing mode of /Users/travis/build/nsoranzo/bioblend/.venv/bin/pip-3.7 to 755 [INFO util:566]
975 generated console scripts pip3 pip pip-3.7 [DEBUG base:53]
975 Attempting to release lock 4344232784 on /Users/travis/Library/Application Support/virtualenv/seed-app-data/v1/3.7/wheels.lock [DEBUG filelock:315]
975 Lock 4344232784 released on /Users/travis/Library/Application Support/virtualenv/seed-app-data/v1/3.7/wheels.lock [INFO filelock:318]
976 add activators for Bash, CShell, Fish, PowerShell, Python, Xonsh [INFO session:65]
980 write /Users/travis/build/nsoranzo/bioblend/.venv/pyvenv.cfg [DEBUG pyenv_cfg:34]
980 	home = /usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7 [DEBUG pyenv_cfg:38]
980 	implementation = CPython [DEBUG pyenv_cfg:38]
981 	version_info = 3.7.5.final.0 [DEBUG pyenv_cfg:38]
981 	virtualenv = 20.0.9 [DEBUG pyenv_cfg:38]
981 	include-system-site-packages = false [DEBUG pyenv_cfg:38]
981 	base-prefix = /usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7 [DEBUG pyenv_cfg:38]
981 	base-exec-prefix = /usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7 [DEBUG pyenv_cfg:38]
982 	base-executable = /usr/local/opt/python/bin/python3.7 [DEBUG pyenv_cfg:38]
982 created virtual environment CPython3.7.5.final.0-64 in 833ms
  creator CPython3Posix(dest=/Users/travis/build/nsoranzo/bioblend/.venv, clear=False, global=False)
  seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, via=copy, app_data_dir=/Users/travis/Library/Application Support/virtualenv/seed-app-data/v1)
  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator [WARNING __main__:21]
@gaborbernat
Copy link
Contributor

@nsoranzo does the same happen within a venv, or only virtualenv is affected by this?

@nsoranzo
Copy link
Contributor Author

nsoranzo commented Mar 10, 2020

@gaborbernat It seems that venv is affected on macOS but not on Linux, as virtualenv: https://travis-ci.org/nsoranzo/bioblend/builds/660579284

@gaborbernat
Copy link
Contributor

gaborbernat commented Mar 10, 2020

@nsoranzo, in this case, this becomes either a conda or CPython venv module upstream bug, did you open tickets there?

A side effect of virtualenv moving to the venv style virtual environments is that users now tend to discover all the venv bugs here 🤷‍♂

@nsoranzo
Copy link
Contributor Author

nsoranzo commented Mar 10, 2020

Thanks for the fast response! No, I did not open an upstream issue yet, I didn't know it was a venv bug till now! (I don't think it's conda's, given it works fine on Linux and with old virtualenv).

I have feeling the issue could get more attention if you open it, but happy to do it myself if you prefer (Should this go on https://bugs.python.org/ ?).

@pfmoore
Copy link
Member

pfmoore commented Mar 10, 2020

Unless I'm missing something, the issue seems to be that the conda command expects to be able to import conda in the active Python, but conda (the library) isn't installed in the active venv, so it can't.

The fix is likely to be for the conda script to use a specific Python binary, not just pick up whatever is first on PATH.

Edit: My mistake, the conda script has a shebang:

#!/Users/travis/miniconda3/bin/python
# -*- coding: utf-8 -*-
import sys
# Before any more imports, leave cwd out of sys.path for internal 'conda shell.*' commands.
# see https://github.com/conda/conda/issues/6549
if len(sys.argv) > 1 and sys.argv[1].startswith('shell.') and sys.path and sys.path[0] == '':
    # The standard first entry in sys.path is an empty string,
    # and os.path.abspath('') expands to os.getcwd().
    del sys.path[0]
if __name__ == '__main__':
    from conda.cli import main
    sys.exit(main())

So it should be getting run by the right interpreter. Sorry for the noise 🙁

@gaborbernat
Copy link
Contributor

Yeah needs to go on bugs.python.org; have a feeling this could be related to python/cpython#9516 🤷‍♂

@nsoranzo
Copy link
Contributor Author

@pfmoore Yep, I was going to point out that I checked that in https://travis-ci.org/nsoranzo/bioblend/jobs/660579286#L182 (and the conda lib is indeed installed in /Users/travis/miniconda3/lib).

@gaborbernat
Copy link
Contributor

Note it could be a conda bug too, hence why saying that could be worthwhile open a bug first there.

@gaborbernat
Copy link
Contributor

I'll close the issue for now. Please post here the upstream bug report links, so if other people run into it they can be redirected. If anything actionable comes out of those I'll reopen and make the changes.

@nsoranzo nsoranzo changed the title conda commands as Python subprocesses inside a virtualenv fail under macOS conda commands as Python3 subprocesses inside a virtualenv fail under macOS Mar 12, 2020
@nsoranzo
Copy link
Contributor Author

Yeah needs to go on bugs.python.org; have a feeling this could be related to python/cpython#9516

@gaborbernat I can confirm this is indeed due to python/cpython#9516 , see https://travis-ci.org/github/nsoranzo/bioblend/jobs/665637175 where deleting os.environ['__PYVENV_LAUNCHER__'] before launching the conda subprocess fixes the issue.

Now that python/cpython#9516 has been merged, would you consider patching virtualenv to remove __PYVENV_LAUNCHER__ for Python3 environments on macOS, given it will take a while for the upstream PYthon fix to reach existing installations?

@gaborbernat gaborbernat reopened this Mar 23, 2020
@gaborbernat
Copy link
Contributor

gaborbernat commented Mar 23, 2020

We could patch it via the pth file. What would this imply python3.7.(<7), python3.8.(<3) on macOs?

@nsoranzo
Copy link
Contributor Author

We could patch it via the pth file. What would this imply python3.7.(<7), python3.8.(<3) on macOs?

I think: <=3.7.7 or (>=3.8 and <=3.8.2)
Or just <3.9 if you want to keep it simple, a guarded del like if '__PYVENV_LAUNCHER__' in os.environ: del os.environ['__PYVENV_LAUNCHER__'] shouldn't create issues on future 3.7 and 3.8 point releases.

@gaborbernat
Copy link
Contributor

I prefer to have the customization only active when needed, instead of always on and guarded. Let's not have redundant generated code.

@nsoranzo
Copy link
Contributor Author

Sure!

@gaborbernat
Copy link
Contributor

@pypa pypa locked and limited conversation to collaborators Jan 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants