简体   繁体   中英

Calling python script from a Bash script

I'm trying to call a python script from a bash script. I get import errors only if I try to run the .py from the bash script. If I run with python myscript.py everything is fine. This is my bash script:

while true; do
  python script.py

  echo "Restarting...";
  sleep 3;
done

The error I get:

Traceback (most recent call last):
  File "script.py", line 39, in <module>
    from pokemongo_bot import logger
  File "/Users/Paolo/Downloads/folder/t/__init__.py", line 4, in <module>
    import googlemaps
ImportError: No module named googlemaps

There is more to this story that isn't in your question. Your PYTHONPATH variable is getting confused somewhere along the way.
Insert a couple quick test lines:

in bash:

echo $PYTHONPATH

in your python:

import os
print os.environ["PYTHONPATH"]

At some point, the path to googlemaps got lost.

Your problem is in the script itself, your bash code is OK!. If you don't have problem running python scrip.py from bash directly, you should test if you use the same interpreter for both calls. You can check the shebang line in the python script, it is the first line in the file for example #!/usr/bin/env python or #!/usr/bin/python and compare it to the output of which python command if the output is different try to change or add the shebang line in to the file. If you call directly file in bash ./some_script.py bash reads the first line and if it is shebang line he wil execute the specific command for the file. My point is that if you use two diferent interpreters for calling file directly with python script.py and indirectly ./script.py one of them may not have the proper python modules.

Howto code:

$ which python
/usr/local/bin/python

So the second line is the path for your interpreter to build a shebang from it write in the first line of your script file this.

#!/usr/local/bin/python

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