在virtualenv中设置环境变量

时间:2022-03-31 01:45:06

I have a Heroku project that uses environment variables to get its configuration, but I use virtualenv to test my app locally first.

我有一个Heroku项目,它使用环境变量来获得配置,但是我首先使用virtualenv在本地测试我的应用程序。

Is there a way to set the environment variables defined on the remote machine inside virtualenv?

是否有一种方法可以在virtualenv中设置远程机器上定义的环境变量?

9 个解决方案

#1


90  

Update

As of 17th May 2017 the README of autoenv states that direnv is probably the better option and implies autoenv is no longer maintained.

截至2017年5月17日,autoenv的自述表明,direnv可能是更好的选择,并暗示autoenv将不再被维护。

Old answer

I wrote autoenv to do exactly this:

我给autoenv写的就是这样:

https://github.com/kennethreitz/autoenv

https://github.com/kennethreitz/autoenv

#2


261  

In case you're using virtualenvwrapper (I highly recommend doing so), you can define different hooks (preactivate, postactivate, predeactivate, postdeactivate) using the scripts with the same names in $VIRTUAL_ENV/bin/. You need the postactivate hook.

如果您正在使用virtualenvwrapper(我极力推荐这么做),您可以使用$VIRTUAL_ENV/bin/中的同名脚本来定义不同的hook (preactivate、postactivate、predeactivate、postdeactivate)。你需要后激活钩子。

$ workon myvenv

$ cat $VIRTUAL_ENV/bin/postactivate
#!/bin/bash
# This hook is run after this virtualenv is activated.
export DJANGO_DEBUG=True
export S3_KEY=mykey
export S3_SECRET=mysecret

$ echo $DJANGO_DEBUG
True

If you want to keep this configuration in your project directory, simply create a symlink from your project directory to $VIRTUAL_ENV/bin/postactivate.

如果您希望在项目目录中保留此配置,只需从项目目录创建一个符号链接到$VIRTUAL_ENV/bin/postactivate。

$ rm $VIRTUAL_ENV/bin/postactivate
$ ln -s .env/postactivate $VIRTUAL_ENV/bin/postactivate

You could even automate the creation of the symlinks each time you use mkvirtualenv.

您甚至可以在每次使用mkvirtualenv时自动创建符号链接。

Cleaning up on deactivate

Remember that this wont clean up after itself. When you deactivate the virtualenv, the environment variable will persist. To clean up symmetrically you can add to $VIRTUAL_ENV/bin/predeactivate.

记住,它不会自己清理。当您停用virtualenv时,环境变量将保持不变。要对称地清理,可以添加$VIRTUAL_ENV/bin/predeactivate。

$ cat $VIRTUAL_ENV/bin/predeactivate
#!/bin/bash
# This hook is run before this virtualenv is deactivated.
unset DJANGO_DEBUG

$ deactivate

$ echo $DJANGO_DEBUG

Remember that if using this for environment variables that might already be set in your environment then the unset will result in them being completely unset on leaving the virtualenv. So if that is at all probable you could record the previous value somewhere temporary then read it back in on deactivate.

请记住,如果对可能已经在您的环境中设置的环境变量使用此方法,那么在离开virtualenv时,unset将导致它们完全未设置。所以如果这是完全可能的,你可以将之前的值记录在某个临时的地方,然后在停用时重新读取它。

Setup:

设置:

$ cat $VIRTUAL_ENV/bin/postactivate
#!/bin/bash
# This hook is run after this virtualenv is activated.
if [[ -n $SOME_VAR ]]
then
    export SOME_VAR_BACKUP=$SOME_VAR
fi
export SOME_VAR=apple

$ cat $VIRTUAL_ENV/bin/predeactivate
#!/bin/bash
# This hook is run before this virtualenv is deactivated.
if [[ -n $SOME_VAR_BACKUP ]]
then
    export SOME_VAR=$SOME_VAR_BACKUP
    unset SOME_VAR_BACKUP
else
    unset SOME_VAR
fi

Test:

测试:

$ echo $SOME_VAR
banana

$ workon myenv

$ echo $SOME_VAR
apple

$ deactivate

$ echo $SOME_VAR
banana

#3


30  

You could try:

你可以试试:

export ENVVAR=value

in virtualenv_root/bin/activate. Basically the activate script is what is executed when you start using the virtualenv so you can put all your customization in there.

在virtualenv_root / bin /激活。基本上,激活脚本是当您开始使用virtualenv时执行的,这样您就可以将所有的自定义放在其中。

#4


25  

Using only virtualenv (without virtualenvwrapper), setting environment variables is easy through the activate script you sourcing in order to activate the virtualenv.

仅使用virtualenv(没有virtualenvwrapper),通过您所提供的激活脚本,设置环境变量很容易,以便激活virtualenv。

Run:

运行:

nano YOUR_ENV/bin/activate

Add the environment variables to the end of the file like this:

将环境变量添加到文件末尾,如下所示:

export KEY=VALUE

You can also set a similar hook to unset the environment variable as suggested by Danilo Bargen in his great answer above if you need.

您还可以设置一个类似的钩子来取消环境变量的设置,如Danilo Bargen在上面他的伟大的回答中所建议的,如果您需要的话。

#5


18  

While there are a lot of nice answers here, I didn't see a solution posted that both includes unsetting environment variables on deactivate and doesn't require additional libraries beyond virtualenv, so here's my solution that just involves editing /bin/activate, using the variables MY_SERVER_NAME and MY_DATABASE_URL as examples:

虽然这里有很多不错的答案,但我没有看到一个解决方案,它既包括在deactivate上的unsetting环境变量,也不需要除virtualenv之外的其他库,所以我的解决方案只涉及编辑/bin/activate,使用变量MY_SERVER_NAME和MY_DATABASE_URL作为示例:

There should be a definition for deactivate in the activate script, and you want to unset your variables at the end of it:

激活脚本中应该有一个禁用的定义,您希望在其末尾取消变量设置:

deactivate () {
    ...

    # Unset My Server's variables
    unset MY_SERVER_NAME
    unset MY_DATABASE_URL
}

Then at the end of the activate script, set the variables:

然后在激活脚本的最后,设置变量:

# Set My Server's variables
export MY_SERVER_NAME="<domain for My Server>"
export MY_DATABASE_URL="<url for database>"

This way you don't have to install anything else to get it working, and you don't end up with the variables being left over when you deactivate the virtualenv.

这样,您就不需要安装任何其他东西来让它工作,并且当您停用virtualenv时,您也不需要留下变量。

#6


16  

Locally within an virtualenv there are two methods you could use to test this. The first is a tool which is installed via the Heroku toolbelt (https://toolbelt.heroku.com/). The tool is foreman. It will export all of your environment variables that are stored in a .env file locally and then run app processes within your Procfile.

在一个virtualenv中,有两个方法可以用来测试它。第一个是通过Heroku工具带(https://toolbelt.heroku.com/)安装的工具。这个工具是工头。它将导出存储在.env文件中的所有环境变量,然后在您的Procfile中运行应用程序进程。

The second way if you're looking for a lighter approach is to have a .env file locally then run:

如果你想要寻找一种更轻松的方法,第二种方法是在本地创建.env文件,然后运行:

export $(cat .env)

#7


6  

Install autoenv either by

安装autoenv通过

$ pip install autoenv

(or)

(或)

$ brew install autoenv

And then create .env file in your virtualenv project folder

然后在virtualenv项目文件夹中创建.env文件

$ echo "source bin/activate" > .env

Now everything works fine.

现在,一切工作正常。

#8


4  

Another way to do it that's designed for django, but should work in most settings, is to use django-dotenv.

另一种为django设计的方法是使用django-dotenv。

  • Original - https://github.com/jacobian/django-dotenv
  • 原来——https://github.com/jacobian/django-dotenv
  • More fully featured fork- https://github.com/tedtieken/django-dotenv-rw (I wrote this to be able to set my remote .env settings on webfaction from my local command line, heroku spoiled me)
  • 更有特色的fork- https://github.com/tedtieken/django-dotenv-rw(我写这篇文章是为了能够从本地命令行设置我的远程.env web settings, heroku宠坏了我)

#9


3  

If you're already using Heroku, consider running your server via Foreman. It supports a .env file which is simply a list of lines with KEY=VAL that will be exported to your app before it runs.

如果您已经在使用Heroku,请考虑通过工头运行您的服务器。它支持一个.env文件,它只是一个带有KEY=VAL的行列表,在运行之前会导出到应用程序中。

#1


90  

Update

As of 17th May 2017 the README of autoenv states that direnv is probably the better option and implies autoenv is no longer maintained.

截至2017年5月17日,autoenv的自述表明,direnv可能是更好的选择,并暗示autoenv将不再被维护。

Old answer

I wrote autoenv to do exactly this:

我给autoenv写的就是这样:

https://github.com/kennethreitz/autoenv

https://github.com/kennethreitz/autoenv

#2


261  

In case you're using virtualenvwrapper (I highly recommend doing so), you can define different hooks (preactivate, postactivate, predeactivate, postdeactivate) using the scripts with the same names in $VIRTUAL_ENV/bin/. You need the postactivate hook.

如果您正在使用virtualenvwrapper(我极力推荐这么做),您可以使用$VIRTUAL_ENV/bin/中的同名脚本来定义不同的hook (preactivate、postactivate、predeactivate、postdeactivate)。你需要后激活钩子。

$ workon myvenv

$ cat $VIRTUAL_ENV/bin/postactivate
#!/bin/bash
# This hook is run after this virtualenv is activated.
export DJANGO_DEBUG=True
export S3_KEY=mykey
export S3_SECRET=mysecret

$ echo $DJANGO_DEBUG
True

If you want to keep this configuration in your project directory, simply create a symlink from your project directory to $VIRTUAL_ENV/bin/postactivate.

如果您希望在项目目录中保留此配置,只需从项目目录创建一个符号链接到$VIRTUAL_ENV/bin/postactivate。

$ rm $VIRTUAL_ENV/bin/postactivate
$ ln -s .env/postactivate $VIRTUAL_ENV/bin/postactivate

You could even automate the creation of the symlinks each time you use mkvirtualenv.

您甚至可以在每次使用mkvirtualenv时自动创建符号链接。

Cleaning up on deactivate

Remember that this wont clean up after itself. When you deactivate the virtualenv, the environment variable will persist. To clean up symmetrically you can add to $VIRTUAL_ENV/bin/predeactivate.

记住,它不会自己清理。当您停用virtualenv时,环境变量将保持不变。要对称地清理,可以添加$VIRTUAL_ENV/bin/predeactivate。

$ cat $VIRTUAL_ENV/bin/predeactivate
#!/bin/bash
# This hook is run before this virtualenv is deactivated.
unset DJANGO_DEBUG

$ deactivate

$ echo $DJANGO_DEBUG

Remember that if using this for environment variables that might already be set in your environment then the unset will result in them being completely unset on leaving the virtualenv. So if that is at all probable you could record the previous value somewhere temporary then read it back in on deactivate.

请记住,如果对可能已经在您的环境中设置的环境变量使用此方法,那么在离开virtualenv时,unset将导致它们完全未设置。所以如果这是完全可能的,你可以将之前的值记录在某个临时的地方,然后在停用时重新读取它。

Setup:

设置:

$ cat $VIRTUAL_ENV/bin/postactivate
#!/bin/bash
# This hook is run after this virtualenv is activated.
if [[ -n $SOME_VAR ]]
then
    export SOME_VAR_BACKUP=$SOME_VAR
fi
export SOME_VAR=apple

$ cat $VIRTUAL_ENV/bin/predeactivate
#!/bin/bash
# This hook is run before this virtualenv is deactivated.
if [[ -n $SOME_VAR_BACKUP ]]
then
    export SOME_VAR=$SOME_VAR_BACKUP
    unset SOME_VAR_BACKUP
else
    unset SOME_VAR
fi

Test:

测试:

$ echo $SOME_VAR
banana

$ workon myenv

$ echo $SOME_VAR
apple

$ deactivate

$ echo $SOME_VAR
banana

#3


30  

You could try:

你可以试试:

export ENVVAR=value

in virtualenv_root/bin/activate. Basically the activate script is what is executed when you start using the virtualenv so you can put all your customization in there.

在virtualenv_root / bin /激活。基本上,激活脚本是当您开始使用virtualenv时执行的,这样您就可以将所有的自定义放在其中。

#4


25  

Using only virtualenv (without virtualenvwrapper), setting environment variables is easy through the activate script you sourcing in order to activate the virtualenv.

仅使用virtualenv(没有virtualenvwrapper),通过您所提供的激活脚本,设置环境变量很容易,以便激活virtualenv。

Run:

运行:

nano YOUR_ENV/bin/activate

Add the environment variables to the end of the file like this:

将环境变量添加到文件末尾,如下所示:

export KEY=VALUE

You can also set a similar hook to unset the environment variable as suggested by Danilo Bargen in his great answer above if you need.

您还可以设置一个类似的钩子来取消环境变量的设置,如Danilo Bargen在上面他的伟大的回答中所建议的,如果您需要的话。

#5


18  

While there are a lot of nice answers here, I didn't see a solution posted that both includes unsetting environment variables on deactivate and doesn't require additional libraries beyond virtualenv, so here's my solution that just involves editing /bin/activate, using the variables MY_SERVER_NAME and MY_DATABASE_URL as examples:

虽然这里有很多不错的答案,但我没有看到一个解决方案,它既包括在deactivate上的unsetting环境变量,也不需要除virtualenv之外的其他库,所以我的解决方案只涉及编辑/bin/activate,使用变量MY_SERVER_NAME和MY_DATABASE_URL作为示例:

There should be a definition for deactivate in the activate script, and you want to unset your variables at the end of it:

激活脚本中应该有一个禁用的定义,您希望在其末尾取消变量设置:

deactivate () {
    ...

    # Unset My Server's variables
    unset MY_SERVER_NAME
    unset MY_DATABASE_URL
}

Then at the end of the activate script, set the variables:

然后在激活脚本的最后,设置变量:

# Set My Server's variables
export MY_SERVER_NAME="<domain for My Server>"
export MY_DATABASE_URL="<url for database>"

This way you don't have to install anything else to get it working, and you don't end up with the variables being left over when you deactivate the virtualenv.

这样,您就不需要安装任何其他东西来让它工作,并且当您停用virtualenv时,您也不需要留下变量。

#6


16  

Locally within an virtualenv there are two methods you could use to test this. The first is a tool which is installed via the Heroku toolbelt (https://toolbelt.heroku.com/). The tool is foreman. It will export all of your environment variables that are stored in a .env file locally and then run app processes within your Procfile.

在一个virtualenv中,有两个方法可以用来测试它。第一个是通过Heroku工具带(https://toolbelt.heroku.com/)安装的工具。这个工具是工头。它将导出存储在.env文件中的所有环境变量,然后在您的Procfile中运行应用程序进程。

The second way if you're looking for a lighter approach is to have a .env file locally then run:

如果你想要寻找一种更轻松的方法,第二种方法是在本地创建.env文件,然后运行:

export $(cat .env)

#7


6  

Install autoenv either by

安装autoenv通过

$ pip install autoenv

(or)

(或)

$ brew install autoenv

And then create .env file in your virtualenv project folder

然后在virtualenv项目文件夹中创建.env文件

$ echo "source bin/activate" > .env

Now everything works fine.

现在,一切工作正常。

#8


4  

Another way to do it that's designed for django, but should work in most settings, is to use django-dotenv.

另一种为django设计的方法是使用django-dotenv。

  • Original - https://github.com/jacobian/django-dotenv
  • 原来——https://github.com/jacobian/django-dotenv
  • More fully featured fork- https://github.com/tedtieken/django-dotenv-rw (I wrote this to be able to set my remote .env settings on webfaction from my local command line, heroku spoiled me)
  • 更有特色的fork- https://github.com/tedtieken/django-dotenv-rw(我写这篇文章是为了能够从本地命令行设置我的远程.env web settings, heroku宠坏了我)

#9


3  

If you're already using Heroku, consider running your server via Foreman. It supports a .env file which is simply a list of lines with KEY=VAL that will be exported to your app before it runs.

如果您已经在使用Heroku,请考虑通过工头运行您的服务器。它支持一个.env文件,它只是一个带有KEY=VAL的行列表,在运行之前会导出到应用程序中。