Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python os.environ, os.putenv, /usr/bin/env

Tags:

python

I want to ensure os.system('env') not contain some specific variable myname which is export in ~/.bashrc as export myname=csj

Therefore, I wrote below python code:

import os

def print_all():
    print "os.environ['myname']=%s" % os.environ.get('myname')
    print "os.getenv('myname')=%s" % os.getenv('myname')
    os.system('env | grep myname')
    print

def delete_myname():
    if 'myname' in os.environ: os.environ.pop('myname')
    if os.getenv('myname'): os.unsetenv('myname')

print_all()

os.putenv('myname', 'csj2')
print "---------------------"
delete_myname()
print_all()

os.putenv('myname', 'csj3')
print "---------------------"
delete_myname()
print_all()

I think examine both os.environ['myname'] and os.getenv('myname') and then delete them if exist, can ensure os.system('env | grep myname') get nothing.

However, the result is:

os.environ['myname']=csj
os.getenv('myname')=csj
myname=csj

---------------------
os.environ['myname']=None
os.getenv('myname')=None

---------------------
os.environ['myname']=None
os.getenv('myname')=None
myname=csj3

I don't understand why I still got csj3 on os.system('env | grep myname')?

like image 853
CSJ Avatar asked Jul 17 '13 16:07

CSJ


People also ask

What is OS environ in Python?

environ in Python is a mapping object that represents the user's environmental variables. It returns a dictionary having user's environmental variable as key and their values as value. os. environ behaves like a python dictionary, so all the common dictionary operations like get and set can be performed.

How do I use .ENV in Python?

First install Python Decouple into your local Python environment. Once installed, create a . env file in the root of your project which you can then open up to add your environment variables. Then save (WriteOut) the file and exit nano.

How do I set an environment variable in Python?

To permanently modify the default environment variables, click Start and search for 'edit environment variables', or open System properties, Advanced system settings and click the Environment Variables button. In this dialog, you can add or modify User and System variables.

How do I see environment variables in Python?

# Checking if an Environment Variable Exists in Python import os if 'USER' in os. environ: print('Environment variable exists! ') else: print('Environment variable does not exist. ') # Returns: # Environment variable exists!


1 Answers

From the docs:

Note: Calling putenv() directly does not change os.environ, so it’s better to modify os.environ.

For unsetenv there is a similar warining:

however, calls to unsetenv() don’t update os.environ, so it is actually preferable to delete items of os.environ.

getenv just returns the value from os.environ, as it's implementation shows, so by using it you get into a state where it seems the value isn't set when you look it up from python, while it acutally is in the real environment. The only way to get it now I can think of would be to call the c getenv function using ctypes...

If i modify your code to use os.environ isnstead of calling putenv/unsetenv everything works as expected:

import os

def print_all():
    print "os.environ['myname']=%s" % (os.environ['myname'] if 'myname' in os.environ else "None")
    os.system('env | grep myname')
    print

def delete_myname():
    if 'myname' in os.environ: os.environ.pop('myname')

print_all()

os.environ['myname'] = 'csj2'
print "---------------------"
print_all()
delete_myname()
print_all()

os.environ['myname'] = 'csj3'
print "---------------------"
print_all()
delete_myname()
print_all()

output:

$ myname=somevalue python2 test.py 
os.environ['myname']=somevalue
myname=somevalue

---------------------
os.environ['myname']=csj2
myname=csj2

os.environ['myname']=None

---------------------
os.environ['myname']=csj3
myname=csj3

os.environ['myname']=None
like image 133
mata Avatar answered Nov 15 '22 05:11

mata