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

Sessions using Python2.7 cannot reuse virtual envs (-r) as Python 2.7 doesn't have attr sys.base_prefix #449

Closed
crwilcox opened this issue Jun 11, 2021 · 9 comments · Fixed by #450

Comments

@crwilcox
Copy link
Collaborator

Describe the bug
A recent change (2021.6.6) caused reuse of virtual environments targeting older python versions (2.7, likely pre 3.3 as well?) from executing

How to reproduce

❯ nox -r -s system-2.7
nox > Running session system-2.7
nox > Command python2.7 -c import sys; print(getattr(sys, 'real_prefix', sys.base_prefix)) failed with exit code 1:
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: 'module' object has no attribute 'base_prefix'
nox > Session system-2.7 failed.

NOTE: the session will work if -r is not specified.

Expected behavior
I would expect it to reuse the existing virtual environment.

@crwilcox crwilcox changed the title Sessions using Python2.7 cannot reuse virtual envs (-r) as Python 2.7 doesn't support real_prefix Sessions using Python2.7 cannot reuse virtual envs (-r) as Python 2.7 doesn't have attr sys.base_prefix Jun 11, 2021
@crwilcox
Copy link
Collaborator Author

I suspect that this may need to also fall back to sys.prefix to support sessions with older interpreters?
https://github.com/theacodes/nox/pull/425/files#diff-c7026fde4100b6dc8f4450d13eab8b0da0133179ce24cca5a482ecb18c23ff0cR337

@cjolowicz
Copy link
Collaborator

cjolowicz commented Jun 11, 2021

I suspect that this may need to also fall back to sys.prefix to support sessions with older interpreters?

It would fix the crash, but the reuse would never happen:

$ /tmp/venv/bin/python -c "import sys; print(sys.prefix)"
/tmp/venv

$ python2.7 -c "import sys; print(sys.prefix)"
/usr

So the resolved interpreter and the interpreter in the environment would be considered different.

See also #441 (comment)

@cjolowicz
Copy link
Collaborator

cjolowicz commented Jun 11, 2021

We could fall back to reading base-prefix from the pyvenv.cfg file in the environment. This method requires virtualenv >= 20, but the older virtualenvs set sys.real_prefix instead (which we're already checking), so we'd be covered in all cases.

@cjolowicz
Copy link
Collaborator

cjolowicz commented Jun 12, 2021

In light of this issue and #441, we may want to consider a bugfix release with the staleness check backed out (#418, #425, #428).

@theacodes @crwilcox what do you think?

To recap, the following issues have surfaced so far:

The second of these issues arises in multiple constellations. So far we've seen two: virtualenv favoring its own interpreter over those on PATH, and an upstream bug in virtualenv's caching with pyenv.

One way to fix these issues would be the following:

  • read pyvenv.cfg instead of querying the interpreter in the existing environment, fall back to sys.real_prefix for old virtualenvs and sys.base_prefix for venv-style environments
  • replace the interpreter resolution in VirtualEnv by invoking virtualenv's discovery mechanism

The second change is large and would require extensive testing. We'll also have to depend on virtualenv >= 20 instead of virtualenv >= 14.

Reverting the feature would give us some time to approach this carefully.

@theacodes
Copy link
Collaborator

theacodes commented Jun 12, 2021 via email

@cjolowicz
Copy link
Collaborator

cjolowicz commented Jun 12, 2021

@theacodes

I'm fine with that.

Cool :) So here's a thought: Can we open a branch 2021.6.x (or so) from the current release, and target the reverting PR and release PR to that? AFAICT the release automation should work fine if the tag is pushed to that branch instead of main. (Is that also true for ReadTheDocs?) The advantage would be that there's no disruption on main (revert, release, revert the revert), and the bugfix release can contain only the revert, not everything that already landed. Does that make sense?

@theacodes
Copy link
Collaborator

theacodes commented Jun 12, 2021 via email

@cjolowicz
Copy link
Collaborator

It turned out that reverting the associated PRs was non-trivial, so I put it behind a feature flag instead, see #451

@crwilcox
Copy link
Collaborator Author

thanks @cjolowicz!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

3 participants