简体   繁体   中英

Trying to get a file name or directory from a Python3 global class and then execute that using bash script

  1. I have a globals.py file that I would like to store pertinent info for the entire system like directory locations, paths to python3 services/scripts etc.
  2. I start these services on boot up using a bash shell script, however I need to retrieve the service locations from my global class enumerations.

I keep getting shell ERROR: python3: can't open file 'path/to/service.py': [Errno 2] No such file or directory

**globals.py** 

#contains a dictionary:
SERVICE_MAP = {
    'test': '~/path/to/service.py',
    'test2': '$HOME/path/to/service.py',
    'test3': '/full/path/to/service.py',
}

I have tried all the above forms of paths to the services. and the method of retrieval is:

**launch.sh**

function get_service() {
   python3 -c "import common.globals as gbl; print(gbl.SERVICE_MAP['$1'])"
}

And then call:

**launch.sh**

service=$(get_service "test")  # get service "test"
echo $service  #Output: is what it should be... "path/to/service.py"

# but when I now do:

python3 $service
#or
python3 "$service"

Output is always:
python3: can't open file '/path/to/service.py': [Errno 2] No such file or directory

What am I doing wrong here?

Instead of using the full path in the globals.py I put the relative path:

SERVICE_MAP = {
    'test': '/rel/path/to/service.py',
}

And in my launch.sh script I prepend $HOME :

function get_service() {
   python3 -c "import common.globals as gbl; print(gbl.SERVICE_MAP['$1'])"
}
python3 "$HOME$(get_service "test")"

I like this best because the $HOME user may change someday... Answered my own question, thanks for looking!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM