简体   繁体   English

如何使用pyenv,virtualenv和pipenv?

[英]How to work with pyenv, virtualenv and pipenv?

I was looking for a version manager for Python similar/equal to RVM (for Ruby), I found pyenv but it is merely a switcher so I'd need to combine it with virtualenv (my understanding so far), a little laborious but I could get used to it. 我正在寻找类似于/等于RVM的 Python版本管理器(对于Ruby),我发现pyenv但它只是一个切换器,所以我需要将它与virtualenv (我的理解到目前为止)结合起来,有点费力但我可以习惯它。 Moreover I've read pipenv is recommended to use instead of virtualenv , so could it work with pyenv ? 此外我读过pipenv建议使用而不是virtualenv ,所以它可以与pyenv一起使用吗? how? 怎么样?

However the article What is the difference between venv, pyvenv, pyenv, virtualenv, virtualenvwrapper, pipenv, etc? 然而文章 venv,pyvenv,pyenv,virtualenv,virtualenvwrapper,pipenv等有什么区别? mentionates pyenv is deprecated in Python 3.6. 提到 pyenv在Python 3.6中已弃用。 So I'm in zero and confused again, what should I use? 所以我又零而又困惑,我该怎么用? and how should I use it? 我应该如何使用它?

System: Arch Linux, Plasma. 系统:Arch Linux,Plasma。

Current (installed) versions for Python: 2.7.14 and 3.6.4 Python的当前(已安装)版本:2.7.14和3.6.4

What I plan to do: I need a framework where if for any reason I need to work with versions: 3.4.7, 3.2.6, 2.6.7 and 2.3.3, I could do it without any pain in the ass. 我打算做什么:我需要一个框架,如果由于任何原因我需要使用版本:3.4.7,3.2.6,2.6.7和2.3.3,我可以毫不费力地做到这一点。

I hope your help, thanks people. 我希望你的帮助,谢谢大家。

you have two options 你有两个选择

  1. use pyenv and pyenv-virtualenv wrapper together. 使用pyenv和pyenv-virtualenv包装器。

for example, you want to create a new project test and also want to create a virtual env for it. 例如,您想要创建一个新的项目测试,并且还想为它创建一个虚拟环境。

  • pyenv install 3.6.5 pyenv安装3.6.5
  • pyenv virtualenv 3.6.5 test pyenv virtualenv 3.6.5测试
  • cd /project_path cd / project_path
  • pyenv local test pyenv本地测试

next time, you access the project dir, it will change to test environment automatically 下次,您访问项目目录,它将自动更改为测试环境

  1. use pyenv and pipenv together 一起使用pyenv和pipenv

firstly, add this script to the env config(bashrc or zshenv, etc.) 首先,将此脚本添加到env配置(bashrc或zshenv等)

export PIPENV_VENV_IN_PROJECT=1
PROMPT_COMMAND='prompt'
precmd() { eval "$PROMPT_COMMAND" }
function prompt()
{
    if [ ! $PIPENV_ACTIVE ]; then
      if [ `pipenv --venv 2>/dev/null` ]; then
        export PIPENV_INITPWD="$PWD"
        pipenv shell
      fi
    elif [ $PIPENV_INITPWD ] ; then
      cd "$PIPENV_INITPWD"
      unset PIPENV_INITPWD
    fi
}

then 然后

  • pyenv install 3.6.5 pyenv安装3.6.5
  • pyenv shell 3.6.5 pyenv shell 3.6.5
  • pip install pipenv pip安装pipenv
  • cd /project_path cd / project_path
  • pipenv --python 3.6.5 pipenv --python 3.6.5

next time you access the directory, it will change to the correct vent(note: you should use pyenv shell 3.6.5 before you access the project dir) 下次访问目录时,它将更改为正确的通风口(注意:在访问项目目录之前应使用pyenv shell 3.6.5)

If I recall correctly, virtualenv is more for making sure your application isn't interfering with others on the same running system. 如果我没记错的话,virtualenv更多的是确保你的应用程序不会干扰同一个运行系统上的其他应用程序。 If you're running a UNIX based OS (Linux/FreeBSD/macOS) you could compile python (with configure --prefix=/opt/python/) and alias the binary executable in your .bashrc or bash_profile. 如果您正在运行基于UNIX的操作系统(Linux / FreeBSD / macOS),您可以编译python(使用configure --prefix = / opt / python /)并在.bashrc或bash_profile中为二进制可执行文件添加别名。

alias python363='/opt/python363/bin/python3'

or if you want to make it system wide and have access to root you can make a symlink ln -s /opt/python363/bin/python3 /usr/bin/python363 (for version 3.6.3 for example). 或者如果你想使它成为系统范围并且可以访问root,你可以创建一个符号链接ln -s /opt/python363/bin/python3 /usr/bin/python363 (例如版本3.6.3)。

then execute the file with python363 /path/to/your/script.py 然后用python363 /path/to/your/script.py执行该文件

Is there a better way to do it? 有没有更好的方法呢? Possibly, but it is one alternative to your predicament albeit not the most elegant. 可能,但它是你的困境的一种替代方案,尽管不是最优雅的。

  • NOTE: You'll also have to alias the pip command installed with that particular version in order to install libraries to the correct verison 注意:您还必须为使用该特定版本安装的pip命令添加别名,以便将库安装到正确的版本

ie in .bashrc or bash_profile 即在.bashrc或bash_profile中

alias pip363='/opt/python363/bin/pip3'

or as root with a symlink 或者作为符号链接的root用户

 ln -s /opt/python363/bin/pip3 /usr/bin/pip363

pyenv supports virtualenvs pyenv支持virtualenvs

Just install the python versions you want, for example: 只需安装所需的python版本,例如:

pyenv install 3.6.4
pyenv install 3.6.0

Select which python version you want with the argument global 使用参数global选择所需的python版本

pyenv global 3.6.4

Then create a virtualenv (I'll name it myve) 然后创建一个virtualenv(我将其命名为myve)

pyenv virtualenv myve

And activate it with activate 并通过激活激活它

pyenv activate myve

The only difference with your typical virtualenv created manually is the location and that you'll have a name for it, but, in the end, it's quite comfortable and similar to how virtualenvwrapper works. 与您手动创建的典型virtualenv的唯一区别是位置,并且您将拥有它的名称,但最后,它非常舒适,类似于virtualenvwrapper的工作方式。

Check which virtualenv or version is activated with 检查激活了哪个virtualenv或版本

pyenv versions

Deactivate with 停用

pyenv deactivate 

Remove a virtualenv or a python version with uninstall 使用uninstall删除virtualenv或python版本

pyenv uninstall myve

The recently old way 最近的老路

Use Pyenv and the first thing you do is install the virtualenv plugin. 使用Pyenv,你要做的第一件事是安装virtualenv插件。 Makes naming and using Virtual environments a charm. 使命名和使用虚拟环境成为一种魅力。

* *

The new way 新的方式

Still use Pyenv for maintaining python versions behind the scenes. 仍然使用Pyenv来维护幕后的python版本。 Use pipenv as package management + virtual environment tool. 使用pipenv作为包管理+虚拟环境工具。 The only trick is to add export PIPENV_PYTHON="$PYENV_ROOT/shims/python" to .rc file (.bashrc or .zshrc) after PYENV_ROOT gets updated using it's shim. 唯一的技巧是在使用它的垫片更新PYENV_ROOT后,将export PIPENV_PYTHON="$PYENV_ROOT/shims/python"到.rc文件(.bashrc或 .zshrc pyenv which python could go wrong later, but pipenv wouldn't. pyenv which python以后会出错,但pipenv不会。 I go into more detail in my blog post , shamelessly mentioning a plug. 我在博客文章中详细介绍了,无耻地提到了一个插件。

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

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