how to use python virtualenv on windows

转自:http://www.tylerbutler.com/2012/05/how-to-install-python-pip-and-virtualenv-on-windows-with-powershell/

不错的blog,很细!!!


How To Install Python, pip, and virtualenv on Windows with PowerShell

Updated May 12, 2013

If you do any Python development, you’ll probably run into an awful lot of package installation instructionsthat read:

To install, use pip:

pip install engineer

Now, that’s all fine and dandy, but what is pip? And what is this virtualenv thing people keep telling me I should use?

If you’re new to Python, getting up and running with pip and virtualenv can be a challenge, especially on Windows. Many guides I’ve seen out there assume either a) you’re working on Linux or UNIX or b) you already have pip/setuptools installed, or you know how to install packages and manage virtualenv. Heck, when I was learning this I didn’t even know what pip was! Having gone through this process several times now, I decided to write it all down from the beginning in the hopes that it’ll be useful to someone in the future.

Before We Start

A brief note before we start… To make sure we’re all on the same page, pip is a Python package installer. It integrates with PyPI, the Python Package Index, and lets you download and install a package from the package index without manually downloading the package, uncompressing it, runningpython setup.py install etc. Pip makes installing libraries for your Python environment a breeze, and when you start developing your own packages it provides a way for you to declare dependencies so those dependent packages will get installed automatically as well.

The more Python development you do, though, the more packages you’re going to need. Wouldn’t it be nice if you could install all the packages into a ‘special’ location where they wouldn’t interfere with any other packages? This is where virtualenv comes in. It creates a virtual Python interpreter and isolates any packages installed for that interpreter from others on the system. There are lots of ways this comes in handy; I’ll leave enumerating them as an exercise for the reader, but if you think for a minute you can see why this will come in handy. And if you can’t yet, then give yourself a few weeks of Python development, then come back and look at this post again once you realize you need to use virtualenv.

Finally, there’s a wrapper utility for virtualenv aptly called virtualenvwrapper. This wrapper makes creating new virtual environments and switching between them really straightforward. Unfortunately, it relies on aUNIX shell, which is kind of a pain on Windows. Luckily there’s a PowerShell clone of the wrapper that works wonderfully and gives us Windows users the same kind of awesomeness that we’ve come to expect from PowerShell.

So with the definitions out of the way, let’s get started…

Ensure You Can Execute PowerShell Scripts

For the most part, this guide assumes you’ve actually used PowerShell a few times and know how to run scripts. If that’s not the case, though, then the very first thing you’ll want to do is enable scripts to run on your system using the Set-ExecutionPolicy command. There’s a great article on TechNet that covers this in detail, so I won’t go into detail here. You can also skip this step for now if you want. Just read that article if you run into the following error message at any point:

...cannot be loaded because the execution of scripts is disabled on
this system. Please see "get-help about_signing" for more details.

Get Python

First things first – get Python! You can get the Python 2.7.3 (the current Python 2.x version as of this writing) 32-bit installer from http://python.org/download/. There is a 64-bit version of Python as well, but I have personally found it to be more hassle than it’s worth. Some packages won’t have 64-bit versions available, and I personally haven’t found any need for the 64-bit version in any project I’ve worked on. Feel free to go with the 64-bit version if you’d like, but this guide assumes you’re using the 32-bit one.

Once you’ve installed Python, open up a PowerShell window and type python and press enter. You should see something like this:

PS C:\> python
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

You might also get an error, however, saying “The term ‘foo’ is not recognized as the name of a cmdlet, function, script file, or operable program.” This is because the Python installer, for whatever reason, does not automatically add the installation directory to your path. (Although apparently it sometimes does. In some cases I have not had to update my path manually.) You’ll need to update your path to include the Python install directory (usually something like C:\Python27\). There are a number of ways to do that; the Using Python on Windows documentation includes more details if needed. You’ll also want to add the Scriptssubdirectory directly (so C:\Python27\Scripts), since that’s where pip will end up being installed.

Once your path is updated, restart PowerShell to ensure the new path is loaded and try typing python again. You should be good to go!

Get Distribute

Distribute is a fork of the Setuptools project “intended to replace Setuptools as the standard method for working with Python module distributions.” It is a prerequisite for pip. Luckily, unlike Setuptools, Distribute is pretty easy to install on Windows.

You need to download the distribute_setup.py file, save it locally, then run it using Python. For example, after saving the distribute_setup.py script locally, open up PowerShell, navigate to where you downloaded the file, and type:

PS C:\> python distribute_setup.py

Get Pip

The easiest way to install pip is to download the get-pip.py script and run it using Python, much like you did with Distribute.

PS C:\> python get-pip.py

There are other ways to get pip, but this is the easiest way I have found. There are more details on this at the pip website. To check if everything is working, just type pip at the command line:

PS C:\> pip
Usage: pip-script.py COMMAND [OPTIONS]
pip-script.py: error: You must give a command (use "pip help" to see a list of commands)

If you get another “command is not recognized” error, check that C:\Python27\Scripts is on your path.

Install virtualenv and virtualenvwrapper-powershell

Pip should now be installed, so type the following commands to get virtualenv and the PowerShell virtualenvwrapper installed:

PS C:\> pip install virtualenv
PS C:\> pip install virtualenvwrapper-powershell

Now you need to import the wrapper module in PowerShell, so type Import-Module virtualenvwrapper. You will probably get an error saying something like this:

Virtualenvwrapper: Virtual environments directory 
'C:\Users\tylerbu/.virtualenvs' does not exist. Create it or 
set $env:WORKON_HOME to an existing directory.

Well, at least you know you’re on the right track! Do exactly what the message says: create the missing directory.

mkdir '~\.virtualenvs'

You might also want to change the location to store your virtual environments. To do that, set the$env:WORKON_HOME variable to wherever you want to store them. I generally stick with the default of~\.virtualenvs though. Whatever you do, remember this location; it will come in handy.

Now try to import the module again. Success! Now you have access to a bunch of virtualenv management commands directly in PowerShell. To see all of them, you can type:

PS C:\> Get-Command *virtualenv*

CommandType     Name                          Definition
-----------     ----                          ----------
Function        CDIntoVirtualEnvironment      ...
Alias           cdvirtualenv                  CDIntoVirtualEnvironment
Function        Copy-VirtualEnvironment       ...
Alias           cpvirtualenv                  Copy-VirtualEnvironment
Function        Get-VirtualEnvironment        ...
Alias           mkvirtualenv                  New-VirtualEnvironment
Function        New-VirtualEnvironment        ...
Function        New-VirtualEnvProject         ...
Function        Remove-VirtualEnvironment     ...
Alias           rmvirtualenv                  Remove-VirtualEnvironment
Function        Set-VirtualEnvironment        ...
Function        Set-VirtualEnvProject         ...
Function        VerifyVirtualEnv              ...
Application     virtualenv.exe                C:\Python27\Scripts\virtualenv.exe
Application     virtualenv.exe.manifest       C:\Python27\Scripts\virtualenv.exe.manifest
Application     virtualenv-script.py          C:\Python27\Scripts\virtualenv-script.py

You’ll see that there are a bunch of nice PowerShell style cmdlets, like New-VirtualEnvironment, but there are also aliases set up mapping those cmdlets to commands you might be more familiar with, likemkvirtualenv. Of course you also get regular PowerShell tab completion for these cmdlets and aliases.

Managing virtualenvs

Now that we have virtualenv installed, let’s make a new virtualenv:

New-VirtualEnvironment engineer --no-site-packages

Replace engineer with whatever you want to call your virtualenv. I usually name it after the project I plan to use that virtualenv for, but whatever you want works. The --no-site-packages argument is also optional. By default, virtualenv copies whatever python packages you have installed in your system python environment to a new one. I dislike this behavior since it can unnecessarily pollute a new virtualenv, so I have the habit of disabling it by passing this argument.

Note: The default virtualenv behavior in recent versions is to not install site packages, so the --no-site-packagesargument may be superfluous if you’re using recent versions.

After the command completes, you should see a PowerShell prompt that looks like this:

(engineer)PS C:\>

The (engineer) prepended to your prompt reminds you that you’re currently working within that virtualenv. If you type workon now you should see the available virtualenvs, and if you typeworkon name_of_another_virtualenv you’ll flip to that environment.

PS C:\> workon

PathInfo         Name          PathToScripts                 PathToSitePackages
--------         ----          -------------                 ------------------
engineer         engineer      C:\Users\tylerbu\.virtuale... C:\Users\tylerbu\.virtuale...

Install Packages with pip

Now that your virtual environments are configured, you can install packages into them using pip. Open a PowerShell prompt, type workon name_of_virtualenv and then type pip install package_name. There are also a couple of additional pip commands that might be useful to know. If you have a project with lots of package requirements, it might have come with (or you might have written) a requirements file (often calledrequirements.txt). To have pip load all of the packages in that file, type:

PS C:\> pip install -r path_to_requirements_file

Also, you might have downloaded a package’s source manually that has a setup.py file in it. You can have pip install that for you by typing:

PS C:\> pip install -e path_to_source

The -e option can also check out source directly from a Mercurial, Git, Subversion, or Bazaar repository and install a package from there.

Automatically Importing the virtualenvwrapper PowerShell Module

You might notice at some point – probably once you open a new PowerShell prompt – that you can no longer use the workon and New-VirtualEnvironment commands. Well, silly, you forgot to import thevirtualenvwrapper module! Now, you could just import it and move on with your life, but that’s going to get annoying really quickly, so you can configure your PowerShell profile so that the module is loaded every time you open up a PowerShell window. First, though, you’re going to need to find your profile. To make matters a bit more confusing, there are actually several profiles that PowerShell uses. But only one or two of them are really relevant to us. To see all the profiles available to you, type:

PS C:\> $profile | Format-List * -Force

AllUsersAllHosts       : C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
AllUsersCurrentHost    : C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts    : D:\Users\tyler\Documents\WindowsPowerShell\profile.ps1
CurrentUserCurrentHost : D:\Users\tyler\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
Length                 : 77

Looks like there are four available profile scripts, and based on their names, they all have different scopes. In our case, we probably want the CurrentUserAllHosts profile, since that will execute for us in every PowerShell instance. If you navigate to the location listed, there might not be a file there to edit. In that case, the following command will create a file there in the right format:

PS C:\> New-Item -Path $Profile.CurrentUserAllHosts -Type file -Force

Or you could just create a file in your favorite text editor and save it in that location (with the correct name, of course).

In that file, put the command you used to import the virtualenvwrapper modules earlier

Import-Module virtualenvwrapper

It’s worth noting that this file is just a regular PowerShell script, so you can put other stuff in it too, such as aliases you like to set up, etc. Anyway, once that’s done, save the file and open a new PowerShell window. Now the workon command and other virtualenv cmdlets should start functioning.

Configuring Your IDE

There is one final step to getting everything really ready for developing Python projects – setting up your IDEto use the appropriate virtualenv for your project. There are several different IDEs out there or you could just rock Notepad++. I personally like PyCharm a lot though.

If you are using PyCharm, version 2.5+ has built-in support for virtualenv. You can create virtual environmentsdirectly in PyCharm or you can import ones you created earlier using virtualenvwrapper. Personally I prefer the latter since virtualenvwrapper doesn’t pick up the environments created by PyCharm (so they don’t show up when you use the workon command, among another things).

Anyway, if you want to use an existing virtualenv, you’ll need to tell PyCharm about it. The PyCharm support site has details, but the key thing to know is that you need to point it to the python.exe inside yourvirtualenv‘s Scripts directory. In my case, the full path isC:\Users\tyler\.virtualenvs\engineer\Scripts\python.exe.

After all of that’s done you should be good to go! You can pop open a PowerShell window and create/switch to virtualenvs as needed and install packages using pip. At this point you should have most of what you need to follow the installation instructions for most Python packages (except those that require C extension compilation, but that’s a topic for another post).

Last updated May 12, 2013. If you notice any errors or missing/out-of-date information in this guide, please let me know at: tyler AT tylerbutler DOT com.


PythonVirtualenv是一个用于创建和管理独立Python环境的工具。它可以让你在同一台机器上拥有多个独立的Python环境,每个环境可以有自己的Python版本和安装的软件包,而不会相互影响。 使用Virtualenv可以帮助你解决不同项目之间的依赖冲突问题。例如,你可以在一个项目中使用Python 2.7,而在另一个项目中使用Python 3.8,而不会相互干扰。 要使用Virtualenv,你可以先安装它,然后通过命令行创建一个新的虚拟环境。以下是一些使用Virtualenv的基本步骤: 1. 安装Virtualenv:在命令行中运行`pip install virtualenv`来安装Virtualenv。 2. 创建虚拟环境:在命令行中进入你想要创建虚拟环境的目录,然后运行`virtualenv <环境名称>`来创建一个新的虚拟环境。例如,`virtualenv myenv`将创建一个名为"myenv"的虚拟环境。 3. 激活虚拟环境:在命令行中运行`source <环境名称>/bin/activate`(对于Windows系统,运行`<环境名称>\Scripts\activate`)来激活虚拟环境。一旦激活,你将在命令行的提示符前看到环境名称。 4. 安装软件包:在虚拟环境中,你可以使用`pip`来安装所需的Python软件包,就像在全局Python环境中一样。这些软件包将被安装在虚拟环境的文件夹中,而不会影响全局Python环境。 5. 退出虚拟环境:要退出虚拟环境,只需在命令行中运行`deactivate`即可。 通过使用Virtualenv,你可以轻松地管理和隔离不同项目的Python环境和依赖。这对于开发和部署应用程序时非常有用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值