创建venv时Python设置环境变量 [英] Python set environment variable when creating venv
问题描述
在我的项目中,我使用内置的python虚拟环境( python -m venv
).
要设置环境变量,我将多个 export VAR1 = VALUE1
添加到 venv/bin/activate
的末尾.
显然,当我删除venv并创建一个新的venv时,例如使用新的python版本时,我所有的env变量都会丢失.
那么,有没有办法保存它们呢?创建venv时是否可能定义env变量?
In my project I use the built-in python virtual env (python -m venv
).
To set environment variables I add multiple export VAR1=VALUE1
to the end of the venv/bin/activate
.
Obviously, when I delete the venv and create a new one, for example with the new python version all my env variables get lost.
So, is there a way to preserve them? May be it is possible to define env variables when creating the venv?
推荐答案
而不是添加到 activate
export VAR1=VALUE1
考虑将它们写入自己的文件中:
consider writing them into their own file:
〜/setupenv.sh :
export VAR1=VALUE1
并将以下内容添加到 activate
source ~/setupenv.sh
但是,就我个人而言,我不会这样做.我会定义一个bash函数来做到这一点:
However, personally, I would not do that. I would instead define a bash function to do it:
myownactivate(){
source <path_to_activate>
export VAR1=VALUE1
}
这篇关于创建venv时Python设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!