3

I am running a python script which imports custom functions. My script is named update_wx.py and is located in the weather directory. When I run this from my linux command line as python weather/update_wx.py, it runs fine. However, when I run it as part of a bash script, I receive the following python error:

ModuleNotFoundError: No module named 'weather'

The line within update_wx.py which is causing this error is

from weather.nasa.nasa import import_wx_data

For context, nasa.py is a script within the nasa directory, which is inside the weather directory. As I said, this works when running from the command line. I have checked the file permissions for all files and directories, changed the working directory within the update_wx.py script, and edited the python path in my bash script, all to no avail.

How can I import this python function when running scripts via bash?

Thanks for your help.

Mark York
  • 33
  • 1
  • 6
  • 1
    Do you have an ____init____.py file in side each folder representing a module ? – NotARobot May 25 '17 at 21:24
  • If your `nasa.py` script is not in the same folder as your `update_wx.py` script, then python cannot find the script. You have several options, depending on how your directory tree is organised. See [this](https://stackoverflow.com/questions/714063/importing-modules-from-parent-folder) question on how to import correctly. – Thomas Kühn May 25 '17 at 21:52
  • Possible duplicate of [Importing modules from parent folder](https://stackoverflow.com/questions/714063/importing-modules-from-parent-folder) – Thomas Kühn May 25 '17 at 21:53
  • @TonyDasilva Good question - all directories have an __init__.py file. Recall that the script runs with python from the command line, but not from the bash script – Mark York May 26 '17 at 16:29
  • Do you have multiple versions of Python installed? – NotARobot May 26 '17 at 16:52

1 Answers1

0

Assuming that your weather module is stored outside of the interpreter, I think the problem is that the interpreter doesn't know about the module, it only knows about the .py file being executing and that you need to relatively let the interpreter know how to navigate to the parent either by using " ... " like

from .weather.nasa.nasa import weather_wx_data

See Relative imports for the billionth time

or that you have multiple interprerters installed and you need to call the interpreter with an absolute path like

/root/python27/python.exe "/c/weather/update_wx.py"
NotARobot
  • 978
  • 2
  • 14
  • 36