1

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?

Milano
  • 18,048
  • 37
  • 153
  • 353
  • 2
    Have you tried `export PRODUCTION=1` ? – Nic3500 Sep 06 '21 at 19:40
  • 1
    As @nic3500 is hinting at, you haven't exported your shell variables to the environment. Running `somevar=1` creates a *shell* variable; it's not in the environment and won't be visible to child processes. You only get an environment variable if you explicitly `export` it. – larsks Sep 06 '21 at 20:57

1 Answers1

1

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
Dharman
  • 30,962
  • 25
  • 85
  • 135
Ricky
  • 2,850
  • 5
  • 28
  • 41