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

Don't require virtualenv #20019

Open
foolip opened this issue Oct 31, 2019 · 12 comments
Open

Don't require virtualenv #20019

foolip opened this issue Oct 31, 2019 · 12 comments

Comments

@foolip
Copy link
Member

foolip commented Oct 31, 2019

When working on #19940 I had this question: do pip and virtualenv really need to be on PATH, or could the instructions be simplified by using them as modules instead?

It looks like ./wpt run never invokes pip internally, and running without pip on PATH works.

For virtualenv, however, not having it on PATH runs into this code:

self.virtualenv = find_executable("virtualenv")
if not self.virtualenv:
raise ValueError("virtualenv must be installed and on the PATH")

Finding virtualenv in a way similar to pip here would remove the requirement:

@property
def bin_path(self):
if sys.platform in ("win32", "cygwin"):
return os.path.join(self.path, "Scripts")
return os.path.join(self.path, "bin")
@property
def pip_path(self):
path = find_executable("pip", self.bin_path)
if path is None:
raise ValueError("pip not found")
return path

Alternatively, using pip and virtualenv as python -m pip/virtualenv or as Python modules could remove the requirement.

@jgraham are there complexities hiding here that I haven't run into yet?

@LukeZielinski how is this dealt with in Chromium?

@foolip
Copy link
Member Author

foolip commented Oct 31, 2019

Looks like the answer for Chromium is --skip-venv-setup.

@foolip
Copy link
Member Author

foolip commented Oct 31, 2019

Actually, that pip_path helper is not analogous to what I'm suggesting. That'll be the _venv/bin/pip binary, it doesn't find pip outside of PATH, like in ~/Library/Python/2.7/bin/pip.

@LukeZielinski
Copy link
Contributor

In Chromium, we set --skip-venv-setup and also set --venv to a directory with an already-setup venv.

@foolip
Copy link
Member Author

foolip commented Oct 31, 2019

I tried #20026 and for me locally on macOS, it means PATH doesn't need to be modified.

@foolip foolip changed the title Don't require virtualenv to be on PATH Don't require virtualenv Feb 11, 2021
@foolip
Copy link
Member Author

foolip commented Feb 11, 2021

After struggling in #27377 I wonder if we still need virtualenv or could simplify for contributors by relying on just venv instead? As far as I can tell, what we need is:

  • an isolated environment using whatever python executable ./wpt ended up using
  • pip at some minimum version (19.2.3 doesn't work for me)
  • installing requirements using pip
  • ensuring those installed dependencies are on PYTHONPATH, if that's how it works

The _venv directory is also used to save browser and driver binaries, but I'm not sure if anything needs to be put on PATH or not.

@jgraham @gsnedders before I go too far down this path, are there complications here I'm not aware of yet?

@jgraham
Copy link
Contributor

jgraham commented Feb 11, 2021

In general I like this idea. I just poked at it and quickly ran into https://stackoverflow.com/questions/27462582/how-can-i-activate-a-pyvenv-vitrualenv-from-within-python-activate-this-py-was (we're using execfile with activate_this.py into the python environment to set up the required paths &c. but venv doesn't support that, so we'd need a different solution there).

@foolip
Copy link
Member Author

foolip commented Feb 11, 2021

Aha, so that explains something. I didn't understand what exec() in activate() was, and it's like eval() in JavaScript.

I also noticed there was no activate_this.py, but I wonder what parts of it we need? Might we be able to get away with just using pip to install into _venv/something, and then setting PYTHONPATH?

@jgraham
Copy link
Contributor

jgraham commented Feb 12, 2021

I would at least be newrous if we didn't copy ~all of activate_this's behavior. So we could just vendor in that file I guess and hope that Python doesn't change in ways that require it to be updated.

@gsnedders
Copy link
Member

How difficult would it be to move to creating a subprocess and invoking the venv python?

@foolip
Copy link
Member Author

foolip commented May 7, 2021

@gsnedders I've experimented with that, invoking python -m venv from the main process works, but I haven't worked out how to update the main process to use the resulting directory without activate_this.py. I think that updating sys.path is actually sufficient, and if it is then a hack would be to invoke a script in the venv which prints sys.path, and then update the main process to match.

But I'm not too sure about that. I'm considering this also in light of web-platform-tests/rfcs#82, which might end up with a suggestion that changes how we currently use virtualenv, so right now I'm not attempting to just swap out virtualenv for python3 -m venv.

@gsnedders
Copy link
Member

@gsnedders I've experimented with that, invoking python -m venv from the main process works, but I haven't worked out how to update the main process to use the resulting directory without activate_this.py. I think that updating sys.path is actually sufficient, and if it is then a hack would be to invoke a script in the venv which prints sys.path, and then update the main process to match.

Note that there may still be differences due to sys.modules already being populated (and sys.exec_prefix etc. being wrong).

What I was suggesting was that the main process wouldn't run anything within the venv, but instead we'd run the majority of the application within a subprocess, which therefore eliminates the need for anything like virtualenv's activate_this.py.

@foolip
Copy link
Member Author

foolip commented May 7, 2021

@gsnedders Yeah, that's an idea worth exploring. When using pipenv run python wpt run (in my experiment) that's pretty much what ends up happening. I'm not sure what the added startup time from the child process would be, and also how much memory the parent process would end up sitting on, but probably it'd be OK.

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

No branches or pull requests

4 participants