Correct way to use venv and pip in pypy
Asked Answered
H

1

6

I've been using cpython forever, but I'm new to pypy.

In cpython, this is how I use virtual environments and pip.

python3 -m venv venv
source venv/bin/activate
python3 -m pip install <package>

I recently started using pypy for a project, and noticed that the following works.

pypy3 -m venv venv
source venv/bin/activate
pypy3 -m pip install <package>

Questions:

  • Are there any differences between cpython venv/pip and pypy venv/pip?
  • Can I create a venv using cpython, and use it with pypy, or vice-versa?
  • Similarly, can I install packages using cpython's pip, and use them from pypy interpreter, or vice-versa?
  • Is what I'm doing "correct", or are there any downsides/issues I'll face in future if I go down this road.

Reasons why I prefer the python3 -m ... invocations:

  • venv is present in std. lib, so I don't have to globally install virtualenv.
  • Less ambiguous than using pip and pip3.

References:


EDIT:
Tried to share venv's between cpython and venv doesn't work (seems obvious in hindsight). It's still possible to create two separate venv's like python3 -m venv cpython_venv and pypy3 -m venv pypy_venv and switch between them as needed. python will be bound to cpython or pypy based on which virtual env is active, and pypi packages need to be installed in every venv where it's needed.

Horse answered 4/3, 2023 at 0:45 Comment(1)
pypy is supposed to be a seamless replacement for cypthon, so if there are differences, those are bugs. But of course, see pypy.org/features.html for more information.Brawley
J
3

Are there any differences between cpython venv/pip and pypy venv/pip?

Yes, PyPy make some changes in the venv Python code, so they may have some differences. Example for 3.7:

Can I create a venv using cpython, and use it with pypy, or vice-versa?

I wouldn't recommend that, since they presumably have good reasons for patching the stdlib venv code.

Similarly, can I install packages using cpython's pip, and use them from pypy interpreter, or vice-versa?

I wouldn't recommend that, for several reasons.

In the case of binary distributions with compatibility tags, the installer may select a wheel file which is specific to the Python interpreter that pip was running in. This package could be totally broken for a different Python runtime. Use python3 -m pip debug --verbose or pypy3 -m pip debug --verbose to list the supported compatibility tags of each runtime.

Even for pure-python packages with no compiled extensions you're not safe - it's also the job of the installer to generate bytecode (.pyc files) at installation time. If you install with a different interpreter, you'll get incompatible bytecode.

Python packages can and do specify conditional dependencies using environment markers in the packaging metadata. It's possible for dependency trees to be different between CPython and PyPy based on the platform_python_implementation environment marker.

Is what I'm doing "correct", or are there any downsides/issues I'll face in future if I go down this road.

Your usage shown in the question is correct.

Jacksonjacksonville answered 4/3, 2023 at 0:59 Comment(2)
If I read this marvelous answer correctly, beyond some possible ambiguities towards the question for the weak-minded, then spinning up a venv and then pip installing packages in it through pypy should work for running your pypy code that relies on those packages. Give or take whether dependencies can be satisfied for the pypy versions of things which pip will somehow look for when run with pypy?. Am I just too wishfully interpreting "Your usage shown in the question is correct", or, is my suggestion somehow correct? As it's not currently recommended elsewhere I can only believe I'm wrong.Tiberias
@matanster If you create the venv using the pypy interpreter (pypy -m venv .venv) and install packages using the associated pip (.venv/bin/pip install ...), you should be fine.Jacksonjacksonville

© 2022 - 2024 — McMap. All rights reserved.