virtualenv에서 환경 변수 설정
환경 변수를 사용하여 구성을 얻는 Heroku 프로젝트가 있지만 virtualenv를 사용하여 로컬에서 앱을 먼저 테스트합니다.
virtualenv 내의 원격 시스템에 정의 된 환경 변수를 설정하는 방법이 있습니까?
최신 정보
2017년 5월 17일 기준으로하는 것이 autoenv 상태의 README direnv은 아마 더 나은 옵션이며 autoenv을 의미는 더 이상 유지되지 않습니다.
이전 답변
나는 이것을 정확하게하기 위해 autoenv를 썼다 :
https://github.com/kennethreitz/autoenv
virtualenvwrapper를 사용하는 경우 (권장 사항)의 동일한 이름의 스크립트를 사용하여 다른 후크 (사전 활성화, 사후 활성화, 사전 비활성화, 사후 비활성화)를 정의 할 수 있습니다 $VIRTUAL_ENV/bin/
. 사후 활성화 후크가 필요합니다.
$ 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
이 구성을 프로젝트 디렉토리에 유지하려면 프로젝트 디렉토리에서로 심볼릭 링크를 작성하십시오 $VIRTUAL_ENV/bin/postactivate
.
$ rm $VIRTUAL_ENV/bin/postactivate
$ ln -s .env/postactivate $VIRTUAL_ENV/bin/postactivate
mkvirtualenv 를 사용할 때마다 심볼릭 링크 생성을 자동화 할 수도 있습니다 .
비활성화시 정리
이 후에는 정리되지 않습니다. 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
환경에 이미 설정되어있을 수있는 환경 변수에이 변수를 사용하면 설정이 해제되어 virtualenv를 떠날 때 완전히 설정 해제됩니다. 따라서 이것이 가능한 경우 이전 값을 임시 어딘가에 기록 한 다음 비활성화시 다시 읽습니다.
설정:
$ 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
테스트:
$ echo $SOME_VAR
banana
$ workon myenv
$ echo $SOME_VAR
apple
$ deactivate
$ echo $SOME_VAR
banana
시도해 볼 수 있습니다 :
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.
Using only virtualenv (without virtualenvwrapper), setting environment variables is easy through the activate
script you sourcing in order to activate the 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.
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:
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.
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.
The second way if you're looking for a lighter approach is to have a .env file locally then run:
export $(cat .env)
Install autoenv either by
$ pip install autoenv
(or)
$ brew install autoenv
And then create .env
file in your virtualenv project folder
$ echo "source bin/activate" > .env
Now everything works fine.
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.
Another way to do it that's designed for django, but should work in most settings, is to use django-dotenv.
- Original - 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)
Another approach is to fork a bash shell with a venv running inside. Run an executable containing:
# my_env.sh
export MY_VENV=true
bash
In ~/.bashrc put:
# .bashrc
if [ "$MY_VENV" = "true" ]; then
source ~/.pyenv/bin/activate
export PYTHONPATH=/some/local/libs
cd /project/path
PS1='(my_venv:\w)$ '
fi
Exiting the forked shell restores the original environment, and you don't have to run deactivate.
참고URL : https://stackoverflow.com/questions/9554087/setting-an-environment-variable-in-virtualenv
'Programing' 카테고리의 다른 글
Jupyter Notebook의 파일에서 이미지를 표시하려면 어떻게해야합니까? (0) | 2020.06.13 |
---|---|
iPhone에서 Objective-C로 메소드 이름 NSLog (0) | 2020.06.13 |
appCompat 22.1 이상에서 새로운 AlertDialog를 사용하고 스타일을 지정하는 방법 (0) | 2020.06.13 |
커서를 잡기위한 CSS (드래그 앤 드롭) (0) | 2020.06.13 |
조건 A가 일치하면 조치 C를 수행하기 위해 조건 B를 일치시켜야합니다. (0) | 2020.06.13 |