简体   繁体   English

Python venv和virtualenvwrapper相结合

[英]Python venv and virtualenvwrapper combined

In Python 3.5 the recommended way to create virtual environments is with the venv , instead of virtualenv . 在Python 3.5中,创建虚拟环境的推荐方法是使用venv ,而不是virtualenv Still the python packaging tutorial mentions both tools. python包装教程仍然提到了这两种工具。

However virtualenvwrapper is a recommended wrapper tool to use when using virtualenv. 但是, virtualenvwrapper是使用virtualenv时使用的推荐包装工具。

My questions are then: 我的问题是:

  1. Is there a way to use virtualenvwrapper with venv? 有没有办法使用virtualenvwrapper与venv?
  2. Or could one even consider virtualenvwrapper not needed due to venv? 或者甚至可以考虑由于venv而不需要virtualenvwrapper? (I cannot see how this could be true since it is a wrapper solving another problem) (我看不出这是怎么回事,因为它是解决另一个问题的包装器)

Edit: I can see that there is some confusion in the answers to my question. 编辑:我可以看到我的问题的答案有一些混乱。 venv is Python's official equivalent of virtualenv, as explained in the links above. venv是Python官方版本的virtualenv,如上面的链接所述。 Multiple stack overflow questions suggests that venv should be used. 多个堆栈溢出问题表明应该使用venv。 As mentioned in the "duplicate" you suggested: 如“重复”中所述,您建议:

the introduction of venv is in part a response to that mess. venv的引入部分是对这一混乱的回应。 If you want to help improve the situation, I suggest you use venv and encourage others to do the same 如果你想帮助改善这种情况,我建议你使用venv并鼓励其他人也这样做

So it is encouraged to use venv. 所以鼓励使用venv。 But as this questions implies, if one is to use venv how does one use a wrapper like virtualenvwrapper 但正如这个问题所暗示的那样,如果要使用venv,那么如何使用像virtualenvwrapper这样的包装器

Append This script to in .bashrc / .bash_profile / .zshrc, then enables you basic management of venv. 将此脚本附加到.bashrc / .bash_profile / .zshrc中,然后启用对venv的基本管理。

In addition, you can extend the script by adding the following line so that it will also display the existing venv lists. 此外,您可以通过添加以下行来扩展脚本,以便它还显示现有的venv列表。

lsvenv(){
    ls $VENV_HOME
}
  1. Is there a way to use virtualenvwrapper with venv? 有没有办法使用virtualenvwrapper与venv?

Yes. 是。 Simply point WORKON_HOME at your venvs directory. 只需在您的venvs目录中指向WORKON_HOME即可。 Here's what I do in my ~/.zshrc , and I use a mix of virtualenv (rare now, just for a few legacy py2 needs) and venv (most common). 这是我在~/.zshrc所做的,我使用了virtualenv(现在很少见,只是为了一些遗留的py2需求)和venv(最常见)。 I renamed the default to .venvs to make it clear these are mostly Python 3 venvs and not virtualenvs. 我将默认值重命名为.venvs,以明确这些主要是Python 3 venvs而不是virtualenvs。

# Python Environment Handling
export WORKON_HOME=$HOME/.venvs  # Default name changed from virtualenv to highlight I am using python3 -m venv (aka pyvenv)
export PROJECT_HOME=$HOME/dev
source /usr/local/bin/virtualenvwrapper.sh  # symlinked to /Library/Frameworks/Python.framework/Versions/3.7/bin/virtualenvwrapper.sh
  1. Or could one even consider virtualenvwrapper not needed due to venv? 或者甚至可以考虑由于venv而不需要virtualenvwrapper? (I cannot see how this could be true since it is a wrapper solving another problem) (我看不出这是怎么回事,因为它是解决另一个问题的包装器)

venv == virtualenv (in short). venv == virtualenv(简而言之)。 venv does not supersede virtualenvwrapper for the same reason virtualenv does supersede it. venv并没有取代virtualenvwrapper,因为virtualenv取代了它。 Your hunch on this is correct. 你对此的预感是正确的。

  1. How is this better than just making an alias that sources the activate? 这比仅仅创建一个来源激活的别名更好吗?

Well, it's up to you to determine what you need, but I find virtualenvwrapper with zsh plugins virtualenv and virtualenvwrapper to be pretty great and better than raw aliases. 好吧,由你来决定你需要什么,但我发现virtualenvwrapper与zsh插件virtualenv和virtualenvwrapper相比非常好,比原始别名更好。

workon to list all venvs is very nice, then workon speech_analyzer to jump right in on it. workon列出所有venvs是非常好的,然后workon speech_analyzer直接跳上它。

  1. Other solutions? 其他方案?

You can also set up hooks to activate venvs on directory change, but if that's what you are after and only that, then that's essentially pipenv. 您还可以设置挂钩来激活目录更改中的venvs,但如果这就是您所追求的而且只是那个,那么这基本上就是pipenv。 Pipenv is great if that's all you want to do. 如果你想做的话,皮彭夫很棒。 Pipenv also has an interesting and promising lockfile feature, but it is too slow for development and too immature with issues in production to comment on at this time. Pipenv也有一个有趣且有前途的锁定文件功能,但它对于开发来说太慢了,而且对于生产中的问题来说太不成熟了。

But I've never liked the 1:1 environment per project workflow for the same reasons given here: https://chriswarrick.com/blog/2018/07/17/pipenv-promises-a-lot-delivers-very-little/ . 但是我从来没有喜欢每个项目工作流程的1:1环境,原因如下: https//chriswarrick.com/blog/2018/07/17/pipenv-promises-a-lot-delivers-very-little / Particularly these needs line up with mine of the multi project single environment: https://chriswarrick.com/blog/2018/07/17/pipenv-promises-a-lot-delivers-very-little/#nikola 特别是这些需求与我的多项目单一环境排成一行: https//chriswarrick.com/blog/2018/07/17/pipenv-promises-a-lot-delivers-very-little/#nikola

I have six environments on my machine and about 20 projects. 我的机器上有六个环境,大约有20个项目。 Pipenv doesn't extend to that situation. 皮彭夫并没有延伸到这种情况。 Pipenv insists on 20 environments for 20 projects. Pipenv坚持20个项目的20个环境。 It just doesn't work and creates more problems than it solves. 它只是不起作用,并创造了比它解决的更多的问题。 If you do have a 1:1 workflow though currently, then pipenv may be the tool you'd like. 如果您目前拥有1:1的工作流程,那么pipenv可能就是您喜欢的工具。 One word of caution, you can ~only~ do that workflow in pipenv, unfortunately. 需要注意的是,不过,你可以〜只做〜在pipenv做那个工作流程。

[EDIT, 8 Jul 19: Readers will probably find that this answer gives a fuller description of the different tools for handling virtual environments in Python. [编辑,7月8日19日:读者可能会发现这个答案更全面地描述了在Python中处理虚拟环境的不同工具。 They all have their issues, as does conda, which has a somewhat more sophisticated concept of "environment".] 他们都有自己的问题,康达也是如此,它有一个更为复杂的“环境”概念。

  1. As its name suggests, virtualenvwrapper was specifically designed to wrap virtualenv , on which it depends. 顾名思义, virtualenvwrapper专门用于包装virtualenv ,它取决于它。 I don't believe that anyone has similarly wrapped venv yet. 我不相信任何人都有类似包裹的venv

  2. venv is intended to do the basic work of creating virtual environments, but environment management has to be done with scripts. venv旨在完成创建虚拟环境的基本工作,但环境管理必须使用脚本完成。 Although shell scripting is often people's first resort, the venv module has an extensive API to help you in those tasks. 虽然shell脚本通常是人们的第一手段,但venv模块有一个广泛的API来帮助您完成这些任务。

Nowadays there are many options for creating Python virtual environments. 如今,创建Python虚拟环境有很多选择。 Besides those you mention, anaconda allows the creation and management of environments and even works pretty well with pip most of the time. 除了你提到的那些, anaconda允许创建和管理环境,甚至大部分时间都可以很好地使用pip

The tools in the virtual environment space have been designed to work together with standard Python distribution tools as far as possible, but the arrival of venv in Python 3.5 did not invalidate either virtualenv or virtualenvwrapper , which should both still work fine. 虚拟环境空间中的工具被设计为尽可能与标准的Python分发工具一起工作,但是venv在Python 3.5中的出现并没有使virtualenvvirtualenvwrapper无效,这两者应该仍然可以正常工作。

The venv module is principally a simple toolkit to allow the creation of virtualenvs inside programs, and is by no means intended to replace the convenience of virtualenvwrapper . venv模块主要是一个简单的工具包,允许在程序内部创建virtualenvs,并不打算取代virtualenvwrapper的便利性。 It simply meets a rather different set of needs. 它只是满足了一系列不同的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM