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

py3 venv creation fails when a virtualenv is active #1095

Closed
rpkilby opened this issue Oct 22, 2017 · 12 comments
Closed

py3 venv creation fails when a virtualenv is active #1095

rpkilby opened this issue Oct 22, 2017 · 12 comments

Comments

@rpkilby
Copy link

rpkilby commented Oct 22, 2017

If a user attempts to create a python 3 "venv" while working under an active "virtualenv", the resulting virtual environment will not be properly created.

As you can see below, the venv and virtualenv environments are both created with python 3's "venv" module. However, the latter was created under an active outer virtualenv, and is missing a number of binaries as well as the contents of its site-packages directory. python -m pip does function though.

Setup:

[vagrant@vagrant-arch ~]$ python3.6 -m venv venv
[vagrant@vagrant-arch ~]$ virtualenv -p python3.6 outer
[vagrant@vagrant-arch ~]$ source outer/bin/activate
(outer) [vagrant@vagrant-arch ~]$ python -m venv virtualenv

bin contents:

[vagrant@vagrant-arch ~]$ ls venv/bin/
activate  activate.csh  activate.fish  easy_install  easy_install-3.6  pip  pip3  pip3.6  python  python3  python3.6

[vagrant@vagrant-arch ~]$ ls virtualenv/bin/
activate  activate.csh  activate.fish  python  python3

site-packages contents:

[vagrant@vagrant-arch ~]$ ls venv/lib/python3.6/site-packages/
__pycache__  easy_install.py  pip  pip-9.0.1.dist-info  pkg_resources  setuptools  setuptools-28.8.0.dist-info

[vagrant@vagrant-arch ~]$ ls virtualenv/lib/python3.6/site-packages/
[vagrant@vagrant-arch ~]$ 
@rpkilby
Copy link
Author

rpkilby commented Oct 22, 2017

It turns out that python -m pip "works", however it installs packages into the original virtualenv.

(outer) [vagrant@vagrant-arch ~]$ deactivate 
[vagrant@vagrant-arch ~]$ source virtualenv/bin/activate
(virtualenv) [vagrant@vagrant-arch ~]$ python -m pip install tox

Is the active venv, should contain tox

(virtualenv) [vagrant@vagrant-arch ~]$ ls virtualenv/lib/python3.6/site-packages/
(virtualenv) [vagrant@vagrant-arch ~]$ ls virtualenv/bin/
activate  activate.csh  activate.fish  python  python3

But tox is installed to to the original virtualenv

(virtualenv) [vagrant@vagrant-arch ~]$ ls outer/lib/python3.6/site-packages/
__pycache__      pip                  pkg_resources  pluggy-0.5.2.dist-info  py-1.4.34.dist-info  setuptools-36.6.0.dist-info  six.py  tox-2.9.1.dist-info          virtualenv.py       wheel
easy_install.py  pip-9.0.1.dist-info  pluggy         py                      setuptools           six-1.11.0.dist-info         tox     virtualenv-15.1.0.dist-info  virtualenv_support  wheel-0.30.0.dist-info
(virtualenv) [vagrant@vagrant-arch ~]$ ls outer/bin 
activate  activate.csh  activate.fish  activate_this.py  easy_install  easy_install-3.6  pip  pip3  pip3.6  python  python-config  python3  python3.6  tox  tox-quickstart  virtualenv  wheel

I've tried creating a similar venv with the --copies option, but it had no effect.

@asottile
Copy link
Contributor

Hitting this here as well: pre-commit/pre-commit#755

I suspect a fix is needed from the venv side to do the .real_prefix dance. I'm going to investigate that approach as a workaround for pre-commit's sake

@asottile
Copy link
Contributor

This appears to be the bpo issue: https://bugs.python.org/issue30811

@rpkilby
Copy link
Author

rpkilby commented May 28, 2018

I suspect a fix is needed from the venv side to do the .real_prefix dance.

It's not clear to me who should be responsible for the fix. Should venv detect that it's being executed from within a virtualenv, or should virtualenv create an environment that's compatible with venv? On the one hand, virtualenv predates venv. On the other, venv is part of the standard library.

Regardless, here's the workaround that I currently have in tox-venv:

https://github.com/tox-dev/tox-venv/blob/58401663fda66dfba4f344553525c73d57432d5e/src/tox_venv/hooks.py#L10-L49

The executable path detection could be improved, as it currently just looks for python3. eg, you may want python3.6, but python3 points to python3.4.

@asottile
Copy link
Contributor

I opted for a similar workaround: https://github.com/pre-commit/pre-commit/blob/805a2921ad0d34698433972c6fcb1a6dca47191d/pre_commit/languages/python_venv.py#L13-L39

(main difference is pre-commit already does some normalization so I use bin/$(basename exe) instead of just bin/python3)

@gaborbernat
Copy link
Contributor

Eh, yeah overall I feel like we should be venv compatible.

@gaborbernat gaborbernat changed the title The "venv" module does not function properly while a "virtualenv" is active venv compatibility with virtualenv Jan 14, 2019
@FranklinYu
Copy link

@gaborbernat According to pew-org/pew#173, that doesn't seem to be the case. @uranusjr Care to chime in?

@stale
Copy link

stale bot commented Apr 14, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Just add a comment if you want to keep it open. Thank you for your contributions.

@stale stale bot added the wontfix label Apr 14, 2019
@pradyunsg
Copy link
Member

#1343

@stale stale bot removed the wontfix label Apr 14, 2019
@FranklinYu
Copy link

@gaborbernat I think the issue should be renamed to “running venv inside virtualenv” to differentiate it from #1339.

@rpkilby rpkilby changed the title venv compatibility with virtualenv py3 venv creation fails when a virtualenv is active Apr 15, 2019
@rpkilby
Copy link
Author

rpkilby commented Apr 15, 2019

@FranklinYu is the updated title sufficient?

@gaborbernat
Copy link
Contributor

This is becoming obsolete with #1366 now in full swing.

@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

No branches or pull requests

5 participants