简体   繁体   English

python virtualenv —是否可以增加$ PATH或添加其他环境变量?

[英]python virtualenv — possible to augment $PATH or add other environment variables?

I'd like to alter my $PATH only in a Python virtual environment. 我只想在Python虚拟环境中更改$ PATH。 Is it possible to have the $PATH change when I activate a virtual environment? 激活虚拟环境时,是否可以更改$ PATH?

You can write an activation script that sources virtualenv's activate (on linux, or calls the bat file on windows) and then updates PATH, PYTHONPATH and other environment variables. 您可以编写一个激活脚本来获取virtualenv的激活信息(在Linux上,或者在Windows上调用bat文件),然后更新PATH,PYTHONPATH和其他环境变量。 Use the virtualenv bootstrap hooks to install the script when the virtualenv is created and call it instead of activate . 使用的virtualenv引导钩创建的virtualenv当安装脚本,并调用它,而不是activate

Depending on what operating system you are using you could edit the activate file and set an environment variable there. 根据您使用的操作系统,可以编辑激活文件并在其中设置环境变量。 For example, a Windows virtualenv folder has a sub-folder called Scripts. 例如,Windows virtualenv文件夹包含一个名为Scripts的子文件夹。 Inside scripts is the activate.bat file. 脚本内部是activate.bat文件。 Edit activate.bat and alter the path variable. 编辑activate.bat并更改路径变量。 One thing to consider though, is you might want to save the original path variable in another temporary environment variable and restore from that temporary environment variable in the deactivate.bat file. 但是要考虑的一件事是,您可能希望将原始路径变量保存在另一个临时环境变量中,并从deactivate.bat文件中的该临时环境变量中恢复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM