python多版本和隔离环境配置

今天主要记录一下python常用的几个工具使用。pyenv,virtualenv,virtualenvwrapper,pyenv-virtualenv

各个工具的说明

以下是我的翻译

pyenv

pyenv用于隔离Python版本。例如,你可能想要针对Python 2.6,2.7,3.3,3.4和3.5测试您的代码,因此您需要一种方法在它们之间切换。一旦激活,它会将PATH环境变量加上〜/ .pyenv / shims,其中有特殊文件与Python命令(python,pip)匹配。这些不是Python提供的命令的副本;它们是根据PYENV_VERSION环境变量或.python版本文件或〜/ .pyenv/version文件决定运行哪个版本的Python的特殊脚本。 pyenv也使得使用pyenv install命令更容易下载和安装多个Python版本的过程。

virtualenv

virtualenv是python库创建独立环境的工具。如果你对这个工具不熟悉,我强烈建议你学习它,因为它是一个非常有用的工具,我会对这个答案的其余部分进行比较。
它通过在一个目录(例如:env/)中安装一堆文件,然后修改PATH环境变量以在其自定义bin目录(例如:env/bin/)前添加这些文件来工作。 python或python3二进制文件的精确副本放置在此目录中,但是Python会首先在环境目录中查找相对于其路径的库。激活后,您可以使用pip在虚拟环境中安装软件包。

pyenv-virtualenv

pyenv-virtualenv是pyenv的一个插件,可以让你在同一时间方便地使用pyenv和virtualenv。但是,如果您使用Python 3.3或更高版本,pyenv-virtualenv会尝试运行 python -m venv 如果可用,则替代virtualenv。如果您不想使用便利功能,则可以不使用pyenv-virtualenv 而使用virtualenv和pyenv。

virtualenvwrapper

virtualenvwrapper是virtualenv的一组扩展(请参阅文档)。它提供了像mkvirtualenv,lssitepackages这样的命令,特别是在不同virtualenv目录之间切换的工作。如果你想要多个virtualenv目录,这个工具特别有用。

pyenv-virtualenvwrapper

pyenv-virtualenvwrapper 是pyenv的一个插件,和pyenv一样,可以方便地将virtualenvwrapper集成到pyenv中。

pipenv

pipenv 由Kenneth Reitz(requests的作者)撰写的pipenv是该列表中的最新项目。它旨在将Pipfile,pip和virtualenv结合到命令行中的一个命令中。 virtualenv目录通常放置在〜/ .local / share / virtualenvs / XXX中,其中XXX是项目目录路径的散列。这与virtualenv不同,该目录通常位于当前工作目录中。

原文如下

  • virtualenv is a very popular tool that creates isolated Python environments for Python libraries. If you're not familiar with this tool, I highly recommend learning it, as it is a very useful tool, and I'll be making comparisons to it for the rest of this answer.
    It works by installing a bunch of files in a directory (eg: env/), and then modifying the PATHenvironment variable to prefix it with a custom bin directory (eg: env/bin/). An exact copy of the python or python3 binary is placed in this directory, but Python is programmed to look for libraries relative to its path first, in the environment directory. It's not part of Python's standard library, but is officially blessed by the PyPA (Python Packaging Authority). Once activated, you can install packages in the virtual environment using pip.
  • pyenv is used to isolate Python versions. For example, you may want to test your code against Python 2.6, 2.7, 3.3, 3.4 and 3.5, so you'll need a way to switch between them. Once activated, it prefixes the PATH environment variable with ~/.pyenv/shims, where there are special files matching the Python commands (python, pip). These are not copies of the Python-shipped commands; they are special scripts that decide on the fly which version of Python to run based on the PYENV_VERSION environment variable, or the .python-version file, or the ~/.pyenv/version file. pyenv also makes the process of downloading and installing multiple Python versions easier, using the command pyenv install.
  • pyenv-virtualenv is a plugin for pyenv by the same author as pyenv, to allow you to use pyenv and virtualenv at the same time conveniently. However, if you're using Python 3.3 or later, pyenv-virtualenv will try to run python -m venv if it is available, instead of virtualenv. You can use virtualenv and pyenv together without pyenv-virtualenv, if you don't want the convenience features.
  • virtualenvwrapper is a set of extensions to virtualenv (see docs). It gives you commands like mkvirtualenv, lssitepackages, and especially workon for switching between different virtualenv directories. This tool is especially useful if you want multiple virtualenvdirectories.
  • pyenv-virtualenvwrapper is a plugin for pyenv by the same author as pyenv, to conveniently integrate virtualenvwrapper into pyenv.
  • pipenv, by Kenneth Reitz (the author of requests), is the newest project in this list. It aims to combine Pipfile, pip and virtualenv into one command on the command-line. The virtualenv directory typically gets placed in ~/.local/share/virtualenvs/XXX, with XXXbeing a hash of the path of the project directory. This is different from virtualenv, where the directory is typically in the current working directory.

安装

安装pyenv

mac下安装pyenv

  • brew 安装,没有安装的brew先安装brew
brew install pyenv
  • 修改文件配置.bash_profile.添加如下配置
# pyenv配置
export PYENV_ROOT=$HOME/.pyenv
export PATH=$PYENV_ROOT/bin:$PATH
eval "$(pyenv init -)"
  • 重启shell。并使配置生效
exec $SHELL
source ~/.bash_profile

如果不想使用中出现莫名其妙的问题,可以安装如下程序
需要安装Xcode Command Line Tools和Homebrew
Xcode Command Line Tools安装指令

  • xcode-select 安装
xcode-select --install
  • 安装其他可选软件
brew install openssl readline xz

我在没有安装上述软件时指定pyenv install xxx 时出了一个错误,如下

zipimport.ZipImportError: can't decompress data; zlib not available

安装以后解决.

pyenv的常用命令

  • pyenv 安装其他版本的python
pyenv install 2.7.8
pyenv install 3.5.0
  • 卸载特定的Python版本
pyenv uninstall xxx
  • 显示当前活动的Python版本
pyenv version
----------------------
system (set by /Users/leon/.pyenv/version)
  • 查看所有的版本(带星号的表示是当前激活的版本)
pyenv versions
* system (set by /Users/leon/.pyenv/version)
  2.7.8
  3.5.0
  • Python的全局设置,整个系统生效
pyenv global 2.7.8
  • Python的局部设置,当前目录生效
pyenv local 2.7.6

安装pyenv-virtualenv

如果将pyenv安装到非标准目录中,请确保将此repo克隆到您安装到的任何位置的“plugins”目录中,默认使用(~/.pyenv)
下载包到指定目录

  • 下载包到指定目录
git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv

virtualenvs的自动激活,就是当你进入到你自己设置过的Python版本的目录下,自动切换到virtualenvs的模式,此时你的相关命令只在当前virtualenvs下有效果。

  • 修改.bash_profile
eval "$(pyenv virtualenv-init -)"
  • 重启并source后生效
exec $SHELL
sourcec ~/.bash_profile

pyenv-virtualenv的使用

要为使用pyenv的Python版本创建virtualenv,请运行 pyenv virtualenv,指定所需的Python版本和virtualenv目录的名称。

  • 从当前版本创建virtualenv
pyenv virtualenv venv27
  • 制定版本创建virtualenv
pyenv virtualenv 2.7.13 venv27

virtualenv安装

  • 通过pip安装virtualenv
pip install virtualenv
  • 测试您的安装
virtualenv --version

virtualenv使用

  • 为一个工程创建一个虚拟环境
cd my_project_folder
virtualenv my_project

virtualenv my_project 将会在当前的目录中创建一个文件夹,包含了Python可执行文件, 以及 pip 库的一份拷贝,这样就能安装其他包了。虚拟环境的名字(此例中是 my_project ) 可以是任意的;若省略名字将会把文件均放在当前目录。
在任何您运行命令的目录中,这会创建Python的拷贝,并将之放在叫做 my_project 的文件中。
也可以选择使用一个Python解释器(比如python2.7

virtualenv -p /usr/bin/python2.7 my_project

或者使用~/.bashrc的一个环境变量将解释器改为全局性的:

export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2.7
  • 要开始使用虚拟环境,其需要被激活:
$ source my_project/bin/activate

当前虚拟环境的名字会显示在提示符左侧(比如说 (my_project)您的电脑:您的工程 用户名$) 以让您知道它是激活的。从现在起,任何您使用pip安装的包将会放在 ``my_project文件夹中, 与全局安装的Python隔绝开。

  • 像平常一样安装包,比如:
$ pip install request
  • 如果您在虚拟环境中暂时完成了工作,则可以停用它:
deactivate

这将会回到系统默认的Python解释器,包括已安装的库也会回到默认的。
要删除一个虚拟环境,只需删除它的文件夹。(要这么直接执行 rm -rf my_project
然后一段时间后,您可能会有很多个虚拟环境散落在系统各处,您将有可能忘记它们的名字或者位置。

virtualenvwrapper安装

(确保 virtualenv 已经安装了)

  • 使用pip安装
pip install virtualenvwrapper
  • 编辑.bash_profile文件
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

virtualenvwrapper使用

  • 创建一个虚拟环境:
mkvirtualenv my_project

这会在 ~/.virtualenvs 中创建 my_project 文件夹。

  • 在虚拟环境上工作:
workon my_project

或者,您可以创建一个项目,它会创建虚拟环境,并在 $WORKON_HOME 中创建一个项目目录。 当您使用 workon myproject 时,会 cd 到项目目录中。

mkproject myproject

virtualenvwrapper 提供环境名字的tab补全功能。当您有很多环境, 并且很难记住它们的名字时,这就显得很有用。
workon 也能停止您当前所在的环境,所以您可以在环境之间快速的切换。

  • 停止是一样的:
 deactivate
  • 删除:
rmvirtualenv my_project
  • 其他有用的命令

lsvirtualenv 列举所有的环境。
cdvirtualenv导航到当前激活的虚拟环境的目录中,比如说这样您就能够浏览它的 site-packages 。
cdsitepackages 和上面的类似,但是是直接进入到 site-packages 目录中。
lssitepackages 显示 site-packages 目录中的内容。

pycharm中配置

  • 选择python版本

如图所示,在安装完pyenv以后,我安装了3.5.0和2.7.8版本的python环境,这两个环境都处.pyenv下。另外还有一个是mac系统自带的python环境。可以在此选择需要的python解释器版本

image

  • 创建virtualenv

image
image
image

这里的创建相当于命令

pyenv virtualenv 2.7.13 venv27

创建名称为venv27,指定python解释器版本为xxx的虚拟环境。
OK 大功告成

posted @ 2018-04-10 22:59  rilweic  阅读(478)  评论(0编辑  收藏  举报