virtualenv 和 pyenv 是什么关系? [英] What is the relationship between virtualenv and pyenv?

查看:64
本文介绍了virtualenv 和 pyenv 是什么关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近学习了如何在我的工作流程中使用 virtualenv 和 virtualenvwrapper,但我在一些指南中看到了 pyenv,但我似乎无法理解 pyenv 是什么以及它与 virtualenv 有何不同/相似之处.pyenv 是 virtualenv 的更好/更新替代品还是免费工具?如果后者有什么不同,两者(和 virtualenvwrapper,如果适用)如何协同工作?

解决方案

Pyenvvirtualenv 是非常不同的工具,它们以不同的方式工作来做不同的事情:

  • Pyenv 是一个 bash 扩展——在 Windows 上不起作用——它会拦截你对 python、pip 等的调用,将它们定向到几个系统 python 工具之一——链.因此,您始终可以使用在所选 Python 版本中安装的所有库 - 因此对于必须在不同版本的 Python 之间切换的用户来说非常有用.

  • VirtualEnv,是纯 python,所以可以在任何地方使用,它复制了可选的特定版本 python 和 pip 本地到激活环境,可能包含也可能不包含指向当前系统工具链的链接,如果没有,您可以将已知的库子集安装到该环境中.因此,它几乎肯定更适合测试和部署,因为您确切地知道使用了哪些库、哪些版本,并且全局更改不会影响您的模块.

venv python > 3.3

请注意,从 Python 3.3 开始,有一个名为 venv 的 VirtualEnv 内置实现(在某些安装中,有一个名为 pyvenv 的包装器 - 这个包装器是 在 Python 3.6 中已弃用),应该优先使用它.为了避免包装器可能出现的问题,通过使用 /path/to/python3 -m venv desired/env/path 直接使用它通常是个好主意,或者您可以使用优秀的 py Windows 上的 python 选择器,带有 py -3 -m venv desired/env/path.它将创建使用 desired/env/path configure 指定的目录并适当地填充它.总的来说,它非常像使用 VirtualEnv.

其他工具

有许多值得一提和考虑的工具,因为它们可以帮助使用上述一个或多个:

  • VirtualEnvWrapper 管理和简化 VirtualEnv 的使用和管理 - 跨平台.
  • pyenv-virtualenvpyenv-installer,它提供了用于管理和连接 VirtualEnv 的 PyEnv 工具 - 有了这个,您可以拥有一个包含更多内容的基础安装一个版本的 python 并在每个版本中创建隔离的环境 - Linux/OS-X.由 Johann Visagie 推荐
  • PyInstaller 可以带走你的python代码,可能是开发的&在 VirtualEnv 下进行测试,并将其捆绑在一起,以便它可以运行一个没有安装您的 python 版本的平台 - 请注意,它不是交叉编译器,您将需要一台 Windows(虚拟)机器构建 Windows 安装等,但即使您可以确定将安装 python 但无法确定 python 的版本和所有库是否与您的代码兼容,它也很方便.

I recently learned how to use virtualenv and virtualenvwrapper in my workflow but I've seen pyenv mentioned in a few guides but I can't seem to get an understanding of what pyenv is and how it is different/similar to virtualenv. Is pyenv a better/newer replacement for virtualenv or a complimentary tool? If the latter what does it do differently and how do the two (and virtualenvwrapper if applicable) work together?

解决方案

Pyenv and virtualenv are very different tools that work in different ways to do different things:

  • Pyenv is a bash extension - will not work on Windows - that intercepts your calls to python, pip, etc., to direct them to one of several of the system python tool-chains. So you always have all the libraries that you have installed in the selected python version available - as such it is good for users who have to switch between different versions of python.

  • VirtualEnv, is pure python so works everywhere, it makes a copy of, optionally a specific version of, python and pip local to the activate environment which may or may not include links to the current system tool-chain, if it does not you can install just a known subset of libraries into that environment. As such it is almost certainly much better for testing and deployment as you know exactly which libraries, at which versions, are used and a global change will not impact your module.

venv python > 3.3

Note that from Python 3.3 onward there is a built in implementation of VirtualEnv called venv (with, on some installations a wrapper called pyvenv - this wrapper is deprecated in Python 3.6), which should probably be used in preference. To avoid possible issues with the wrapper it is often a good idea to use it directly by using /path/to/python3 -m venv desired/env/path or you can use the excellent py python selector on windows with py -3 -m venv desired/env/path. It will create the directory specified with desired/env/path configure and populate it appropriately. In general it is very much like using VirtualEnv.

Additional Tools

There are a number of tools that it is worth mentioning, and considering, as they can help with the use of one or more of the above:

  • VirtualEnvWrapper Manage and simplify the use and management of VirtualEnv - Cross Platform.
  • pyenv-virtualenv, installed by pyenv-installer, which gives PyEnv tools for managing and interfacing to VirtualEnv - with this you can have a base installation that includes more than one version of python and create isolated environments within each of them - Linux/OS-X. Suggested by Johann Visagie
  • PyInstaller can take your python code, possibly developed & tested under VirtualEnv, and bundle it up so that it can run one platforms that do not have your version of python installed - Note that it is not a cross compiler you will need a Windows (virtual-)machine to build Windows installs, etc., but it can be handy even where you can be sure that python will be installed but cannot be sure that the version of python and all the libraries will be compatible with your code.

这篇关于virtualenv 和 pyenv 是什么关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆