Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updated environment variable but os.getenv() keeps returning None [closed]

I can't seem to get my code to respond to custom environmental variables so I wrote a piece of code to test it. getenv is not pulling the environmental variables that I've set in BASH into my python code. Any help you can provide will be very appreciated.

$ FRUSTRATION="PYTHON!!" $ echo FRUSTRATION PYTHON!! export FRUSTRATION ipython import os very_frustrated = os.getenv("FRUSTRATION") print(very_frustrated) None 
like image 922
aselya Avatar asked Nov 21 '16 19:11

aselya


People also ask

What is os Getenv ()?

Overview. The os. getenv() method is used to extract the value of the environment variable key if it exists. Otherwise, the default value will be returned. Note: The os module in Python provides an interface to interact with the operating system.

How do you use os Getenv in Python?

getenv() method in Python returns the value of the environment variable key if it exists otherwise returns the default value. default (optional) : string denoting the default value in case key does not exists. If omitted default is set to 'None'.

What does Getenv return?

The getenv() function returns a pointer to the string containing the value for the specified varname in the current environment. If getenv() cannot find the environment string, NULL is returned, and errno is set to indicate the error.

What does Getenv return in C?

Return Value The getenv() function returns a pointer to the c-string containing the value of the environment variable that corresponds to env_var . If no environment variable is found, it returns a null pointer . Environment variables are system-wide variables that are available to all processes in the system.


1 Answers

Works for me:

:: export FOO=boo :: python Python 2.7.10 (default, Jul 30 2016, 18:31:42) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> os.getenv('FOO') 'boo' >>> 

My guess is that you either forgot to export the variable, or you spelled the variable wrong.

like image 146
John Szakmeister Avatar answered Oct 04 '22 12:10

John Szakmeister