安装pip、virtualenv和分发Python的正确方法是什么?

时间:2022-12-05 14:30:13

Short Question

  • What is the proper way to install pip, virtualenv, and distribute?
  • 安装pip、virtualenv和分发的正确方法是什么?

Background

In my answer to SO question 4314376, I recommended using ez_setup so that you could then install pip and virtualenv as follows:

在我的回答是4314376,我建议使用ez_setup,这样你就可以安装pip和virtualenv了:

curl -O http://peak.telecommunity.com/dist/ez_setup.py
sudo python ez_setup.py
sudo easy_install pip
sudo pip install virtualenv

I originally pulled these instructions from Jesse Noller's blog post So you want to use Python on the Mac?. I like the idea of keeping a clean global site-packages directory, so the only other packages I install there are virtualenvwrapper and distribute. (I recently added distribute to my toolbox because of this Python public service announcement. To install these two packages, I used:

我最初从Jesse Noller的博客文章中删除了这些指令,所以你想在Mac上使用Python。我喜欢保持一个干净的全局站点包目录的想法,所以我安装的其他软件包都是virtualenvwrapper和分发版。(由于这条Python公共服务声明,我最近添加了分发到我的工具箱。为了安装这两个包,我使用了:

sudo pip install virtualenvwrapper
curl -O http://python-distribute.org/distribute_setup.py
sudo python distribute_setup.py

No more setuptools and easy_install

To really follow that Python public service announcement, on a fresh Python install, I would do the following:

为了真正遵循Python公共服务声明,在一个新的Python安装上,我将执行以下操作:

curl -O http://python-distribute.org/distribute_setup.py
sudo python distribute_setup.py
sudo easy_install pip
sudo pip install virtualenv
sudo pip install virtualenvwrapper

Glyph's Rebuke

In a comment to my answer to SO question 4314376, SO user Glyph stated:

在我对这个问题的回答是4314376,所以用户字形说明:

NO. NEVER EVER do sudo python setup.py install whatever. Write a ~/.pydistutils.cfg that puts your pip installation into ~/.local or something. Especially files named ez_setup.py tend to suck down newer versions of things like setuptools and easy_install, which can potentially break other things on your operating system.

不。永远不要做sudo python设置。py安装等等。写~ / .pydistutils。将你的pip安装到~/的cfg。当地什么的。尤其是ez_setup命名的文件。py倾向于吸收新版本的东西,比如setuptools和easy_install,这可能会破坏操作系统上的其他东西。

Back to the short question

So Glyph's response leads me to my original question:

因此,Glyph的反应引出了我最初的问题:

  • What is the proper way to install pip, virtualenv, and distribute?
  • 安装pip、virtualenv和分发的正确方法是什么?

15 个解决方案

#1


169  

You can do this without installing anything into python itself.

您可以在不将任何东西安装到python本身的情况下做到这一点。

You don't need sudo or any privileges.

您不需要sudo或任何特权。

You don't need to edit any files.

您不需要编辑任何文件。

Install virtualenv into a bootstrap virtual environment. Use the that virtual environment to create more. Since virtualenv ships with pip and distribute, you get everything from one install.

将virtualenv安装到引导虚拟环境中。使用虚拟环境创建更多。由于virtualenv船舶具有pip和分布,您可以从一个安装中得到所有东西。

  1. Download virtualenv:
  2. 下载virtualenv: http://pypi.python.org/pypi/virtualenv https://pypi.python.org/packages/source/v/virtualenv/virtualenv/virtualenv/virtualenv/virtualenv/virtualenv/virtualenv/virtualenv/virtualenv/virtualenv/virtualenv/virtualenv/virtualenv/virtualenv/virtualenv/virtualenv/virtualenv/virtualenv/virtualenv/virtualenv/
  3. Unpack the source tarball
  4. 解压源代码tarball
  5. Use the unpacked tarball to create a clean virtual environment. This virtual environment will be used to "bootstrap" others. All of your virtual environments will automatically contain pip and distribute.
  6. 使用未打包的tarball创建一个干净的虚拟环境。这个虚拟环境将被用来“引导”其他人。所有虚拟环境将自动包含pip和分发。
  7. Using pip, install virtualenv into that bootstrap environment.
  8. 使用pip,将virtualenv安装到引导环境中。
  9. Use that bootstrap environment to create more!
  10. 使用引导环境创建更多!

Here is an example in bash:

下面是bash中的一个示例:

# Select current version of virtualenv:
VERSION=12.0.7
# Name your first "bootstrap" environment:
INITIAL_ENV=bootstrap
# Set to whatever python interpreter you want for your first environment:
PYTHON=$(which python)
URL_BASE=https://pypi.python.org/packages/source/v/virtualenv

# --- Real work starts here ---
curl -O $URL_BASE/virtualenv-$VERSION.tar.gz
tar xzf virtualenv-$VERSION.tar.gz
# Create the first "bootstrap" environment.
$PYTHON virtualenv-$VERSION/virtualenv.py $INITIAL_ENV
# Don't need this anymore.
rm -rf virtualenv-$VERSION
# Install virtualenv into the environment.
$INITIAL_ENV/bin/pip install virtualenv-$VERSION.tar.gz

Now you can use your "bootstrap" environment to create more:

现在您可以使用您的“引导”环境来创建更多:

# Create a second environment from the first:
$INITIAL_ENV/bin/virtualenv py-env1
# Create more:
$INITIAL_ENV/bin/virtualenv py-env2

Go nuts!

发疯!

Note

This assumes you are not using a really old version of virtualenv. Old versions required the flags --no-site-packges (and depending on the version of Python, --distribute). Now you can create your bootstrap environment with just python virtualenv.py path-to-bootstrap or python3 virtualenv.py path-to-bootstrap.

这假设您没有使用真正的老版本的virtualenv。旧版本需要标记——无站点包(并依赖于Python的版本——分发)。现在您可以使用python virtualenv创建引导环境。py路径到bootstrap或python3 virtualenv。py path-to-bootstrap。

#2


20  

I think Glyph means do something like this:

我认为雕文的意思是这样的:

  1. Create a directory ~/.local, if it doesn't already exist.
  2. 创建一个目录~ /。当地,如果它还不存在的话。
  3. In your ~/.bashrc, ensure that ~/.local/bin is on PATH and that ~/.local is on PYTHONPATH.
  4. 在~ /。bashrc,(确保~ /。本地/bin是在路径上的~/。当地PYTHONPATH环境。
  5. Create a file ~/.pydistutils.cfg which contains

    创建一个文件~ / .pydistutils。cfg包含

    [install]
    prefix=~/.local
    

    It's a standard ConfigParser-format file.

    它是一个标准的configparser格式文件。

  6. Download distribute_setup.py and run python distribute_setup.py (no sudo). If it complains about a non-existing site-packages directory, create it manually:

    下载distribute_setup。py并运行python发行版。py(无sudo)。如果它抱怨一个不存在的站点包目录,请手动创建它:

    mkdir -p ~/.local/lib/python2.7/site-packages/

    mkdir - p ~ /。local / lib / python2.7 /网站/

  7. Run which easy_install to verify that it's coming from ~/.local/bin

    运行easy_install来验证它来自~/.local/bin。

  8. Run pip install virtualenv
  9. pip安装virtualenv运行
  10. Run pip install virtualenvwrapper
  11. 运行pip安装virtualenvwrapper
  12. Create a virtual env containing folder, say ~/.virtualenvs
  13. 创建一个虚拟的env文件夹,例如~/.virtualenv。
  14. In ~/.bashrc add

    在~ /。bashrc添加(

    export WORKON_HOME
    source ~/.local/bin/virtualenvwrapper.sh
    

That's it, no use of sudo at all and your Python environment is in ~/.local, completely separate from the OS's Python. Disclaimer: Not sure how compatible virtualenvwrapper is in this scenario - I couldn't test it on my system :-)

就是这样,不使用sudo,而Python环境在~/中。本地的,完全独立于操作系统的Python。免责声明:不确定在这个场景中如何兼容virtualenvwrapper——我无法在我的系统上测试它:-)

#3


16  

If you follow the steps advised in several tutorials I linked in this answer, you can get the desired effect without the somewhat complicated "manual" steps in Walker's and Vinay's answers. If you're on Ubuntu:

如果您按照我在这个答案中链接的几个教程中所建议的步骤,您可以获得想要的效果,而不需要在Walker和Vinay的回答中使用一些复杂的“手动”步骤。如果你在Ubuntu上:

sudo apt-get install python-pip python-dev

The equivalent is achieved in OS X by using homebrew to install python (more details here).

通过使用homebrew来安装python(这里有更多的细节),可以在OS X中实现等效。

brew install python

With pip installed, you can use it to get the remaining packages (you can omit sudo in OS X, as you're using your local python installation).

安装了pip后,您可以使用它来获得剩余的包(在OS X中可以省略sudo,因为您正在使用本地python安装)。

sudo pip install virtualenvwrapper

(these are the only packages you need installed globally and I doubt that it will * with anything system-level from the OS. If you want to be super-safe, you can keep the distro's versions sudo apt-get install virtualenvwrapper)

(这是您在全球唯一需要安装的软件包,我怀疑它是否会与系统级别的操作系统发生冲突。如果您想要超级安全,您可以保留发行版的sudo apt-get安装virtualenvwrapper)

Note: in Ubuntu 14.04 I receive some errors with pip install, so I use pip3 install virtualenv virtualenvwrapper and add VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 to my .bashrc/.zshrc file.

注意:在Ubuntu 14.04中,我通过pip安装得到一些错误,所以我使用pip3安装virtualenv virtualenvwrapper,并将VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3添加到my .bashrc/。zshrc中。

You then append to your .bashrc file

然后附加到.bashrc文件。

export WORKON_HOME
source /usr/local/bin/virtualenvwrapper.sh

and source it

和源

. ~/.bashrc

This is basically it. Now the only decision is whether you want to create a virtualenv to include system-level packages

这基本上是。现在唯一的决定是您是否想要创建一个virtualenv来包含系统级的包。

mkvirtualenv --system-site-packages foo

where your existing system packages don't have to be reinstalled, they are symlinked to the system interpreter's versions. Note: you can still install new packages and upgrade existing included-from-system packages without sudo - I tested it and it works without any disruptions of the system interpreter.

在不需要重新安装现有系统包的地方,它们与系统解释器的版本是一致的。注意:您仍然可以安装新的包,并升级现有的包含自系统包的包,而不使用sudo——我测试了它,并且在系统解释器没有中断的情况下运行。

kermit@hocus-pocus:~$ sudo apt-get install python-pandas
kermit@hocus-pocus:~$ mkvirtualenv --system-site-packages s
(s)kermit@hocus-pocus:~$ pip install --upgrade pandas
(s)kermit@hocus-pocus:~$ python -c "import pandas; print(pandas.__version__)"
0.10.1
(s)kermit@hocus-pocus:~$ deactivate
kermit@hocus-pocus:~$ python -c "import pandas; print(pandas.__version__)"
0.8.0

The alternative, if you want a completely separated environment, is

另一种选择,如果你想要一个完全分离的环境,是。

mkvirtualenv --no-site-packages bar

or given that this is the default option, simply

或者假设这是默认选项。

mkvirtualenv bar

The result is that you have a new virtualenv where you can freely and sudolessly install your favourite packages

其结果是,您有了一个新的virtualenv,您可以在其中*地、毫无疑问地安装您最喜欢的软件包。

pip install flask

#4


12  

Python 3.4 onward

Python 3.3 adds the venv module, and Python 3.4 adds the ensurepip module. This makes bootstrapping pip as easy as:

Python 3.3添加了venv模块,而Python 3.4添加了ensurepip模块。这使得bootstrapping pip容易:

python -m ensurepip

python - m ensurepip

Perhaps preceded by a call to venv to do so inside a virtual environment.

在虚拟环境中,可能会有一个调用venv来这样做。

Guaranteed pip is described in PEP 453.

在PEP 453中描述了有保证的pip。

#5


10  

On Ubuntu:

在Ubuntu上:

sudo apt-get install python-virtualenv

sudo apt-get安装python-virtualenv

The package python-pip is a dependency, so it will be installed as well.

包python-pip是一个依赖项,因此它也将被安装。

#6


5  

Update: As of July 2013 this project is no longer maintained. The author suggests using pyenv. (pyenv does not have built-in support for virtualenv, but plays nice with it.)

更新:截至2013年7月,该项目已不再维护。作者建议使用pyenv。(pyenv没有内置对virtualenv的支持,但是对它很好。)

Pythonbrew is a version manager for python and comes with support for virtualenv.

Pythonbrew是python的版本管理器,它支持virtualenv。

After installing pythonbrew and a python-version using venvs is really easy:

在安装了pythonbrew和python版本后,使用venvs真的很简单:

# Initializes the virtualenv 
pythonbrew venv init

# Create a virtual/sandboxed environment 
pythonbrew venv create mycoolbundle  

# Use it 
pythonbrew venv use mycoolbundle

#7


4  

I made this procedure for us to use at work.

我做这个程序是为了让我们在工作中使用。

cd ~
curl -s https://pypi.python.org/packages/source/p/pip/pip-1.3.1.tar.gz | tar xvz
cd pip-1.3.1
python setup.py install --user
cd ~
rm -rf pip-1.3.1

$HOME/.local/bin/pip install --user --upgrade pip distribute virtualenvwrapper

# Might want these three in your .bashrc
export PATH=$PATH:$HOME/.local/bin
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS="--distribute"
source $HOME/.local/bin/virtualenvwrapper.sh

mkvirtualenv mypy
workon mypy
pip install --upgrade distribute
pip install pudb # Or whatever other nice package you might want.

Key points for the security minded:

安全意识的要点:

  1. curl does ssl validation. wget doesn't.
  2. 旋度是ssl验证。wget没有。
  3. Starting from pip 1.3.1, pip also does ssl validation.
  4. 从pip 1.3.1开始,pip也进行了ssl验证。
  5. Fewer users can upload the pypi tarball than a github tarball.
  6. 可以上传pypi tarball的用户要少于github tarball。

#8


3  

There is no problem to do sudo python setup.py install, if you're sure it's what you want to do.

做sudo python设置是没有问题的。py安装,如果你确定它是你想做的。

The difference is that it will use the site-packages directory of your OS as a destination for .py files to be copied.

不同之处在于,它将使用您的OS的站点包目录作为.py文件的目的地。

so, if you want pip to be accessible os wide, that's probably the way to go. I do not say that others way are bad, but this is probably fair enough.

所以,如果你想让pip可以访问操作系统,那很可能就是这样。我并不是说其他人的方式不好,但这可能是公平的。

#9


3  

Install ActivePython. It includes pip, virtualenv and Distribute.

安装ActivePython。它包括pip、virtualenv和分发。

#10


2  

I came across the same problem recently. I’m becoming more partial to the “always use a virtualenv” mindset, so my problem was to install virtualenv with pip without installing distribute to my global or user site-packages directory. To do this, I manually downloaded distribute, pip and virtualenv, and for each one I ran “python setup.py install --prefix ~/.local/python-private” (with a temporary setting of PYTHONPATH=~/.local/python-private) so that setup scripts were able to find distribute). I’ve moved the virtualenv script to another directory I have on my PATH and edited it so that the distribute and virtualenv modules can be found on sys.path. Tada: I did not install anything to /usr, /usr/local or my user site-packages dir, but I can run virtualenv anywhere, and in that virtualenv I get pip.

我最近遇到了同样的问题。我越来越倾向于“总是使用虚拟环境”的心态,所以我的问题是在没有安装到我的全局或用户站点包目录的情况下,用pip安装virtualenv。为此,我手动下载了分发、pip和virtualenv,并为每个我运行的“python设置”。py安装——prefix ~ /。本地/python-私有”(使用python的临时设置=~/.local/python-private),以便设置脚本能够找到分布)。我将virtualenv脚本移动到路径上的另一个目录,并编辑它,以便在sys.path中找到分发和virtualenv模块。Tada:我没有安装任何东西到/usr, /usr/local或我的用户site-package dir,但是我可以在任何地方运行virtualenv,在这个virtualenv中我得到pip。

#11


2  

  • You can do this without installing anything into python itself.

    您可以在不将任何东西安装到python本身的情况下做到这一点。

  • You don't need sudo or any privileges.

    您不需要sudo或任何特权。

  • You don't need to find the latest version of a virtualenv tar file

    您不需要找到virtualenv tar文件的最新版本。

  • You don't need to edit version info in a bash script to keep things up-to-date.

    您不需要在bash脚本中编辑版本信息以保持更新。

  • You don't need curl/wget or tar installed, nor pip or easy_install

    您不需要安装curl/wget或tar,也不需要pip或easy_install。

Save the following to /tmp/initvenv.py:

保存如下/tmp/initvenv.py:

from __future__ import print_function

import os, sys, shutil, tempfile, subprocess, tarfile, hashlib

try:
    from urllib2 import urlopen
except ImportError:
    from urllib.request import urlopen

tmp_dir = tempfile.mkdtemp(prefix='initvenv_')
try:
    # read the latest version from PyPI
    f = urlopen("https://pypi.python.org/pypi/virtualenv/")
    # retrieve the .tar.gz file
    for line in f.read().splitlines():
        if isinstance(line, bytes):
            line = line.decode('utf-8')
        if 'tar.gz<' not in line:
            continue
        for url in line.split('"'):
            if url.startswith('http'):
                url, md5 = url.split('#')
                assert md5.startswith('md5=')
                md5 = md5[4:]
                break
        else:
            continue
        break
    else:
        print('tar.gz not found')
        sys.exit(1)
    file_name = url.rsplit('/', 1)[1]
    print(file_name)
    # url = "https://pypi.python.org/packages/source/v/virtualenv/" + file_name
    os.chdir(tmp_dir)
    with open(file_name, 'wb') as fp:
        data = urlopen(url).read()
        data_md5 = hashlib.md5(data).hexdigest()
        if md5 != data_md5:
            print('md5 not correct')
            print(md5)
            print(data_md5)
            sys.exit(1)
        fp.write(data)
    tar = tarfile.open(file_name)
    tar.extractall()
    tar.close()
    os.chdir(file_name.replace('.tar.gz', ''))
    print(subprocess.check_output([sys.executable, 'virtualenv.py'] +
                                  [sys.argv[1]]).decode('utf-8'), end='')
    if len(sys.argv) > 2:
        print(subprocess.check_output([
            os.path.join(sys.argv[1], 'bin', 'pip'), 'install', 'virtualenv'] +

            sys.argv[2:]).decode('utf-8'), end='')
except:
    raise
finally:
    shutil.rmtree(tmp_dir)  # always clean up

and use it as

和使用它

python_binary_to_use_in_venv /tmp/initvenv.py your_venv_name [optional packages]

e.g. (if you really need the distribute compatibility layer for setuptools)

(如果你真的需要setuptools的分发兼容层)

python /tmp/initvenv.py venv distribute

Please note that, with older python versions, this might give you InsecurePlatformWarnings¹.

请注意,与旧的python版本,这可能会给你InsecurePlatformWarnings¹。

Once you have your virtualenv (name e.g. venv) you can setup another virtualenv by using the virtualenv just installed:

一旦你有了virtualenv(名字,venv),你就可以通过使用virtualenv来设置另一个virtualenv:

venv/bin/virtualenv venv2

virtualenvwrapper

I recommend taking a look at virtualenvwrapper as well, after a one time setup:

我建议在一个时间设置之后,再看一下virtualenvwrapper:

% /opt/python/2.7.10/bin/python /tmp/initvenv.py venv virtualenvwrapper

and activation (can be done from your login script):

激活(可以从你的登录脚本完成):

% source venv/bin/virtualenvwrapper.sh

you can do things like:

你可以这样做:

% mktmpenv 
New python executable in tmp-17bdc3054a46b2b/bin/python
Installing setuptools, pip, wheel...done.
This is a temporary environment. It will be deleted when you run 'deactivate'.
(tmp-17bdc3054a46b2b)% 

¹ I have not found a way to suppress the warning. It could be solved in pip and/or request, but the developers point to each other as the cause. I got the, often non-realistic, recommendation to upgrade the python version I was using to the latest version. I am sure this would break e.g my Linux Mint 17 install. Fortunately pip caches packages, so the Warning is made only once per package install.

¹我还没有找到一个方法来抑制警告。它可以在pip和/或请求中得到解决,但是开发人员将彼此视为原因。我得到了,通常是不现实的建议,升级我使用的python版本的最新版本。我确信这将打破e。我的Linux Mint 17安装。幸运的是,pip缓存包,因此警告只在每个包安装一次。

#12


2  

I've had various problems (see below) installing upgraded SSL modules, even inside a virtualenv, on top of older OS-provided Python versions, so I now use pyenv.

我遇到过各种各样的问题(见下面),安装升级的SSL模块,甚至在virtualenv中,在旧的os提供的Python版本之上,所以我现在使用pyenv。

pyenv makes it very easy to install a new Python versions and supports virtualenvs. Getting started is much easier than the recipes for virtualenv listed in other answers:

pyenv使安装新的Python版本和支持virtualenv变得非常容易。开始比在其他答案中列出的virtualenv的食谱要容易得多:

  • On Mac, type brew install pyenv and on Linux, use pyenv-installer
  • 在Mac电脑上,类型brew安装pyenv和Linux,使用pyenv安装程序。
  • this gets you built-in virtualenv support as well as Python version switching (if required)
  • 这将为您提供内置的virtualenv支持以及Python版本切换(如果需要)
  • works well with Python 2 or 3, can have many versions installed at once
  • 与Python 2或3兼容,可以同时安装多个版本。

This works very well to insulate the "new Python" version and virtualenv from system Python. Because you can easily use a more recent Python (post 2.7.9), the SSL modules are already upgraded, and of course like any modern virtualenv setup you are insulated from the system Python modules.

这可以很好地隔离“新Python”版本和来自系统Python的virtualenv。因为您可以很容易地使用最近的Python (post 2.7.9), SSL模块已经升级了,当然,就像任何现代的virtualenv设置一样,您可以与系统Python模块隔离。

A couple of nice tutorials:

一些不错的教程:

  • Using pyenv and virtualenv - when selecting a Python version, it's easier to use pyenv global 3.6.1 (global to current user) or pyenv local 2.7.13 (local to current directory).
  • 使用pyenv和virtualenv——在选择Python版本时,更容易使用pyenv全局3.6.1(全局对当前用户)或pyenv本地2.7.13(本地到当前目录)。
  • pyenv basics and use with virtualenv
  • pyenv基础和与virtualenv的使用。

The pyenv-virtualenv plugin is now built in - type pyenv commands | grep virtualenv to check. I wouldn't use the pyenv-virtualenvwrapper plugin to start with - see how you get on with pyenv-virtualenv which is more integrated into pyenv, as this covers most of what virtualenvwrapper does.

pyenv-virtualenv插件现在内置了- type pyenv命令| grep virtualenv检查。我不会使用pyenv-virtualenvwrapper插件——看看你如何使用pyenv-virtualenv,它更集成到pyenv中,因为它涵盖了virtualenvwrapper所做的大部分工作。

pyenv is modelled on rbenv (a good tool for Ruby version switching) and its only dependency is bash.

pyenv是模仿rbenv (Ruby版本转换的一个好工具),它唯一的依赖是bash。

  • pyenv is unrelated to the very similarly named pyvenv - that is a virtualenv equivalent that's part of recent Python 3 versions, and doesn't handle Python version switching
  • pyenv与非常类似的pyvenv无关——它是最近Python 3版本的一部分,并且不处理Python版本转换。

Caveats

Two warnings about pyenv:

两个警告pyenv:

  1. It only works from a bash or similar shell - or more specifically, the pyenv-virtualenv plugin doesn't like dash, which is /bin/sh on Ubuntu or Debian.
  2. 它只适用于bash或类似的shell——或者更确切地说,pyenv-virtualenv插件不喜欢dash,它是/bin/sh在Ubuntu或Debian上。
  3. It must be run from an interactive login shell (e.g. bash --login using a terminal), which is not always easy to achieve with automation tools such as Ansible.
  4. 它必须从交互式的登录shell(例如bash——使用终端登录)中运行,使用自动化工具(如anable)并不总是很容易实现。

Hence pyenv is best for interactive use, and less good for scripting servers.

因此,pyenv最适合于交互式使用,而对脚本服务器则不太好。

SSL module problems

One reason to use pyenv is that there are often problems with upgrading Python SSL modules when using older system-provided Python versions:

使用pyenv的一个原因是,在使用旧的系统提供的Python版本时,升级Python SSL模块经常会出现问题:

  • Ubuntu 14.04 includes Python 2.7.6 which requires considerable effort to upgrade it to the right SSL modules so it handles SNI (server name indication) as client. (I wrote some Ansible scripts for this which was quite painful and still broke in some cases.)
  • ubuntu14.04包含Python 2.7.6,需要相当大的努力才能将其升级到正确的SSL模块,因此它可以作为客户机处理SNI(服务器名表示)。(我写了一些很痛苦的脚本,在某些情况下还会被打破。)
  • upgraded SSL modules will be more important as Python.org sites move to TLS 1.2 only during 2017 and 2018.
  • 升级的SSL模块将更重要,因为Python.org网站只在2017年和2018年迁移到TLS 1.2。

#13


0  

There are good instructions on the Virtualenv official site. https://pypi.python.org/pypi/virtualenv

在Virtualenv官方网站上有很好的说明。https://pypi.python.org/pypi/virtualenv

Basically what I did, is install pip with sudo easy_install pip, then used sudo pip install virtualenv then created an environment with: virtualenv my_env (name it what you want), following that I did: virtualenv --distribute my_env; which installed distribute and pip in my virtualenv.

基本上我所做的是,用sudo easy_install pip安装pip,然后使用sudo pip安装virtualenv,然后创建一个环境:virtualenv my_env(命名为您想要的名称),下面是我所做的:virtualenv——发布my_env;在我的virtualenv中安装了分布式和pip。

Again, follow the instruction on the virtualenv page.

同样,遵循virtualenv页面的指示。

Kind of a hassle, coming from Ruby ;P

有点麻烦,来自Ruby;P。

#14


0  

Here is a nice way to install virtualenvwrapper(update of this).

Download virtualenv-1.11.4 (you can find latest at here), Unzip it, open terminal

下载virtualenv-1.11.4(您可以在这里找到最新的),解压它,打开终端。

# Create a bootstrapenv and activate it:
$ cd ~
$ python <path to unzipped folder>/virtualenv.py bootstrapenv
$ source bootstrapenv/bin/activate

# Install virtualenvwrapper:
$ pip install virtualenvwrapper
$ mkdir -p ~/bootstrapenv/Envs

# append it to file `.bashrc`
$ vi ~/.bashrc
  source ~/bootstrapenv/bin/activate
  export WORKON_HOME=~/bootstrapenv/Envs
  source ~/bootstrapenv/bin/virtualenvwrapper.sh

# run it now.
$ source ~/.bashrc

That is it, now you can use mkvirtualenv env1, lsvirtualenv ..etc

就是这样,现在你可以使用mkvirtualenv env1, lsvirtualenv .等等。

Note: you can delete virtualenv-1.11.4 and virtualenv-1.11.4.zip from Downloads folders.

注意:您可以删除virtualenv-1.11.4和virtualenv-1.11.4。zip下载文件夹。

#15


0  

The good news is if you have installed python3.4, pyvenv is already been installed. So, Just

好消息是,如果已经安装了python3.4, pyvenv已经安装好了。所以,就

pyvenv project_dir
source project_dir/bin/activate
python --version   
python 3.4.*

Now in this virtual env, you can use pip to install modules for this project.

现在,在这个虚拟环境中,您可以使用pip来为这个项目安装模块。

Leave this virtual env , just

让这个虚拟的env。

deactivate

#1


169  

You can do this without installing anything into python itself.

您可以在不将任何东西安装到python本身的情况下做到这一点。

You don't need sudo or any privileges.

您不需要sudo或任何特权。

You don't need to edit any files.

您不需要编辑任何文件。

Install virtualenv into a bootstrap virtual environment. Use the that virtual environment to create more. Since virtualenv ships with pip and distribute, you get everything from one install.

将virtualenv安装到引导虚拟环境中。使用虚拟环境创建更多。由于virtualenv船舶具有pip和分布,您可以从一个安装中得到所有东西。

  1. Download virtualenv:
  2. 下载virtualenv: http://pypi.python.org/pypi/virtualenv https://pypi.python.org/packages/source/v/virtualenv/virtualenv/virtualenv/virtualenv/virtualenv/virtualenv/virtualenv/virtualenv/virtualenv/virtualenv/virtualenv/virtualenv/virtualenv/virtualenv/virtualenv/virtualenv/virtualenv/virtualenv/virtualenv/virtualenv/
  3. Unpack the source tarball
  4. 解压源代码tarball
  5. Use the unpacked tarball to create a clean virtual environment. This virtual environment will be used to "bootstrap" others. All of your virtual environments will automatically contain pip and distribute.
  6. 使用未打包的tarball创建一个干净的虚拟环境。这个虚拟环境将被用来“引导”其他人。所有虚拟环境将自动包含pip和分发。
  7. Using pip, install virtualenv into that bootstrap environment.
  8. 使用pip,将virtualenv安装到引导环境中。
  9. Use that bootstrap environment to create more!
  10. 使用引导环境创建更多!

Here is an example in bash:

下面是bash中的一个示例:

# Select current version of virtualenv:
VERSION=12.0.7
# Name your first "bootstrap" environment:
INITIAL_ENV=bootstrap
# Set to whatever python interpreter you want for your first environment:
PYTHON=$(which python)
URL_BASE=https://pypi.python.org/packages/source/v/virtualenv

# --- Real work starts here ---
curl -O $URL_BASE/virtualenv-$VERSION.tar.gz
tar xzf virtualenv-$VERSION.tar.gz
# Create the first "bootstrap" environment.
$PYTHON virtualenv-$VERSION/virtualenv.py $INITIAL_ENV
# Don't need this anymore.
rm -rf virtualenv-$VERSION
# Install virtualenv into the environment.
$INITIAL_ENV/bin/pip install virtualenv-$VERSION.tar.gz

Now you can use your "bootstrap" environment to create more:

现在您可以使用您的“引导”环境来创建更多:

# Create a second environment from the first:
$INITIAL_ENV/bin/virtualenv py-env1
# Create more:
$INITIAL_ENV/bin/virtualenv py-env2

Go nuts!

发疯!

Note

This assumes you are not using a really old version of virtualenv. Old versions required the flags --no-site-packges (and depending on the version of Python, --distribute). Now you can create your bootstrap environment with just python virtualenv.py path-to-bootstrap or python3 virtualenv.py path-to-bootstrap.

这假设您没有使用真正的老版本的virtualenv。旧版本需要标记——无站点包(并依赖于Python的版本——分发)。现在您可以使用python virtualenv创建引导环境。py路径到bootstrap或python3 virtualenv。py path-to-bootstrap。

#2


20  

I think Glyph means do something like this:

我认为雕文的意思是这样的:

  1. Create a directory ~/.local, if it doesn't already exist.
  2. 创建一个目录~ /。当地,如果它还不存在的话。
  3. In your ~/.bashrc, ensure that ~/.local/bin is on PATH and that ~/.local is on PYTHONPATH.
  4. 在~ /。bashrc,(确保~ /。本地/bin是在路径上的~/。当地PYTHONPATH环境。
  5. Create a file ~/.pydistutils.cfg which contains

    创建一个文件~ / .pydistutils。cfg包含

    [install]
    prefix=~/.local
    

    It's a standard ConfigParser-format file.

    它是一个标准的configparser格式文件。

  6. Download distribute_setup.py and run python distribute_setup.py (no sudo). If it complains about a non-existing site-packages directory, create it manually:

    下载distribute_setup。py并运行python发行版。py(无sudo)。如果它抱怨一个不存在的站点包目录,请手动创建它:

    mkdir -p ~/.local/lib/python2.7/site-packages/

    mkdir - p ~ /。local / lib / python2.7 /网站/

  7. Run which easy_install to verify that it's coming from ~/.local/bin

    运行easy_install来验证它来自~/.local/bin。

  8. Run pip install virtualenv
  9. pip安装virtualenv运行
  10. Run pip install virtualenvwrapper
  11. 运行pip安装virtualenvwrapper
  12. Create a virtual env containing folder, say ~/.virtualenvs
  13. 创建一个虚拟的env文件夹,例如~/.virtualenv。
  14. In ~/.bashrc add

    在~ /。bashrc添加(

    export WORKON_HOME
    source ~/.local/bin/virtualenvwrapper.sh
    

That's it, no use of sudo at all and your Python environment is in ~/.local, completely separate from the OS's Python. Disclaimer: Not sure how compatible virtualenvwrapper is in this scenario - I couldn't test it on my system :-)

就是这样,不使用sudo,而Python环境在~/中。本地的,完全独立于操作系统的Python。免责声明:不确定在这个场景中如何兼容virtualenvwrapper——我无法在我的系统上测试它:-)

#3


16  

If you follow the steps advised in several tutorials I linked in this answer, you can get the desired effect without the somewhat complicated "manual" steps in Walker's and Vinay's answers. If you're on Ubuntu:

如果您按照我在这个答案中链接的几个教程中所建议的步骤,您可以获得想要的效果,而不需要在Walker和Vinay的回答中使用一些复杂的“手动”步骤。如果你在Ubuntu上:

sudo apt-get install python-pip python-dev

The equivalent is achieved in OS X by using homebrew to install python (more details here).

通过使用homebrew来安装python(这里有更多的细节),可以在OS X中实现等效。

brew install python

With pip installed, you can use it to get the remaining packages (you can omit sudo in OS X, as you're using your local python installation).

安装了pip后,您可以使用它来获得剩余的包(在OS X中可以省略sudo,因为您正在使用本地python安装)。

sudo pip install virtualenvwrapper

(these are the only packages you need installed globally and I doubt that it will * with anything system-level from the OS. If you want to be super-safe, you can keep the distro's versions sudo apt-get install virtualenvwrapper)

(这是您在全球唯一需要安装的软件包,我怀疑它是否会与系统级别的操作系统发生冲突。如果您想要超级安全,您可以保留发行版的sudo apt-get安装virtualenvwrapper)

Note: in Ubuntu 14.04 I receive some errors with pip install, so I use pip3 install virtualenv virtualenvwrapper and add VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 to my .bashrc/.zshrc file.

注意:在Ubuntu 14.04中,我通过pip安装得到一些错误,所以我使用pip3安装virtualenv virtualenvwrapper,并将VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3添加到my .bashrc/。zshrc中。

You then append to your .bashrc file

然后附加到.bashrc文件。

export WORKON_HOME
source /usr/local/bin/virtualenvwrapper.sh

and source it

和源

. ~/.bashrc

This is basically it. Now the only decision is whether you want to create a virtualenv to include system-level packages

这基本上是。现在唯一的决定是您是否想要创建一个virtualenv来包含系统级的包。

mkvirtualenv --system-site-packages foo

where your existing system packages don't have to be reinstalled, they are symlinked to the system interpreter's versions. Note: you can still install new packages and upgrade existing included-from-system packages without sudo - I tested it and it works without any disruptions of the system interpreter.

在不需要重新安装现有系统包的地方,它们与系统解释器的版本是一致的。注意:您仍然可以安装新的包,并升级现有的包含自系统包的包,而不使用sudo——我测试了它,并且在系统解释器没有中断的情况下运行。

kermit@hocus-pocus:~$ sudo apt-get install python-pandas
kermit@hocus-pocus:~$ mkvirtualenv --system-site-packages s
(s)kermit@hocus-pocus:~$ pip install --upgrade pandas
(s)kermit@hocus-pocus:~$ python -c "import pandas; print(pandas.__version__)"
0.10.1
(s)kermit@hocus-pocus:~$ deactivate
kermit@hocus-pocus:~$ python -c "import pandas; print(pandas.__version__)"
0.8.0

The alternative, if you want a completely separated environment, is

另一种选择,如果你想要一个完全分离的环境,是。

mkvirtualenv --no-site-packages bar

or given that this is the default option, simply

或者假设这是默认选项。

mkvirtualenv bar

The result is that you have a new virtualenv where you can freely and sudolessly install your favourite packages

其结果是,您有了一个新的virtualenv,您可以在其中*地、毫无疑问地安装您最喜欢的软件包。

pip install flask

#4


12  

Python 3.4 onward

Python 3.3 adds the venv module, and Python 3.4 adds the ensurepip module. This makes bootstrapping pip as easy as:

Python 3.3添加了venv模块,而Python 3.4添加了ensurepip模块。这使得bootstrapping pip容易:

python -m ensurepip

python - m ensurepip

Perhaps preceded by a call to venv to do so inside a virtual environment.

在虚拟环境中,可能会有一个调用venv来这样做。

Guaranteed pip is described in PEP 453.

在PEP 453中描述了有保证的pip。

#5


10  

On Ubuntu:

在Ubuntu上:

sudo apt-get install python-virtualenv

sudo apt-get安装python-virtualenv

The package python-pip is a dependency, so it will be installed as well.

包python-pip是一个依赖项,因此它也将被安装。

#6


5  

Update: As of July 2013 this project is no longer maintained. The author suggests using pyenv. (pyenv does not have built-in support for virtualenv, but plays nice with it.)

更新:截至2013年7月,该项目已不再维护。作者建议使用pyenv。(pyenv没有内置对virtualenv的支持,但是对它很好。)

Pythonbrew is a version manager for python and comes with support for virtualenv.

Pythonbrew是python的版本管理器,它支持virtualenv。

After installing pythonbrew and a python-version using venvs is really easy:

在安装了pythonbrew和python版本后,使用venvs真的很简单:

# Initializes the virtualenv 
pythonbrew venv init

# Create a virtual/sandboxed environment 
pythonbrew venv create mycoolbundle  

# Use it 
pythonbrew venv use mycoolbundle

#7


4  

I made this procedure for us to use at work.

我做这个程序是为了让我们在工作中使用。

cd ~
curl -s https://pypi.python.org/packages/source/p/pip/pip-1.3.1.tar.gz | tar xvz
cd pip-1.3.1
python setup.py install --user
cd ~
rm -rf pip-1.3.1

$HOME/.local/bin/pip install --user --upgrade pip distribute virtualenvwrapper

# Might want these three in your .bashrc
export PATH=$PATH:$HOME/.local/bin
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS="--distribute"
source $HOME/.local/bin/virtualenvwrapper.sh

mkvirtualenv mypy
workon mypy
pip install --upgrade distribute
pip install pudb # Or whatever other nice package you might want.

Key points for the security minded:

安全意识的要点:

  1. curl does ssl validation. wget doesn't.
  2. 旋度是ssl验证。wget没有。
  3. Starting from pip 1.3.1, pip also does ssl validation.
  4. 从pip 1.3.1开始,pip也进行了ssl验证。
  5. Fewer users can upload the pypi tarball than a github tarball.
  6. 可以上传pypi tarball的用户要少于github tarball。

#8


3  

There is no problem to do sudo python setup.py install, if you're sure it's what you want to do.

做sudo python设置是没有问题的。py安装,如果你确定它是你想做的。

The difference is that it will use the site-packages directory of your OS as a destination for .py files to be copied.

不同之处在于,它将使用您的OS的站点包目录作为.py文件的目的地。

so, if you want pip to be accessible os wide, that's probably the way to go. I do not say that others way are bad, but this is probably fair enough.

所以,如果你想让pip可以访问操作系统,那很可能就是这样。我并不是说其他人的方式不好,但这可能是公平的。

#9


3  

Install ActivePython. It includes pip, virtualenv and Distribute.

安装ActivePython。它包括pip、virtualenv和分发。

#10


2  

I came across the same problem recently. I’m becoming more partial to the “always use a virtualenv” mindset, so my problem was to install virtualenv with pip without installing distribute to my global or user site-packages directory. To do this, I manually downloaded distribute, pip and virtualenv, and for each one I ran “python setup.py install --prefix ~/.local/python-private” (with a temporary setting of PYTHONPATH=~/.local/python-private) so that setup scripts were able to find distribute). I’ve moved the virtualenv script to another directory I have on my PATH and edited it so that the distribute and virtualenv modules can be found on sys.path. Tada: I did not install anything to /usr, /usr/local or my user site-packages dir, but I can run virtualenv anywhere, and in that virtualenv I get pip.

我最近遇到了同样的问题。我越来越倾向于“总是使用虚拟环境”的心态,所以我的问题是在没有安装到我的全局或用户站点包目录的情况下,用pip安装virtualenv。为此,我手动下载了分发、pip和virtualenv,并为每个我运行的“python设置”。py安装——prefix ~ /。本地/python-私有”(使用python的临时设置=~/.local/python-private),以便设置脚本能够找到分布)。我将virtualenv脚本移动到路径上的另一个目录,并编辑它,以便在sys.path中找到分发和virtualenv模块。Tada:我没有安装任何东西到/usr, /usr/local或我的用户site-package dir,但是我可以在任何地方运行virtualenv,在这个virtualenv中我得到pip。

#11


2  

  • You can do this without installing anything into python itself.

    您可以在不将任何东西安装到python本身的情况下做到这一点。

  • You don't need sudo or any privileges.

    您不需要sudo或任何特权。

  • You don't need to find the latest version of a virtualenv tar file

    您不需要找到virtualenv tar文件的最新版本。

  • You don't need to edit version info in a bash script to keep things up-to-date.

    您不需要在bash脚本中编辑版本信息以保持更新。

  • You don't need curl/wget or tar installed, nor pip or easy_install

    您不需要安装curl/wget或tar,也不需要pip或easy_install。

Save the following to /tmp/initvenv.py:

保存如下/tmp/initvenv.py:

from __future__ import print_function

import os, sys, shutil, tempfile, subprocess, tarfile, hashlib

try:
    from urllib2 import urlopen
except ImportError:
    from urllib.request import urlopen

tmp_dir = tempfile.mkdtemp(prefix='initvenv_')
try:
    # read the latest version from PyPI
    f = urlopen("https://pypi.python.org/pypi/virtualenv/")
    # retrieve the .tar.gz file
    for line in f.read().splitlines():
        if isinstance(line, bytes):
            line = line.decode('utf-8')
        if 'tar.gz<' not in line:
            continue
        for url in line.split('"'):
            if url.startswith('http'):
                url, md5 = url.split('#')
                assert md5.startswith('md5=')
                md5 = md5[4:]
                break
        else:
            continue
        break
    else:
        print('tar.gz not found')
        sys.exit(1)
    file_name = url.rsplit('/', 1)[1]
    print(file_name)
    # url = "https://pypi.python.org/packages/source/v/virtualenv/" + file_name
    os.chdir(tmp_dir)
    with open(file_name, 'wb') as fp:
        data = urlopen(url).read()
        data_md5 = hashlib.md5(data).hexdigest()
        if md5 != data_md5:
            print('md5 not correct')
            print(md5)
            print(data_md5)
            sys.exit(1)
        fp.write(data)
    tar = tarfile.open(file_name)
    tar.extractall()
    tar.close()
    os.chdir(file_name.replace('.tar.gz', ''))
    print(subprocess.check_output([sys.executable, 'virtualenv.py'] +
                                  [sys.argv[1]]).decode('utf-8'), end='')
    if len(sys.argv) > 2:
        print(subprocess.check_output([
            os.path.join(sys.argv[1], 'bin', 'pip'), 'install', 'virtualenv'] +

            sys.argv[2:]).decode('utf-8'), end='')
except:
    raise
finally:
    shutil.rmtree(tmp_dir)  # always clean up

and use it as

和使用它

python_binary_to_use_in_venv /tmp/initvenv.py your_venv_name [optional packages]

e.g. (if you really need the distribute compatibility layer for setuptools)

(如果你真的需要setuptools的分发兼容层)

python /tmp/initvenv.py venv distribute

Please note that, with older python versions, this might give you InsecurePlatformWarnings¹.

请注意,与旧的python版本,这可能会给你InsecurePlatformWarnings¹。

Once you have your virtualenv (name e.g. venv) you can setup another virtualenv by using the virtualenv just installed:

一旦你有了virtualenv(名字,venv),你就可以通过使用virtualenv来设置另一个virtualenv:

venv/bin/virtualenv venv2

virtualenvwrapper

I recommend taking a look at virtualenvwrapper as well, after a one time setup:

我建议在一个时间设置之后,再看一下virtualenvwrapper:

% /opt/python/2.7.10/bin/python /tmp/initvenv.py venv virtualenvwrapper

and activation (can be done from your login script):

激活(可以从你的登录脚本完成):

% source venv/bin/virtualenvwrapper.sh

you can do things like:

你可以这样做:

% mktmpenv 
New python executable in tmp-17bdc3054a46b2b/bin/python
Installing setuptools, pip, wheel...done.
This is a temporary environment. It will be deleted when you run 'deactivate'.
(tmp-17bdc3054a46b2b)% 

¹ I have not found a way to suppress the warning. It could be solved in pip and/or request, but the developers point to each other as the cause. I got the, often non-realistic, recommendation to upgrade the python version I was using to the latest version. I am sure this would break e.g my Linux Mint 17 install. Fortunately pip caches packages, so the Warning is made only once per package install.

¹我还没有找到一个方法来抑制警告。它可以在pip和/或请求中得到解决,但是开发人员将彼此视为原因。我得到了,通常是不现实的建议,升级我使用的python版本的最新版本。我确信这将打破e。我的Linux Mint 17安装。幸运的是,pip缓存包,因此警告只在每个包安装一次。

#12


2  

I've had various problems (see below) installing upgraded SSL modules, even inside a virtualenv, on top of older OS-provided Python versions, so I now use pyenv.

我遇到过各种各样的问题(见下面),安装升级的SSL模块,甚至在virtualenv中,在旧的os提供的Python版本之上,所以我现在使用pyenv。

pyenv makes it very easy to install a new Python versions and supports virtualenvs. Getting started is much easier than the recipes for virtualenv listed in other answers:

pyenv使安装新的Python版本和支持virtualenv变得非常容易。开始比在其他答案中列出的virtualenv的食谱要容易得多:

  • On Mac, type brew install pyenv and on Linux, use pyenv-installer
  • 在Mac电脑上,类型brew安装pyenv和Linux,使用pyenv安装程序。
  • this gets you built-in virtualenv support as well as Python version switching (if required)
  • 这将为您提供内置的virtualenv支持以及Python版本切换(如果需要)
  • works well with Python 2 or 3, can have many versions installed at once
  • 与Python 2或3兼容,可以同时安装多个版本。

This works very well to insulate the "new Python" version and virtualenv from system Python. Because you can easily use a more recent Python (post 2.7.9), the SSL modules are already upgraded, and of course like any modern virtualenv setup you are insulated from the system Python modules.

这可以很好地隔离“新Python”版本和来自系统Python的virtualenv。因为您可以很容易地使用最近的Python (post 2.7.9), SSL模块已经升级了,当然,就像任何现代的virtualenv设置一样,您可以与系统Python模块隔离。

A couple of nice tutorials:

一些不错的教程:

  • Using pyenv and virtualenv - when selecting a Python version, it's easier to use pyenv global 3.6.1 (global to current user) or pyenv local 2.7.13 (local to current directory).
  • 使用pyenv和virtualenv——在选择Python版本时,更容易使用pyenv全局3.6.1(全局对当前用户)或pyenv本地2.7.13(本地到当前目录)。
  • pyenv basics and use with virtualenv
  • pyenv基础和与virtualenv的使用。

The pyenv-virtualenv plugin is now built in - type pyenv commands | grep virtualenv to check. I wouldn't use the pyenv-virtualenvwrapper plugin to start with - see how you get on with pyenv-virtualenv which is more integrated into pyenv, as this covers most of what virtualenvwrapper does.

pyenv-virtualenv插件现在内置了- type pyenv命令| grep virtualenv检查。我不会使用pyenv-virtualenvwrapper插件——看看你如何使用pyenv-virtualenv,它更集成到pyenv中,因为它涵盖了virtualenvwrapper所做的大部分工作。

pyenv is modelled on rbenv (a good tool for Ruby version switching) and its only dependency is bash.

pyenv是模仿rbenv (Ruby版本转换的一个好工具),它唯一的依赖是bash。

  • pyenv is unrelated to the very similarly named pyvenv - that is a virtualenv equivalent that's part of recent Python 3 versions, and doesn't handle Python version switching
  • pyenv与非常类似的pyvenv无关——它是最近Python 3版本的一部分,并且不处理Python版本转换。

Caveats

Two warnings about pyenv:

两个警告pyenv:

  1. It only works from a bash or similar shell - or more specifically, the pyenv-virtualenv plugin doesn't like dash, which is /bin/sh on Ubuntu or Debian.
  2. 它只适用于bash或类似的shell——或者更确切地说,pyenv-virtualenv插件不喜欢dash,它是/bin/sh在Ubuntu或Debian上。
  3. It must be run from an interactive login shell (e.g. bash --login using a terminal), which is not always easy to achieve with automation tools such as Ansible.
  4. 它必须从交互式的登录shell(例如bash——使用终端登录)中运行,使用自动化工具(如anable)并不总是很容易实现。

Hence pyenv is best for interactive use, and less good for scripting servers.

因此,pyenv最适合于交互式使用,而对脚本服务器则不太好。

SSL module problems

One reason to use pyenv is that there are often problems with upgrading Python SSL modules when using older system-provided Python versions:

使用pyenv的一个原因是,在使用旧的系统提供的Python版本时,升级Python SSL模块经常会出现问题:

  • Ubuntu 14.04 includes Python 2.7.6 which requires considerable effort to upgrade it to the right SSL modules so it handles SNI (server name indication) as client. (I wrote some Ansible scripts for this which was quite painful and still broke in some cases.)
  • ubuntu14.04包含Python 2.7.6,需要相当大的努力才能将其升级到正确的SSL模块,因此它可以作为客户机处理SNI(服务器名表示)。(我写了一些很痛苦的脚本,在某些情况下还会被打破。)
  • upgraded SSL modules will be more important as Python.org sites move to TLS 1.2 only during 2017 and 2018.
  • 升级的SSL模块将更重要,因为Python.org网站只在2017年和2018年迁移到TLS 1.2。

#13


0  

There are good instructions on the Virtualenv official site. https://pypi.python.org/pypi/virtualenv

在Virtualenv官方网站上有很好的说明。https://pypi.python.org/pypi/virtualenv

Basically what I did, is install pip with sudo easy_install pip, then used sudo pip install virtualenv then created an environment with: virtualenv my_env (name it what you want), following that I did: virtualenv --distribute my_env; which installed distribute and pip in my virtualenv.

基本上我所做的是,用sudo easy_install pip安装pip,然后使用sudo pip安装virtualenv,然后创建一个环境:virtualenv my_env(命名为您想要的名称),下面是我所做的:virtualenv——发布my_env;在我的virtualenv中安装了分布式和pip。

Again, follow the instruction on the virtualenv page.

同样,遵循virtualenv页面的指示。

Kind of a hassle, coming from Ruby ;P

有点麻烦,来自Ruby;P。

#14


0  

Here is a nice way to install virtualenvwrapper(update of this).

Download virtualenv-1.11.4 (you can find latest at here), Unzip it, open terminal

下载virtualenv-1.11.4(您可以在这里找到最新的),解压它,打开终端。

# Create a bootstrapenv and activate it:
$ cd ~
$ python <path to unzipped folder>/virtualenv.py bootstrapenv
$ source bootstrapenv/bin/activate

# Install virtualenvwrapper:
$ pip install virtualenvwrapper
$ mkdir -p ~/bootstrapenv/Envs

# append it to file `.bashrc`
$ vi ~/.bashrc
  source ~/bootstrapenv/bin/activate
  export WORKON_HOME=~/bootstrapenv/Envs
  source ~/bootstrapenv/bin/virtualenvwrapper.sh

# run it now.
$ source ~/.bashrc

That is it, now you can use mkvirtualenv env1, lsvirtualenv ..etc

就是这样,现在你可以使用mkvirtualenv env1, lsvirtualenv .等等。

Note: you can delete virtualenv-1.11.4 and virtualenv-1.11.4.zip from Downloads folders.

注意:您可以删除virtualenv-1.11.4和virtualenv-1.11.4。zip下载文件夹。

#15


0  

The good news is if you have installed python3.4, pyvenv is already been installed. So, Just

好消息是,如果已经安装了python3.4, pyvenv已经安装好了。所以,就

pyvenv project_dir
source project_dir/bin/activate
python --version   
python 3.4.*

Now in this virtual env, you can use pip to install modules for this project.

现在,在这个虚拟环境中,您可以使用pip来为这个项目安装模块。

Leave this virtual env , just

让这个虚拟的env。

deactivate