简体   繁体   English

如何使用 Python 3 venv 使用 postactivate 脚本?

[英]How can I use a postactivate script using Python 3 venv?

I'm using venv (used pyvenv to create the environment) and would like to set up environment variables here, but postactivate looks like a virtualenv thing.我正在使用venv (使用pyvenv创建环境)并想在这里设置环境变量,但postactivate看起来像一个virtualenv的东西。 Can this be done with venv ?这可以用venv完成吗?

venv has the activate script which you can modify to add your environment variables. venvactivate脚本,你可以修改它来添加你的环境变量。

I would add the variables at the bottom, making a nice comment block to clearly separate the core functionality and my custom variables.我会在底部添加变量,制作一个很好的注释块,以清楚地区分核心功能和我的自定义变量。

Put your setup config in [your_virtualenv_dir]/bin/postactivate and your teardown config in [your_virtualenv_dir]/bin/predeactivate .将您的设置配置放入[your_virtualenv_dir]/bin/postactivate并将您的拆解配置放入[your_virtualenv_dir]/bin/predeactivate

eg in postactivate :例如在postactivate中:

YOUR_ENV_VAR="hello world!"

eg in predeactivate :例如在predeactivate中:

unset YOUR_ENV_VAR

Open [your_virtualenv_dir]/bin/activate (not activate.csh or activate.fish)打开[your_virtualenv_dir]/bin/activate (不是 activate.csh 或 activate.fish)

To set environment variable on activate, put your exports at the bottom of the file eg要在激活时设置环境变量,请将您的导出文件放在文件的底部,例如

export SOME_VARIABLE=some_value

To unset the environment variable upon deactivation, unset the variables within the deactivate function at the top of the file eg要在停用时取消设置环境变量,请取消设置文件顶部停用功能中的变量,例如

deactivate(
  ...
  # default code by venv
  ...
  
  unset SOME_VARIABLE

)

Seems to work fine for me.似乎对我来说工作得很好。

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

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