6

In the Ubuntu bash shell, I can set the environment variable from the .env file by using command lines "source" (example: source $OCSSWROOT/OCSSW_bash.env).

But source is not a function in python. So, how to set an environment variable from the .env file in python?

'l2gen'is a command line program of SeaDAS which be supported by NASA for processing ocean satellite data. I can run it in the bash shell. now, the more program needs to be coded with python script and 'l2gen' is one of program.

But the environment variable needs to be set again in python script. According to the google search result, I fond some method to set the environment variable in python. But lacking information about the .env file.

in the Linux bash shell, I set the environment variable by vim and source:

 vim .profile
 export OCSSWROOT=[SeaDAS_install_dir]/ocssw (adding this in the profile 
 file and then save/exit)
 source $OCSSWROOT/OCSSW_bash.env

where: SeaDAS_install_dir is the directory where I installed software SeaDAS.

I try to add the environment variable as following:

import subprocess
if __name__=='__main__':
 l2cmdtest = 'l2gen -h'
 new_env = os.environ.copy()
 new_env['OCSSWROOT'] = '/usr/local/seadas-7.5.3/ocssw'
 new_env['OCSSWROOT'] = 'OCSSWROOT/OCSSW_bash.env'
 eturnCodetest = subprocess.call(l2cmdtest, shell=True)

where: 'l2gen -h' is a program which can work in the bash shell.

executing python script, and then an error:

/bin/sh: 1: l2gen: not found
Mureinik
316k54 gold badges392 silver badges403 bronze badges
asked Jun 10, 2019 at 4:52
1
  • l2gen: not found seems like the real problem, not setting environmental variables. Maybe you should use the full path to l2gen in l2cmdtest. You can find the full pathname with command -v l2gen in Bash shell. Commented Jun 10, 2019 at 5:01

1 Answer 1

7

The python-dotenv package can do the heavy lifting for you:

from dotenv import load_dotenv
load_dotenv(dotenv_path='OCSSW_bash.env')
answered Jun 10, 2019 at 5:36
Sign up to request clarification or add additional context in comments.

3 Comments

thanks. But how to link an environment variable to .env file?
@KevinLee I'm not sure I understand the question. You edit the .env file with the variables you need (e.g., MYVAR=123), and use load_dotenv to load this file. It places all entries there as environment variables, and any subsequent part of your code that relies on environment variables will be able to use them.
thanks very much. I solved this problem by as following code. commands = 'source $OCSSWROOT/OCSSW_bash.env;' +l2gencmd1 process=subprocess.Popen(commands,executable='/bin/bash',stdin=subprocess.PIPE,stdout=subprocess.PIPE, shell=True) out, err = process.communicate() where commands consists of two parts. 'source $OCSSWROOT/OCSSW_bash.env;' is the setting environment variable.

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.