How to import a package from a blueprint?

172 views Asked by At

Imagine a simple Flask project implemented using the Blueprint pattern.

app
+-blueprint_1
|  +-__init__.py
|  +-views.py
|
+-utils.py

Now, imagine I want to import utils in my blueprint_1 views using the best practice here: http://docs.python-guide.org/en/latest/writing/structure/#modules

i.e., I'd like to simply do import foo rather than from foo import too_lazy, to_type, bunch_of, names

How can I do this style of imports in a Blueprint?

2

There are 2 answers

0
metmirr On BEST ANSWER

This may not the right way, it means from is good intention:

#blueprint_1/views.py
...
try:
    import os
    os.chdir('..')
    import utils
except:
    pass
0
HanSooloo On

Not sure how I missed the very obvious answer, but apparently I can do this:

# blueprint_1/views.py
from .. import utils