I have added the following line to my bin/activate
inside my Python 3 venv.
export RAIN_ENV="dev"
I activate the venv, then fire up the REPL and can see the RAIN_ENV
...
source /var/www/rain/bin/activate
(rain) crmpicco/var/www/rain(master|✚1) % python
Python 3.9.10 (main, Jan 15 2022, 11:48:04)
[Clang 13.0.0 (clang-1300.0.29.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print(os.environ)
environ({'__CFBundleIdentifier': 'com.apple.Terminal', 'TERM_PROGRAM_VERSION': '445', 'TERM_SESSION_ID': '12033E65-4D4B-4C11-8471-2708CFC4F550', 'SHELL': '/bin/zsh', 'PATH': '/var/www/rain/bin:/usr/local/opt/[email protected]/sbin:/usr/local/opt/[email protected]/bin:/Users/crm/.yarn/bin:/Users/crm/.config/yarn/global/node_modules/.bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/opt/postgresql@12/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin', 'SHLVL': '1', 'PWD': '/var/www/rain', 'OLDPWD': '/var/www/rain/rain', 'LIBRESSL_REDIRECT_STUB_ABORT': '0', 'EDITOR': '/usr/bin/vim', '__GIT_PROMPT_DIR': '/Users/crm/zsh-git-prompt', 'GIT_PROMPT_EXECUTABLE': 'python', 'PS1': '(rain) %B%m%~%b$(git_super_status) %# ', 'RAIN_ENV': 'dev', 'VIRTUAL_ENV': '/var/www/rain', 'LANG': 'en_AU.UTF-8', '_': '/var/www/rain/bin/python'})
>>>
However, when I start my Python 3 application inside the venv the code returns an entirely different set of environment variables:
(rain) crmpicco/var/www/rain(master|✚1) % sudo python main.py
Password:
env vars
environ({'TERM': 'xterm-256color', 'SSH_AUTH_SOCK': '/private/tmp/com.apple.launchd.rny3w7r22E/Listeners', 'HOME': '/Users/crm', 'PATH': '/var/www/rain/bin:/usr/local/opt/[email protected]/sbin:/usr/local/opt/[email protected]/bin:/Users/crm/.yarn/bin:/Users/crm/.config/yarn/global/node_modules/.bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/opt/postgresql@12/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin', 'EDITOR': '/usr/bin/vim', 'PS1': '(rain) %B%m%~%b$(git_super_status) %# ', 'LANG': 'en_AU.UTF-8', 'MAIL': '/var/mail/root', 'LOGNAME': 'root', 'USER': 'root', 'SHELL': '/bin/sh', 'SUDO_COMMAND': '/var/www/rain/bin/python main.py --env=dev', 'SUDO_USER': 'crm', 'SUDO_UID': '502', 'SUDO_GID': '20', '__CF_USER_TEXT_ENCODING': '0x0:0:15'})
The output there is the result of print(os.environ)
How do I pull in the variables from the venv?
(rain) crmpicco/var/www/rain(master|✚2) % which python
/var/www/rain/bin/python
CodePudding user response:
When you use sudo
then it runs command as different user in different environment - without venv
, using standard python.
You may need to run which python
to get /full/path/to/python
in active venv
and later use
sudo /full/path/to/python main.py
It may also needs to run sudo
with option --preserve-env
(-E
) to have access to RAIN_ENV="dev"
and all other settings from original environment
sudo -E /full/path/to/python main.py
or shorter (in bash
, probably in zsh
, and in the newest fish
)
sudo -E $(which python) main.py
You may test environment using sudo -E env