-1

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/php@7.4/sbin:/usr/local/opt/php@7.4/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/php@7.4/sbin:/usr/local/opt/php@7.4/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
crmpicco
  • 16,605
  • 26
  • 134
  • 210
  • you run it with `sudo` so it runs as different user in different environment - without `venv`, using standard `python`. You may have to get `/full/path/to/python` in this `venv` ( run `which python` to get this path) and later use `sudo /full/path/to/python main.py` – furas Sep 20 '22 at 05:49
  • @furas it's definitely running it inside that environment because I have dependencies installed inside the Python 3 venv that are not available at system level – crmpicco Sep 20 '22 at 05:56
  • @furas Thanks, I tried the full path but that has no effect. – crmpicco Sep 20 '22 at 05:57
  • now I released that you would have to use `sudo` to run script which runs both `export RAIN_ENV="dev"` and `/full/path/to/python main.py`, or both `source /var/www/rain/bin/activate` and `python main.py`. `sudo` runs as new user with new environment and it may not know `RAIN_ENV="dev"` – furas Sep 20 '22 at 06:20
  • 1
    I checked `sudo --help` and it has option `--preserve-env` (`-E`) and maybe this can help. `sudo -E /full/path/to/python main.py main.py` (in active venv). BTW: you can also test it with `sudo -E env` – furas Sep 20 '22 at 06:23
  • @furas That worked, thank you. Feel free to submit as an answer and i'll gladly accept. – crmpicco Sep 20 '22 at 07:55

1 Answers1

1

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

furas
  • 134,197
  • 12
  • 106
  • 148