0

I have a .sh script under directory /me/app/environment/env_param.sh

Which holds few variables(trying to export) as

export username = "uname"
export pw = "password"

. . .

And a python script under directory /me/dbconnect.py

So how do I import the env_param.sh in my dbconnect.py script and make use of 'username' and 'pw' variables(in dbconnect.py)?

Saleem Ali
  • 1,363
  • 11
  • 21
Vishnu kanth
  • 11
  • 1
  • 5
  • just call the .sh file before .py file – Shrey Nov 08 '19 at 09:06
  • You can use the `subprocess` module to run the `.sh` file, i.e. `subprocess.call(['./test.sh'])`. Then the `os` module to access the environment variables. – shynjax287 Nov 08 '19 at 09:08
  • @shynjax287 the environment of the python process and the sub process are not shared. It's one way inheritance during sub process setup. – dash-o Nov 08 '19 at 09:32

2 Answers2

1

Usually, you do no import shell script into python program, those are separate scripting engines, with different grammar, runtime-time conventions and environments.

Common setup include building a 'launcher' script - small wrapper around you 'py`

~/me/dbconnect.sh

source ~/app/environment/env_param.sh
~/me/dbconnect.py ...

Inside ~/me/dbconnect.py

   import os ;
   ...
   print(os.environ['username')) ;

Alternative 1:

You can also consider the alternative of converting the env_param.sh to a property file, where each line follows keyword=value format. In this setup, sh scripts can 'source' the param file (assuming no wild card, etc), and python program can parse the file into a a dictionary.

env.param:

user=my-user-id
password=my-pass

Using this solution will only support simple constants.

See: Python: How to create a dictionary from properties file while omitting comments on how to read property file into dictionary.

Alternative 2: If both options are not practical, consider opening the env_param.sh in python, and looking for lines matching 'export var=value', and loading them into dictionary. The down side is that any extension to the parameter file that will use bash/shell features will not work.

dash-o
  • 13,723
  • 1
  • 10
  • 37
0

Aside from the fact that the env_params.sh you posted is syntactically invalid, I assume that you have good reasons why you want to provide the parameters in a shell file, but want to read them from within Python. For doing this, I suggest that you create an auxiliary shell file, which sources your parameter file, and outputs all the parameters:

 #!/bin/sh
 # Auxiliary file
 . /path/to/env_params.sh
 echo username=$username
 echo pw=$pw
 ....

You run this script from your Python program, and parse its output, which - unless some variables expand to a string containing newlines - is then a simple set of NAME=VALUE lines, similar to an INI file.

Doing this means that you can use all shell features in your parameter file, such as parameter expansion and so an, and you can also use the parameter file in other shell scripts, if needed.

user1934428
  • 19,864
  • 7
  • 42
  • 87