使用PyPy时正确的venv和pip的方式。

huangapple go评论124阅读模式
英文:

Correct way to use venv and pip in pypy

问题

我一直在使用CPython,但是对PyPy是新手。

在CPython中,我使用虚拟环境和pip的方式如下。

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

最近我开始在一个项目中使用PyPy,并注意到以下方式也可以工作。

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

问题:

  • CPython的venv/pip和PyPy的venv/pip之间有什么区别吗?
  • 我能够使用CPython创建一个虚拟环境,然后在PyPy中使用它,反之亦然吗?
  • 同样,我能够使用CPython的pip安装包,然后从PyPy解释器中使用它们,反之亦然吗?
  • 我正在做的是“正确的”,还是如果我继续这样做,将来会遇到任何问题或缺点?

我更喜欢使用python3 -m ...命令的原因:

  • venv包含在标准库中,因此我不必全局安装virtualenv。
  • 比使用pippip3更明确。

参考:


编辑:
尝试在CPython和PyPy之间共享虚拟环境不起作用(事后看来很明显)。仍然可以创建两个单独的虚拟环境,如python3 -m venv cpython_venvpypy3 -m venv pypy_venv,根据需要在它们之间切换。python将绑定到CPython或PyPy,具体取决于哪个虚拟环境处于活动状态,并且需要在每个需要它的虚拟环境中安装PyPI包。

英文:

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 &lt;package&gt;

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 &lt;package&gt;

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.

答案1

得分: 3

  • 有没有cpython venv/pip和pypy venv/pip之间的区别?

是的,PyPy在venv Python代码中进行了一些更改,因此它们可能有一些区别。例如,对于3.7:

  • CPython stdilb:<https://github.com/python/cpython/blob/v3.7.13/Lib/venv/init.py>

  • PyPy stdlib:<https://github.com/mozillazg/pypy/blob/release-pypy3.7-v7.3.9/lib-python/3/venv/init.py>

  • 我可以使用cpython创建一个venv,并在pypy中使用它,反之亦然吗?

我不建议这样做,因为它们可能有充分的理由来修补stdlib venv代码。

  • 同样,我可以使用cpython的pip安装包,并从pypy解释器中使用它们,反之亦然吗?

我不建议这样做,由于几个原因。

对于带有兼容性标签的二进制分发,安装程序可能会选择一个特定于pip所在Python解释器的wheel文件。这个包可能对不同的Python运行时完全不兼容。使用 python3 -m pip debug --verbosepypy3 -m pip debug --verbose 来列出每个运行时支持的兼容性标签。

即使对于没有编译扩展的纯Python包,也不安全 - 安装时还是安装程序的任务生成字节码(.pyc文件)。如果使用不同的解释器安装,你将得到不兼容的字节码。

Python包可以并且通常使用环境标记在包装元数据中指定条件依赖关系。根据platform_python_implementation环境标记,依赖树在CPython和PyPy之间可能不同。

  • 我所做的是否“正确”,或者如果我继续这样做,将来是否会面临任何问题或问题?

你在问题中展示的用法是正确的。

英文:

> 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:

  • CPython stdilb: <https://github.com/python/cpython/blob/v3.7.13/Lib/venv/init.py>
  • PyPy stdlib: <https://github.com/mozillazg/pypy/blob/release-pypy3.7-v7.3.9/lib-python/3/venv/init.py>

> 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.

huangapple
  • 本文由 发表于 2023年3月4日 08:45:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632978.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定