librelist archives

« back to archive

Circular Imports

Circular Imports

From:
Joe Greenawalt
Date:
2013-03-30 @ 00:20
Reading this: http://flask.pocoo.org/docs/patterns/packages/

It says  that its alright to have circular imports in this case.

I have the following:

in my webapp/__init__.py:
*app = Flask(__name__, static_path='/static')
import webapp.views.base*

---continue on down, app loads fine because when i do this:
*app.add_url_rule('/', view_func=views.base.welcome)
app.add_url_rule('/register', view_func=views.base.register,
methods=['GET', 'POST'])*

it works fine.


in webapp.views i have this:
*from webapp import app

@app.route('/')
def welcome():
*

however, i get the following error:
*  File "/Users/.../webapp/views/base.py", line 6, in <module>
    from webapp import app
ImportError: cannot import name app
*

Which i'm assuming is because of the circular imports.

Does anyone have any ideas what else to try?
I have a workaround, but I'd much prefer suing the decorator.

Thanks,

Joe

Re: [flask] Circular Imports

From:
Teo Klestrup Röijezon
Date:
2013-03-30 @ 01:21
You're correct about the error message, circular imports in Python
essentially break execution of the first module at the import. Depending on
context, this can either fail silently (or much later) or cause relatively
undecipherable error messages like the one you just experienced.

Personally I use blueprints <http://flask.pocoo.org/docs/blueprints/> for
this. They allow you to use the decorator syntax without causing
problematic circular imports, although you still have to add an import and
registration to the __init__.py (which I'd say is good, since it gives you
a clear thread to follow when you need to find how something works later
on).


On 30 March 2013 01:20, Joe Greenawalt <joe.greenawalt@gmail.com> wrote:

> Reading this: http://flask.pocoo.org/docs/patterns/packages/
>
> It says  that its alright to have circular imports in this case.
>
>
> I have the following:
>
> in my webapp/__init__.py:
> *app = Flask(__name__, static_path='/static')
> import webapp.views.base*
>
> ---continue on down, app loads fine because when i do this:
> *app.add_url_rule('/', view_func=views.base.welcome)
> app.add_url_rule('/register', view_func=views.base.register, 
methods=['GET', 'POST'])*
>
> it works fine.
>
>
> in webapp.views i have this:
> *from webapp import app
>
> @app.route('/')
> def welcome():
> *
>
> however, i get the following error:
> *  File "/Users/.../webapp/views/base.py", line 6, in <module>
>     from webapp import app
> ImportError: cannot import name app
> *
>
> Which i'm assuming is because of the circular imports.
>
>
> Does anyone have any ideas what else to try?
> I have a workaround, but I'd much prefer suing the decorator.
>
> Thanks,
>
> Joe
>
>

Re: [flask] Circular Imports

From:
greatghoul
Date:
2013-03-30 @ 01:17
> from webapp import app

Change it to

> from flask import current_app as app

On Sat, Mar 30, 2013 at 8:20 AM, Joe Greenawalt <joe.greenawalt@gmail.com>wrote:

> from webapp import app





-- 
 *greatghoul <http://www.g2w.me> - Ask and Learn!*

Re: [flask] Circular Imports

From:
Teo Klestrup Röijezon
Date:
2013-03-30 @ 01:22
This does not work; current_app is only for use from *within* the request
context (the view), not at setup time.


On 30 March 2013 02:17, greatghoul <greatghoul@gmail.com> wrote:

> > from webapp import app
>
> Change it to
>
> > from flask import current_app as app
>
> On Sat, Mar 30, 2013 at 8:20 AM, Joe Greenawalt <joe.greenawalt@gmail.com>wrote:
>
>> from webapp import app
>
>
>
>
>
> --
>  *greatghoul <http://www.g2w.me> - Ask and Learn!*
>

Re: [flask] Circular Imports

From:
Joe Greenawalt
Date:
2013-03-30 @ 02:50
I just tried, the "from flask import current_app as app", and got the
conext error.

It looks like blueprints is the best answer, if i want to break those views
out.

Thanks everyone for the input.

Joe


On Fri, Mar 29, 2013 at 9:22 PM, Teo Klestrup Röijezon <teo@nullable.se>wrote:

> This does not work; current_app is only for use from *within* the request
> context (the view), not at setup time.
>
>
> On 30 March 2013 02:17, greatghoul <greatghoul@gmail.com> wrote:
>
>> > from webapp import app
>>
>> Change it to
>>
>> > from flask import current_app as app
>>
>> On Sat, Mar 30, 2013 at 8:20 AM, Joe Greenawalt <joe.greenawalt@gmail.com
>> > wrote:
>>
>>> from webapp import app
>>
>>
>>
>>
>>
>> --
>>  *greatghoul <http://www.g2w.me> - Ask and Learn!*
>>
>
>

Re: [flask] Circular Imports

From:
Nguyễn Hồng Quân
Date:
2013-03-31 @ 02:41
Hi,

I reserved a file common.py for defining app, db v.v...
In__init__.py, I import app from common.py
In views.py, I import app from common.py

So I can avoid circular import.


2013/3/30 Joe Greenawalt <joe.greenawalt@gmail.com>

> I just tried, the "from flask import current_app as app", and got the
> conext error.
>
> It looks like blueprints is the best answer, if i want to break those
> views out.
>
> Thanks everyone for the input.
>
> Joe
>
>
> On Fri, Mar 29, 2013 at 9:22 PM, Teo Klestrup Röijezon <teo@nullable.se>wrote:
>
>> This does not work; current_app is only for use from *within* the
>> request context (the view), not at setup time.
>>
>>
>> On 30 March 2013 02:17, greatghoul <greatghoul@gmail.com> wrote:
>>
>>> > from webapp import app
>>>
>>> Change it to
>>>
>>> > from flask import current_app as app
>>>
>>> On Sat, Mar 30, 2013 at 8:20 AM, Joe Greenawalt <
>>> joe.greenawalt@gmail.com> wrote:
>>>
>>>> from webapp import app
>>>
>>>
>>>
>>>
>>>
>>> --
>>>  *greatghoul <http://www.g2w.me> - Ask and Learn!*
>>>
>>
>>
>


-- 
***********************************************
* Nguyễn Hồng Quân                            *
* Y!M: ng_hquan_vn                            *
* Identi.ca: hongquan                         *
***********************************************

Re: [flask] Circular Imports

From:
Joe Greenawalt
Date:
2013-03-31 @ 03:04
You know, I tried that too, in a utils file.  but I got an error right off
the bat, and I quickly moved on.  Can't remember why it failed but it
seemed state oriented.  Maybe i should relook at that.

Thanks,
Joe


On Sat, Mar 30, 2013 at 10:41 PM, Nguyễn Hồng Quân
<ng.hong.quan@gmail.com>wrote:

> Hi,
>
> I reserved a file common.py for defining app, db v.v...
> In__init__.py, I import app from common.py
> In views.py, I import app from common.py
>
> So I can avoid circular import.
>
>
> 2013/3/30 Joe Greenawalt <joe.greenawalt@gmail.com>
>
>> I just tried, the "from flask import current_app as app", and got the
>> conext error.
>>
>> It looks like blueprints is the best answer, if i want to break those
>> views out.
>>
>> Thanks everyone for the input.
>>
>> Joe
>>
>>
>> On Fri, Mar 29, 2013 at 9:22 PM, Teo Klestrup Röijezon <teo@nullable.se>wrote:
>>
>>> This does not work; current_app is only for use from *within* the
>>> request context (the view), not at setup time.
>>>
>>>
>>> On 30 March 2013 02:17, greatghoul <greatghoul@gmail.com> wrote:
>>>
>>>> > from webapp import app
>>>>
>>>> Change it to
>>>>
>>>> > from flask import current_app as app
>>>>
>>>> On Sat, Mar 30, 2013 at 8:20 AM, Joe Greenawalt <
>>>> joe.greenawalt@gmail.com> wrote:
>>>>
>>>>> from webapp import app
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> --
>>>>  *greatghoul <http://www.g2w.me> - Ask and Learn!*
>>>>
>>>
>>>
>>
>
>
> --
> ***********************************************
> * Nguyễn Hồng Quân                            *
> * Y!M: ng_hquan_vn                            *
> * Identi.ca: hongquan                         *
> ***********************************************
>

Re: [flask] Circular Imports

From:
Smartboy
Date:
2013-03-31 @ 04:05
Make sure that you aren't accessing the resources from both sides of the
circular import. I ran into a problem where I created a resource in
mod1.py, imported a resource from mod2.py, which needed to access the
resource from mod1, etc. It went downhill quickly, and I resolved it by
making sure that resource access only went one way (in this case, mod2.py
imported and used the resources from mod1.py, but mod1.py did not use the
resources from mod2.py), even though it meant refactoring of my code.

Smartboy

On Sat, Mar 30, 2013 at 8:04 PM, Joe Greenawalt <joe.greenawalt@gmail.com>wrote:

> You know, I tried that too, in a utils file.  but I got an error right off
> the bat, and I quickly moved on.  Can't remember why it failed but it
> seemed state oriented.  Maybe i should relook at that.
>
> Thanks,
> Joe
>
>
> On Sat, Mar 30, 2013 at 10:41 PM, Nguyễn Hồng Quân <ng.hong.quan@gmail.com
> > wrote:
>
>> Hi,
>>
>> I reserved a file common.py for defining app, db v.v...
>> In__init__.py, I import app from common.py
>> In views.py, I import app from common.py
>>
>> So I can avoid circular import.
>>
>>
>> 2013/3/30 Joe Greenawalt <joe.greenawalt@gmail.com>
>>
>>> I just tried, the "from flask import current_app as app", and got the
>>> conext error.
>>>
>>> It looks like blueprints is the best answer, if i want to break those
>>> views out.
>>>
>>> Thanks everyone for the input.
>>>
>>> Joe
>>>
>>>
>>> On Fri, Mar 29, 2013 at 9:22 PM, Teo Klestrup Röijezon <teo@nullable.se>wrote:
>>>
>>>> This does not work; current_app is only for use from *within* the
>>>> request context (the view), not at setup time.
>>>>
>>>>
>>>> On 30 March 2013 02:17, greatghoul <greatghoul@gmail.com> wrote:
>>>>
>>>>> > from webapp import app
>>>>>
>>>>> Change it to
>>>>>
>>>>> > from flask import current_app as app
>>>>>
>>>>> On Sat, Mar 30, 2013 at 8:20 AM, Joe Greenawalt <
>>>>> joe.greenawalt@gmail.com> wrote:
>>>>>
>>>>>> from webapp import app
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>>  *greatghoul <http://www.g2w.me> - Ask and Learn!*
>>>>>
>>>>
>>>>
>>>
>>
>>
>> --
>> ***********************************************
>> * Nguyễn Hồng Quân                            *
>> * Y!M: ng_hquan_vn                            *
>> * Identi.ca: hongquan                         *
>> ***********************************************
>>
>
>