1

I've got the following project structure:enter image description here

When I run main.py from my IDE everything seems to work fine.

When I try and run main.py from the terminal I get the following error: enter image description here

I have absolutely no idea why this is happening.

I thought that since I'm running it from the SmartHome1 directory then the import of from SpeechRecognition.TextToSpeech ... should work (since the import is correct relative to the project root). Why then am I getting this error?

At first I thought the weather.py might "think" that the cwd is different than the project root. So I checked it using os.getcwd() and it showed that it is the root. I'm stumped.

So what am I doing wrong here?

Edit: Now when I think about it, it probably has to do with the fact that weather.py is run as a standalone script using subprocess.Popen. I'm still not sure how to fix this though.

matanc1
  • 6,525
  • 6
  • 37
  • 57
  • 1
    Does http://stackoverflow.com/questions/11536764/attempted-relative-import-in-non-package-even-with-init-py answer your question? – ThiefMaster Jun 06 '14 at 22:19
  • I'm checking it right now. I've also updated my question a bit at the bottom. I'm pretty sure that me calling `weather.py` using `subprocess.Popen` as a new python instance is the problem. – matanc1 Jun 06 '14 at 22:25
  • `Popen` uses a global python binary and it does see your packages in either Virtualenv or (without Virtualenv) in your project as it's not registered in `PYTHONPATH`. – yedpodtrzitko Jun 06 '14 at 22:29
  • does SpeachRecognition.__init__.py import TextToSpeach? – tdelaney Jun 06 '14 at 22:29
  • @tdelaney - no it does not. Should it? – matanc1 Jun 06 '14 at 22:30
  • @ThiefMaster - ihm's solution seemed to work. From what I understand that just adds to the pythonpath the `../../` path. This seems more like a workaround than a proper way to do it though. – matanc1 Jun 06 '14 at 22:47
  • I meant the `python -m ....` solution from the accepted answer instead of the sys.path hack. – ThiefMaster Jun 06 '14 at 22:51
  • @ThiefMaster - I tried running `python -m main` but I got the same result. edit: without the `.py` – matanc1 Jun 06 '14 at 22:51
  • @Shookie - I *thought* it had to but after a quick test, turns out you shouldn't need to import it (which I find strange... but thats just me). – tdelaney Jun 06 '14 at 23:28

0 Answers0