tags:

views:

83

answers:

2

Looks like I am having a real tough day with python imports.I am using Flask and am trying to organise my app structure.I am using it on GAE and thus have to put python packages in my app itself. It looks something like this below:-

-MyFolder
  -flask
  -werkzeug
  -Myapp
    - __init__.py
    -templates
    -static
    -views.py
  -blinker

As of now I import the blinker library into Myapp's __init__. But I wanted to organise these extra packages like blinker into a helper package so as to look like this.

-helper
 -__init__.py
 -blinker

(blinker's __init__.py file looks like this)

from blinker.base import.....

But when I try importing blinker into Myapp's __init__ using

from helper import blinker

I get an import error saying no module named blinker.base.Why should that happen. Looks like it looks for a blinker package outside of the current one. Why should it happen?

A: 

Smells like you want to be using a relative import.

from .base import ...
Ignacio Vazquez-Abrams
Note that this only works with Python 2.5 and up
WoLpH
Should be fine, since App Engine is the target platform and it's using Python 2.5.
Will McCutchen
That is true Ignacio,WoLpH and Will. Even "from base import ..." will work, but then I would have to do those changes in several imports in the blinker package. That defeats the purpose. Cant there be something like site packages and the imports happen from there. I am not sure I am clear enough here.
Alice
Yes. They're called relative imports.
Ignacio Vazquez-Abrams
+1  A: 

sys.path.append could also fit your purpose.

ultimatebuster
You almost never want to add a subdirectory of an existing entry to `sys.path`. This can result in hard-to-debug issues.
Ignacio Vazquez-Abrams
Since she's on GAE and can't use pip, `sys.path.append('helper')` looks like the only solution even if there might be future debug issues.
Thierry Lam
Worked perfectly for me
Alice
I do add subdirectory to sys.path and never had a real problem. On the other hand, I can't seems to work the from .parent import ...
ultimatebuster