pythonpyenvpyenv-virtualenv

Pyenv does not see environment variables


I'm using a virtualenv with pyenv and although I have environment variables loaded, I can't access them in Python:

(venv) ➜ which python
/home/gonczor/.pyenv/versions/3.9.6/envs/venv/bin/python
(venv) ➜ echo $DEBUG 

(venv) ➜ source .env
(venv) ➜ echo $DEBUG
True
(venv) ➜  python        
Python 3.9.6 (default, Aug 15 2021, 19:46:48) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ["DEBUG"]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/gonczor/.pyenv/versions/3.9.6/lib/python3.9/os.py", line 679, in __getitem__
    raise KeyError(key) from None
KeyError: 'DEBUG'
>>> 

Solution

  • I don't know why but it seems if you do not define the environment variable before sourcing into the environment this issue will be there.

    Try

    export DEBUG=True
    

    and then try again it'll solve your issue.