Pylons  - 讨论区

标题:转:【翻译】The Definitive Guide to Pylons(九)

2010年10月22日 星期五 17:05

Advanced Topics
高级话题

Activating and Deactivating a Virtual Python Environment
激活和关闭虚拟的Python环境

If you are working regularly with the files in a virtualenv virtual Python environment it can become tedious to have to type the path/to/env/bin/ part in front of every script you wish to execute. You can solve this problem by activating the environment you are currently working in. This puts the virtual Python environment executables first in your PATH environment variable so that you can run them without typing the full path to the virtual Python environment.

如果你是经常使用在虚拟的Python环境的virtualenv目录中的文件工作,那么在你想要执行的每个脚本前不得不键入/to/env/bin/路径部分,是非常令人厌烦的。你可以通过激活你所工作的环境来解决这一问题,它将虚拟的Python环境的可执行文件加入PATH环境变量,这样你就可以直接地运行这些可执行文件,而不需要键入到虚拟的Python环境的完整路径。

Here's how to do it on Windows:
以下是在Windows下的做法:

C:\Test>..\env\Scripts\activate.bat
(env) C:\Test>python
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pylons
>>>

Once you've activated a virtual Python environment with activate.bat the scripts in the virtual Python environment take precedence over the normal system ones. In the example above you can see that running python actually runs the version in your virtual Python environment where Pylons is installed, and not the main system Python. The same is true for the other scripts available. Notice how the command prompt changes to show you that you are in a virtual Python environment.

一旦你用activate.bat激活了一个虚拟的Python环境,在虚拟的Python环境中的脚本就能凌驾于正常的系统中的脚本。在上面的例子中,可以看到,运行python时,实际运行的Python的版本是Pylons安装所在的虚拟环境下的Python,而不是主系统的Python。这同样适用于其它可用的脚本。请注意命令提示符的变化,提示你是在一个虚拟的Python环境。

You can deactivate it by typing deactivate.bat and things go back to normal:
你可以通过运行deactivate.bat停用它,回到正常的情况:

(env) C:\Test>..\env\Scripts\deactivate.bat
C:\Test>

On Linux and Mac OS X you active the environment using a Bash script like this:
在Linux和Mac OS X中,你可以使用一个Bash脚本,如下:

$ source ~/env/bin/activate

This script works in Bash and zsh and probably most other Bourne-compatible shells. It will not work on csh or tcsh. If it doesn't work in your non-Bash shell, you'll have to modify it to conform to your shell's syntax. Don't forget the word "source"! That makes it execute in the current shell rather than starting a new process, so that it can modify the current shell's PATH and other variables. The oldest Bourne shells used "." instead of "source".

此脚本工作在Bash和zsh,以及大多数其他兼容Bourne的shell下。它不能在csh或tcsh下工作。如果它不能在不是Bash的shell下工作,你需要修改它以符合你所在的shell的语法。不要忘了“source”这个词!这使得它在在当前的shell执行,而不是开始一个新的进程,因此,它可以修改当前shell的路径和其他变量。最古老的shells使用“.”而不是“source”。

Notice that after a shell is activated that the prompt changes to "(env)$" to remind you which environment you're in. This is handy if you end up working in multiple virtual environments in different windows.

请注意,shell被激活后,提示符会改变 "(env)$" ,提醒你进入了哪个环境,如果你在不同的窗口中使用多个虚拟环境工作,这是很方便的。

If you want to de-activate a shell you would enter this command on Linux and Mac OS X:
在Linux和Mac OS X下,如果您想要去关闭一个shell,输入此命令:

deactivate

The paths will then return to normal and the prompt will disappear.
然后路径将恢复正常,提示消失。

Caution!

There is one potential problem to keep in mind with activate/deactivate. The shell keeps the path of each command in memory so it doesn't have to look it up again when the command is re-executed. This bypasses activate's changes to the environment, so activate/deactivate try to clear this cache but sometimes they fail. The result is you type the command name and get the "wrong" one. To make 100% sure you're getting the right command, run "which python" (or "which easy_install", etc) and it will print the full path of the command it would have executed. If it's the wrong one, execute it once by typing the full path, and it will update the memory cache to use that version of the command.

If in doubt it is probably worth using the executables directly without the activate and deactivate magic.

注意!

关于activate/deactivate,还有一个潜在的问题要牢记。shell不断地在内存中记录每一个命令的路径,这样再次重新执行命令时,shell就不用再寻找该命令。这种特性会绕过activate的变化而作用于环境,因此,activate/deactivate尝试清除这个缓存,但有时它们会失败。其结果是你输入命令的名称,获得“错误”的一个命令。为了100%确保你获得正确的命令,运行“which python” (或“which easy_install”等),这将打印已执行的命令的完整路径。如果这是错误的,输入完整的路径执行一次,这将更新缓存,这样就可以使用该版本的命令。 
如果有疑问,那就直接使用可执行文件,不要使用activate/deactivate技巧。

原文: http://hi.baidu.com/zhuifengdenanhai/blog/item/b7b00908ed813837e8248804.html

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2024

    京ICP备05028076号