-2

In order to declare a number of environment variables then call some python scripts using them, I create a myfile.sh file which is the on to run by bash myfile.sh . I have, however, plenty of scripts that should read these environment variables, but cannot create a myfile.sh for each one!

So my idea is to create an environment variable file and access it by each of my python scripts.

So my question is, how to access such a file with python 2.7 ?

A most relevant question is the following: Where does os.environ read the environment variables from?

It should be noted that I cannot install additional libraries such as dotenv. So a solution, if possible, should be based on standard libraries.

Any help would be mostly appreciated!

Matina G
  • 1,452
  • 2
  • 14
  • 28
  • Possible duplicate of [Emulating Bash 'source' in Python](https://stackoverflow.com/questions/3503719/emulating-bash-source-in-python) – Danil Aug 30 '18 at 10:10
  • 1
    Environment variables are by definition set by the environment. Python doesn't read them from any specific file, it reads them from *the environment*. That is set by whatever is executing the file. E.g. `export Foo="bar"; python baz.py`. – deceze Aug 30 '18 at 10:10
  • What have you tried so far? – bork Aug 30 '18 at 10:15
  • For one script, I can create a myfile.sh file to declare my environment variables and call specific script., then execute this from command line with bash myfile.sh But I do not know how to proceed since I have plenty of python scripts and cannot create a .sh file for each one. That is why I thought about an environment variable file to be read by each of my scripts – Matina G Aug 30 '18 at 10:21

2 Answers2

4

I have to create a new environment variables file that should be accessed by my script.

That's not how it works. What you need is to set the environment variables from the file BEFORE calling your script:

myenv.sh

export DB_USER="foo"
export DB_PWD="bar"
export DB_NAME="mydb"
# etc

myscript.py

import os

DB_USER = os.getenv("DB_USER")
DB_PWD = os.getenv("DB_PWD")
DB_NAME = os.getenv("DB_NAME")

# etc

Then

$ source /path/to/myenv.sh
$ python /path/to/myscript.py

Most often you will want to wrap the above two lines in a shell script to make your sysadmin's life easier.

EDIT: if you want those env vars to be "automagically" set, you can always source them from .bashrc or some similar place - but then this is a sysadmin question, not a programming one.

Now the question is: do you really need to use environment variables here ? You can as well use a python settings file - a plain Python module that just defines those variables, ie:

mysettings.py

DB_USER = "foo"
DB_PWD = "bar"
# etc

and make sure the path to the directory containing this script is in your $PYTHONPATH. Then your scripts only have to import it (like they import any other module), and you're done.

Icing on the cake: you can even mix both solutions, by having your settings module looking up environment variables and providing a default, ie:

mysettings.py

import os
DB_USER = os.getenv("DB_USER", "foo")
DB_PWD = os.getenv("DB_PWD", "bar")
# etc
bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
  • OK, this is close to what I had been doing. But where does this myenv.sh file need to be saved in order that the variables are read with os.getenv ? Also, I have plenty of files, so I wouldn't want to create a shell script for each one, if possible.. – Matina G Aug 30 '18 at 10:27
  • There's no special place where myenv.sh "should be saved in order that the variables are read with os.getenv()", what is NEEDED is that you _explicitely_ source this file - one way or another - before you run your python script. For per-user envvars, this is usually done in .profile or .bashrc but you mentionned (in a previous version of your question) you couldn't use those files. – bruno desthuilliers Aug 30 '18 at 10:54
  • Thanks a lot for your time (and an upvote) although it is not what I wanted (a config.py file is an option, or an xml or json file with the options, but I was asked to to it on bash and without modifying ~/.profile or ~/.bashrc .... Anyways, thanks a lot – Matina G Aug 30 '18 at 12:45
  • Well the only solution is to explicitely source your myenv.sh before executing the script (manually or using a wrapper). – bruno desthuilliers Aug 30 '18 at 13:47
  • Note that you could have one single wrapper bash script for all your python scripts - all you have to do is to pass the python script path as argument to the bash script, ie `$ mywrapper.sh path/to/myscript.py` – bruno desthuilliers Aug 30 '18 at 13:51
2

Show environment from commandline

  • linux: export
  • windows: set

Setting an environment variable

  • linux: export foo=bar
  • windows: set foo=bar

Printing env var:

  • linux: echo $foo
  • windows: echo %foo%
Michael D.
  • 1,795
  • 2
  • 18
  • 25