69

I am on ubuntu 13.04, bash, python2.7.4

The interpreter doesn't see variables I set.

Here is an example:

$ echo $A
5
$ python -c 'import os; print os.getenv( "A" )'
None
$ python -c 'import os; print os.environ[ "A" ]'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python2.7/UserDict.py", line 23, in __getitem__
    raise KeyError(key)
KeyError: 'A'

But everything works fine with the PATH variable:

$ echo $PATH 
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
$ python -c 'import os; print os.getenv("PATH")'
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

And it notices changes in PATH:

$ PATH="/home/alex/tests/:$PATH"
$ echo $PATH 
/home/alex/tests/:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
$ python -c 'import os; print os.getenv("PATH")'
/home/alex/tests/:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

What could be wrong?

PS the problem comes when using $PYTHONPATH:

$ python -c 'import os; print os.getenv("PYTHONPATH")'
None

4 Answers 4

118

Aha! the solution is simple!

I was setting variables with plain $ A=5 command; when you use $ export B="foo" everything is fine.

That is because export makes the variable available to sub-processes:

  • it creates a variable in the shell
  • and exports it into the environment of the shell
  • the environment is passed to sub-processes of the shell.

Plain $ A="foo" just creates variables in the shell and doesn't do anything with the environment.

The interpreter called from the shell obtains its environment from the parent -- the shell. So really the variable should be exported into the environment before.

1
  • environment is not code, so it should not be code-formatted. Commented Oct 18, 2021 at 21:27
9

Those variables (parameters in bash terminology) are not environment variables. You want to export them into the environment, using export or declare -x. See the bash documentation on environment.

3
  • hey! terminology is vague: docs.python.org/2/using/cmdline.html#environment-variables
    – xealits
    Commented Sep 28, 2013 at 19:18
  • Not sure which term you're complaining about, but the issue lies with bash. Python has no control over which variables bash puts in the environment. Commented Sep 28, 2013 at 19:21
  • I mean that if it is a "parameter" or "variable" -- doesn't matter to me. I care about setting PYTHONPATH to work. Though, I see your point -- you mean that assignment $ A=5 doesn't make an environment variable at all. There is a list of variables in the shell, called "environment". It is passed to sub-processes. And export declares some variables in that list (exports them into it). Plain assignment doesn't create variables in that list -- it puts them somewhere else. I'll correct my answer according to all of this.
    – xealits
    Commented Sep 28, 2013 at 19:32
0

Adding as I do not see an answer that has the exact issue I had. If you have multiple "shells" eg BASH and Z-Shell, ensure that you have exported the environment in the correct shell and that this is available to python.

If you are using VSCode and set the default shell to Z shell, then understandably, variables in .bashrc will not be visible to the python interpreter if they do not also exist in .zshrc. The solution then is to export the variable in both shells or change the default shell to the one with the necessary variables.

0

As @NelsonGon points out, this problem may occur if you set the environment variable in shell window A but then start Python from shell window B, because window B doesn't know anything about window A's environment.

One solution outlined in this Reddit post is to use a Python library called python-dotenv to load a small environment file into the Python environment you're using.

To quote their answer (with minor edits):

  1. Install python-dotenv
pip install python-dotenv
  1. Make a file in your python project directory called .env

  2. Open it with your editor (nano, vim, etc.) and add secrets to it like so:

MY_SECRET_KEY="12345"
SOME_OTHER_SECRET="45678"

  1. Then in your python code, you can write the following and it will work.
from dotenv import load_dotenv
import os

load_dotenv() # you can specify a location to your .env file as an argument if it's not at your project root

SECRET_KEY = os.getenv('MY_SECRET_KEY')
1
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – Ouroborus
    Commented Feb 1 at 3:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.