4

I'm getting ImportError: No module named when I try to deploy my app to heroku but the app builds fine locally. Here are the logs from Heroku

Traceback (most recent call last):
File "mr_meeseeks.py", line 4, in <module>
    import Helpers.Plugin_Handler as Plugin_Handler
File "/app/Helpers/Plugin_Handler.py", line 5, in <module>
    from Utils.constants import Plugin_Type
ImportError: No module named 'Utils.constants'

Here is my file structure: File Structure

As far as I can tell, Utils/constants.py exists. In case it's relevant, this is a SlackBot. The rest of the code can be found here.

TWOF
  • 388
  • 3
  • 14
  • I'm surprised it doesn't throw an error locally, Utils is not in the same module as Helpers. Could you remove all your `__pycache__` locally and try again? Also how do you know it's not failing locally? did you try the prod steps or are you saying so based on your tests. Speaking of which your tests aren't checked in :) – maininformer Jan 01 '17 at 02:26
  • @plumSemPy I don't have any tests yet sadly. I know it's not failing locally because I can run `python mr_meeseeks.py` and it won't crash like it did on Heroku. The Slackbot will also respond to messages as intended. The contents of my Procfile are `worker: python mr_meeseeks.py` so the two should be equivilent to the best of my knowlege. I removed the `__pycache__` files and it still didn't crash locally. Is that what you were asking? – TWOF Jan 01 '17 at 02:38
  • Yes, that import in your `Plugin_Handler` should not work, because Utils is not in the same module (directory) as `Plugin_Handler`, which makes me think something is cached. Could you also please try removing all your `.pyc` files? I am trying to reproduce your prod error locally – maininformer Jan 01 '17 at 02:43
  • Yup. Removed the .pyc files and reran it locally. Still working fine. – TWOF Jan 01 '17 at 02:59
  • see @noɥʇʎԀʎzɐɹƆ's answer below. Also you have a `utils` in your github that has the `constants` and then a `Utils` that has nothing. Two folders. – maininformer Jan 01 '17 at 03:04

2 Answers2

2

The Python Interpreter looks for modules under the $PYTHONPATH environment variable. It looks like you or your editor (my editor does this when I mark a directory as sources) root has added SlackBot/ to $PYTHONPATH.

I myself encountered this error when I marked a directory as sources root.

You have a few options:

Also, a note on style: python classes should be CamelCase and python modules should be lowercase_with_underscores. If you have an editor like PyCharm, your editor can fix these issues automatically, and more.

PEP 8 is the official python style guide, although I recommend using a linter so these issues can be automatically detected and fixed.

Community
  • 1
  • 1
noɥʇʎԀʎzɐɹƆ
  • 9,967
  • 2
  • 50
  • 67
0

In your Procfile, append Slackbot to PYTHONPATH, try adding

--pythonpath SlackBot

as a argument. This link address the exact same issue as well.

Jonathan Chow
  • 1,207
  • 1
  • 9
  • 11