James
James

Reputation: 1461

Environment variable NOT set in os Ubuntu, but set in os.environ

I'm using the os package to set some environment variable, like this :

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
print ("--------------------------> : ", os.environ)

print '>>>> ',os.getenv('MY_ENV_APP')

if "MY_ENV_APP" not in os.environ:
    print "not in !"
    os.environ['MY_ENV_APP'] = "prod"
else:
    print "ok, good boy !"


print '>>>> ',os.getenv('MY_ENV_APP')
print ("--------------------------> : ", os.environ)

The "MY_ENV_APP" is setting using os.environ, but NOT in Ubuntu system :

...
/usr/local/rvm/bin', 'MY_ENV_APP': 'prod', 'GEM_HOME': '/us
...

>>>>  None
not in !
>>>>  prod

...
/usr/local/rvm/bin', 'MY_ENV_APP': 'prod', 'GEM_HOME': '/us
...

System os :

[02:59 ]-[vagrant@host]-[/var/www/python]
$ printenv | grep -i my_env_app

[02:59 ]-[vagrant@host]-[/var/www/python]
$ printenv | grep MY_ENV_APP

[03:00 ]-[vagrant@host]-[/var/www/python]
$ echo $MY_ENV_APP

$[03:02 ]-[vagrant@host]-[/var/www/python]

So, the environment variable is setting "in python" but not in the system.

Can you help me please ?

Thanks, Fabrice

Upvotes: 0

Views: 410

Answers (1)

baldr
baldr

Reputation: 2999

Example:

MY_ENV_APP=abcdef python test.py

This will set the variable for this process only.

Upvotes: 1

Related Questions