python os.getenv can't see variables defined in ~/.profile

1.2k Views Asked by At

I'm trying to set some environment variables on the DigitalOcean droplet for my python project.

I put them to the ~/.profile file. Now it looks like this:

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi

PRODUCTION=1

After droplet reset, I tried to get the PRODUCTION in python script but it returns None.

>>> import os
>>> os.getenv('PRODUCTION')
>>>

What I'm doing wrong? If not .profile, what file should I use to permanently set such variables?

1

There are 1 best solutions below

0
Ricky On

Posting an answer since I spent some time searching too

As both comments stated, you need to specifically export the variable in your ~/profile file

# Use this
export MYVAR=1

# NOT this
MYVAR=1