1

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
Lex Scarisbrick
  • 1,540
  • 1
  • 24
  • 31
Levenlol
  • 305
  • 5
  • 17
  • 1
    I think the reason why you are getting downvotes is because the error has to do with your script.py not having a module named googlemaps when you try to `import googlemaps`. – Ali Jul 29 '16 at 18:15
  • See: http://stackoverflow.com/questions/7332299/trace-python-imports – jmunsch Jul 29 '16 at 18:22
  • Also using `virtualenv` or `venv` is generally really helpful too. – jmunsch Jul 29 '16 at 18:23
  • See: http://stackoverflow.com/questions/5844869/comprehensive-beginners-virtualenv-tutorial – jmunsch Jul 29 '16 at 18:24
  • The question is more about how to debug module imports in python than it is about how to exec python using bash. As you already have that. – jmunsch Jul 29 '16 at 18:25
  • actually i used virtaulenv, but if i run the script.py from outside the script bash it works fine – Levenlol Jul 29 '16 at 18:27

2 Answers2

3

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.

Paul Cuddihy
  • 477
  • 6
  • 12
1

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
filvarga
  • 56
  • 3