简体   繁体   English

自定义virtualenv`activate`脚本

[英]Customise the virtualenv `activate` script

What is the conventional way to customise the changes made by a Python virtualenv bin/activate script? 定制Python virtualenv bin/activate脚本所做的更改的常规方式是什么?

The Python virtualenv is activated by a $VENV/bin/activate script . Python virtualenv由$VENV/bin/activate脚本$VENV/bin/activate This script works primarily by setting environment variables specific to the virtualenv. 该脚本主要通过设置特定于virtualenv的环境变量来工作。

(This is different from the “bootstrap script” discussed in the Virtualenv documentation . Those are used once, when creating the virtualenv; they are not used when activating the virtualenv. This question is about the activate script.) (这与Virtualenv文档中讨论的“引导脚本”不同。在创建virtualenv时仅使用一次;在激活virtualenv时不使用它们。这个问题与激活脚本有关。)

Many of the code bases for which I use a Python virtualenv, need additional (custom) environment variables set, at the same time the virtualenv is activated. 我使用Python virtualenv的许多代码库都需要设置其他(自定义)环境变量,同时激活virtualenv。

  • Edit the $VENV/bin/activate script directly, to add statements that set more environment variables? 直接编辑$VENV/bin/activate脚本,以添加设置更多环境变量的语句?

  • Write a custom wrapper script, that incidentally calls $VENV/bin/activate ? 编写一个自定义包装脚本,该脚本偶然会调用$VENV/bin/activate

  • Write a separate script with a specific name, that will be automatically called by the $VENV/bin/activate script? 编写一个具有特定名称的单独脚本,该脚本将由$VENV/bin/activate脚本自动调用吗? Does such a thing exist? 这样的事情存在吗?

Of course I could write a script with a different name, and instruct developers to run that instead. 当然,我可以使用其他名称编写脚本,并指示开发人员运行该脚本。 Or I could instruct them to install virtualenvwrapper . 或者,我可以指示他们安装virtualenvwrapper But that is not the if-you-got-the-virtualenv-working-then-this-works-too automation that I'm seeking. 但这不是您想要的虚拟环境那么工作,而是我所追求的自动化程度。

Instead, this question is asking how to hook into the existing convention, of activating the virtualenv with a known name $VENV/bin/activate . 取而代之的是,这个问题询问如何激活现有的约定,即使用已知名称$VENV/bin/activate virtualenv。

How about using virtualenvwrapper and editing $VIRTUAL_ENV/bin/postactivate ? 如何使用virtualenvwrapper和编辑$VIRTUAL_ENV/bin/postactivate http://virtualenvwrapper.readthedocs.io/en/latest/scripts.html#scripts-postactivate http://virtualenvwrapper.readthedocs.io/en/latest/scripts.html#scripts-postactivate

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

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