6

I need to set a environment variables in the python, and I try the commands bellow

import os
basepath = os.putenv('CPATH','/Users/cat/doc') 
basepath = os.getenv('CPATH','/Users/cat/doc') 

And when I print the varible, they are not set: print basepath None

What i'm doing wrong?

Rephrasing the question, I would like to create a base path based on a evironment variable. I'm testing this:

os.environ["CPATH"] = "/Users/cat/doc"
print os.environ["CPATH"]
base_path=os.getenv('C_PATH')

And when I try to print the basepath: print basepath it always return None

pgiecek
8,2805 gold badges41 silver badges48 bronze badges
asked Oct 1, 2014 at 16:32
4
  • What operating system? Commented Oct 1, 2014 at 16:46
  • OS X Mavericks (version 10.9) Commented Oct 1, 2014 at 16:56
  • Are you using Flask ? Commented Oct 1, 2014 at 17:06
  • @Ofiris: I'm having this problem and I am using Flask. os.getenv('PATH') == os.environ('PATH') == os.environ.get('PATH') == None. Got advice? Commented Dec 12, 2014 at 2:28

3 Answers 3

13

Try this one.

os.environ["CPATH"] = "/Users/cat/doc"

Python documentation is quite clear about it.

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

Based on the documentation, the os.getenv() is available on most flavors of Unix and Windows. OS X is not listed.

Use rather the snippet below to retrieve the value.

value = os.environ.get('CPATH')
answered Oct 1, 2014 at 16:37
3
  • In this way I can print the env variable but when I try to ascribe it to a base path, eg: base_path = os.getenv('CPATH') Commented Oct 1, 2014 at 16:42
  • @CatarinaCM, pardon? Modifying os.environ quite certainly does update the environment variables active for your script -- and base_path = os.environ.get('CPATH') [if you want None in the not-found case] is the best-practices approach to retrieval. If you're seeing contrary results, please provide a reproducer so we know how you're testing. Commented Oct 1, 2014 at 16:44
  • Use os.environ.get('CPATH') as @Charles suggested. Commented Oct 1, 2014 at 16:52
3

Use os.environ:

os.environ['CPATH'] = '/Users/cat/doc' 
print os.environ['CPATH'] # /Users/cat/doc
print os.environ.get('CPATH') # /Users/cat/doc

See the above link for more details:

If the platform supports the putenv() function, this mapping may be used to modify the environment as well as query the environment. putenv() will be called automatically when the mapping is modified.

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

answered Oct 1, 2014 at 16:38
0

It's a good practice to restore the environment variables at function completion. You may need something like the modified_environ context manager describe in this question to restore the environment variables.

Usage example:

with modified_environ(CPATH='/Users/cat/doc'):
 call_my_function()
answered May 10, 2017 at 20:15

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.