3

My project has the following structure:

server/ (root of project)
  |
  |--- __init__.py
  |--- requirements.txt
  |--- env/ (virtual environment)
  |--- app/ (main app folder)
        |--- __init__.py (defines a 'app = Flask(__name__)' object)
        |--- app.py (runs app on local server)
        |--- models.py
        |--- views.py

The way I import different modules in app.py on my local machine is do:

# /server/app/app.py

from server.app import app 
from server.app.models import *
from server.app.views import *

It works fine on my local machine (using PyCharm IDE, and the Python binary inside virtual environment folder /server/env/bin/.

However, when I push this to the production server running Ubuntu, where I install all dependencies globally, it keeps throwing the error no module named server.app when I run:

python server/app/app.py

Does anyone know why?

benjaminz
  • 3,118
  • 3
  • 35
  • 47
  • there is no server/app.py there is however server/app/app.py – Rash Jun 11 '15 at 17:19
  • @Rash thanks for the reminder, just corrected it. – benjaminz Jun 11 '15 at 17:25
  • 1
    in your IDE environment, they edit your python source path automatically. You need to add your path to the python path. Use this sys.path.append('/path/to/the/example_file.py') – Rash Jun 11 '15 at 17:29
  • Hmm. Unfortunately when you guys mentioned me using @ myusername, I did not get the notification. Interesting. maybe because you added apostrophe after my username – Rash Jun 11 '15 at 18:23
  • @Rash we are mentioning you out of this comment area `scope`, maybe that is why. – benjaminz Jun 11 '15 at 18:25

2 Answers2

1

Any IDE environment usually sets the pythonpath. E.g. in eclipse right click your project and see the properties. You will see that your main project is listed in the pythonpath. This path is used to locate modules.

Now in your production code you are not in your IDE. So normal python interpreter cannot find your path. Hence you need to specify this.

One way is to add sys.path.append('/path/to/the/project') before you do your import (This should be done for the first script that got executed, in this case app.py, that way you only need to do this once).

You can also add your path permanently to your production environment. See this post.

Community
  • 1
  • 1
Rash
  • 7,677
  • 1
  • 53
  • 74
0

As @Rash mentionned, your IDE very probably add the directory containing /server to your python path. You can check this in your app.py by adding

import sys
print "\n".join(sys.path)

before your imports.

When you're manually running your app, ie python server/app/app.py, the parent of your server directory is obviously not in your python path, so you have to add yourself.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
  • If I were to use @Rash's suggestion `sys.path.append('/path/to/the/example_file.py')`, which file do I put this code in? Thanks! – benjaminz Jun 11 '15 at 17:57
  • Messing with `sys.path` is more often than not a bad idea. It's better to update your `PYTHONPATH` environment variable (the one of the user that will run your app). While we're at it, it's also way safer to use a virtualenv in production... – bruno desthuilliers Jun 12 '15 at 09:45