virtualenv
I was searching for a nice virtualenv quickstart guide today, but couldn’t find one that I liked. Either they were outdated and still relied on easy_install, or they were too complicated. So here’s my own.
我今天在寻找一个不错的virtualenv快速入门指南,但是找不到我喜欢的指南。 它们要么已经过时,仍然依赖easy_install ,要么它们太复杂了。 所以这是我自己的。
为什么要使用virtualenv? (Why use virtualenv?)
Virtualenv (http://www.virtualenv.org/) basically provides you with a full Python environment (and/or versions) inside a single folder. This way, you can have multiple Python environments next to each other (usually one per project), each with its own binaries and packages.
Virtualenv( http://www.virtualenv.org/ )基本上在单个文件夹内为您提供了完整的Python环境(和/或版本)。 这样,您可以使多个Python环境彼此相邻(通常每个项目一个),每个环境都有自己的二进制文件和程序包。
I really recommend using virtualenv for all Python projects. Tools like virtualenvwrapper and requirements files make setting up a new virtual environment a breeze.
我真的建议对所有Python项目使用virtualenv。 诸如virtualenvwrapper和需求文件之类的工具使设置新的虚拟环境变得轻而易举。
建立 (Setup)
In case you’re still using easy_install to install Python packages, you should switch to pip immediately:
如果您仍在使用easy_install来安装Python软件包,则应立即切换到pip :
$ sudo easy_install pip
Then install virtualenv itself:
然后安装virtualenv本身:
$ sudo pip install virtualenv
Those are usually the only two Python packages that you should install to your systemwide PYTHONPATH.
这些通常是应该安装到系统级PYTHONPATH的仅有的两个Python软件包。
创建一个virtualenv (Creating a virtualenv)
Now you need to initialize your virtual environment. This can be located anywhere. I’d recommend either creating it into a folder called VIRTUAL inside your project directory, or creating a folder called .virtualenv in your home directory and placing it in there, named like your project.
现在,您需要初始化您的虚拟环境。 它可以位于任何地方。 我建议您将其创建到项目目录中名为VIRTUAL的文件夹中,或者在您的主目录中创建一个名为.virtualenv的文件夹并将其放置在其中,命名为您的项目。
$ virtualenv --no-site-packages VIRTUAL
New python executable in VIRTUAL/bin/python
Installing setuptools............done.
Installing pip...............done.
启用虚拟环境 (Enabling a virtualenv)
To actually work inside a virtualenv, you need to enable it first. This is done by sourcing bin/activate inside your virtualenv folder.
要在virtualenv内部实际工作,您需要先启用它。 这是通过在您的virtualenv文件夹中获取bin / activate来完成的。
$ source VIRTUAL/bin/activate
This step needs to be done each time you start a new bash prompt. Now every time you call a Python-related binary (e.g. python or pip), the version from your virtualenv instead of the system version will be used.
每当您启动新的bash提示时,都需要执行此步骤。 现在,每次您调用与Python相关的二进制文件(例如python或pip )时,都会使用virtualenv中的版本而不是系统版本。
You can also use your virtual python without sourcing the mentioned file first, but then you need to specify the full path to the desired binary (e.g. VIRTUAL/bin/python manage.py runserver). This can be useful for bash scripts.
您也可以使用虚拟python,而无需先采购提到的文件,但是随后您需要指定所需二进制文件的完整路径(例如VIRTUAL / bin / python manage.py runserver )。 这对于bash脚本很有用。
安装软件包,跟踪需求 (Installing packages, tracking requirements)
Installing Python packages is as simple as pip install <packagename> after enabling your virtualenv. When having worked inside a virtualenv for a while, you’ve probably installed a few packages and want to document those dependencies somehow. This is where the pip freeze command and requirements.txt files can and should be used.
启用virtualenv之后,安装Python软件包就像pip install <packagename>一样简单。 在virtualenv中工作了一段时间后,您可能已经安装了一些软件包,并希望以某种方式记录这些依赖项。 在这里可以使用pip Frozen命令和requirements.txt文件。
$ pip install Django
...
$ pip freeze > requirements.txt
$ cat requirements.txt
Django==1.4.1
wsgiref==0.1.2
(Note: The wsgiref package is a part of the Python standard library. Currently it is the only standard library package that includes package metadata, so it is the only standard library package whose presence pip reports.)
( 注意: wsgiref软件包是Python标准库的一部分。当前,它是唯一包含软件包元数据的标准库软件包,因此它是唯一一个存在pip报告的标准库软件包。)
The requirements.txt file is a very good convention, as it allows you or another developer to quickly replicate the environment you’re currently working in. After creating an empty virtualenv, you can simply install all necessary packages with:
requirements.txt文件是一个非常好的约定,因为它允许您或另一个开发人员快速复制当前正在使用的环境。创建空的virtualenv之后,您可以使用以下命令安装所有必需的软件包:
$ pip install -r requirements.txt
For more information, please refer to the pip docs.
有关更多信息,请参考pip docs 。
下一步 (Next steps)
To simplify your life with virtualenv, you should start using virtualenvwrapper, which gives you nice shortcut functions like mkvirtualenv to create a new environment, workon to enable a specific virtual environment, rmvirtualenv to remove an environment and more.
为了简化您使用virtualenv的生活,您应该开始使用virtualenvwrapper ,它为您提供了不错的快捷功能,例如mkvirtualenv来创建新环境, workon来启用特定的虚拟环境, rmvirtualenv来删除环境等等。
This should be enough to get you started. In case some parts of this quickstart guide are difficult to understand or if you have any questions, please leave a comment below.
这应该足以让您入门。 如果本快速入门指南的某些部分难以理解或有任何疑问,请在下面留下评论。
翻译自: https://www.pybloggers.com/2012/09/virtualenv-quickstart-guide/
virtualenv