librelist archives

« back to archive

Trying to split a project into multiple files

Trying to split a project into multiple files

From:
Faryus
Date:
2015-03-12 @ 11:56
Hello everyone,

I'm trying to split up a flask project into several files, following the 
material here: http://flask.pocoo.org/docs/0.10/patterns/packages/

In my __init__.py file I have this:

    from flask import Flask
    import planck.db
    planck = Flask(__name__)

    """ some code here """


In my db.py file, I have the following:

    from planck import planck

    """ some code here """


The directory structure is

    *$ pwd*
    /var/www/flask

    *$tree*
    |-- planck
    |   |-- db.py
    |   |-- __init__.py
    |   |-- planck.db
    |   |-- static
    |   `-- templates
    |-- planck.wsgi
    `-- run.py


And my run.py is:

    from planck import planck
    planck.run(debug=True, host="0.0.0.0")


However when I execute run.py I get this error:

    *$ python run.py*
    Traceback (most recent call last):
       File "run.py", line 1, in <module>
         from planck import planck
       File "/var/www/flask/planck/__init__.py", line 9, in <module>
         import db
       File "/var/www/flask/planck/db.py", line 1, in <module>
         from planck import planck
    ImportError: cannot import name planck

What am I doing wrong here?

Thanks!

Re: [flask] Trying to split a project into multiple files

From:
steve
Date:
2015-03-12 @ 19:23
Very curious as to the difference between the "good" and "bad". Does 
anyone have a simple explanation?Thanks!

Date: Thu, 12 Mar 2015 14:31:31 +0200
From: faryus@cock.li
Subject: Re: [flask] Trying to split a project into multiple files
To: flask@librelist.com


  
    
  
  
    Thanks to Tomek I realized that my code was a bit
      wrong.

      The import needs to be done after the app name declaration.

      

      Good:

    
    planck = Flask(__name__)

      from planck import db

    
    

      Bad:

    
    from planck import db

      planck = Flask(__name__)

    
    

    Cheers!

    

    On 12.03.2015 14:22, Tomek Sz. wrote:

    
    
      
      Hi!

      

      I've faced same problem some time ago and found two approaches.

      

      1.
      
http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world

      Thus having (__init__ in main app folder) you are able to use app
      and db (or however you call it:) in any module.

      Remember about something called "circular import" - order of
      imports matters.

      

      2. https://github.com/mitsuhiko/flask/wiki/Large-app-how-to

      
https://www.digitalocean.com/community/tutorials/how-to-structure-large-flask-applications

      

      In some sense, combine 1&2 and use Blueprints - have access to
      db variable, but attach
      
      components using Blueprints.

      

      Disclaimer: I am new in the Flask world, these are only results of
      my research that I implemented... but it works as expected:).

      

      Cheers,

      TSz

      

      On 03/12/2015 12:56 PM, Faryus wrote:

      
      
        
        Hello everyone,

          

          I'm trying to split up a flask project into several files,
          following the material here: 
http://flask.pocoo.org/docs/0.10/patterns/packages/

          

          In my __init__.py file I have this:

        
        from flask import Flask

          import planck.db

          planck = Flask(__name__)

          

          """ some code here """

        
        

          In my db.py file, I have the following:

        
        from planck import planck

            

          """ some code here """

        
        

          The directory structure is

        
        $ pwd

          /var/www/flask

          

          $tree

          |-- planck

          |   |-- db.py

          |   |-- __init__.py

          |   |-- planck.db

          |   |-- static

          |   `-- templates

          |-- planck.wsgi

          `-- run.py

        
        

          And my run.py is:

        
        from planck import planck

          planck.run(debug=True, host="0.0.0.0")

        
        

          However when I execute run.py I get this error:

        
        $ python run.py

            Traceback (most recent call last):

            File "run.py", line 1, in <module>

              from planck import planck

            File "/var/www/flask/planck/__init__.py", line 9,
            in <module>

              import db

            File "/var/www/flask/planck/db.py", line 1, in
            <module>

              from planck import planck

          ImportError: cannot import name planck

        
        What am I doing wrong here?

          

          Thanks!

         
      

    
    
 		 	   		  

Re: [flask] Trying to split a project into multiple files

From:
Tomek Sz.
Date:
2015-03-12 @ 12:22
Hi!

I've faced same problem some time ago and found two approaches.

1. 
http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
Thus having (__init__ in main app folder) you are able to use app and db 
(or however you call it:) in any module.
Remember about something called "circular import" - order of imports 
matters.

2. https://github.com/mitsuhiko/flask/wiki/Large-app-how-to

https://www.digitalocean.com/community/tutorials/how-to-structure-large-flask-applications

In some sense, combine 1&2 and use Blueprints - have access to db 
variable, but attach components using Blueprints.

Disclaimer: I am new in the Flask world, these are only results of my 
research that I implemented... but it works as expected:).

Cheers,
TSz

On 03/12/2015 12:56 PM, Faryus wrote:
> Hello everyone,
>
> I'm trying to split up a flask project into several files, following 
> the material here: http://flask.pocoo.org/docs/0.10/patterns/packages/
>
> In my __init__.py file I have this:
>
>     from flask import Flask
>     import planck.db
>     planck = Flask(__name__)
>
>     """ some code here """
>
>
> In my db.py file, I have the following:
>
>     from planck import planck
>
>     """ some code here """
>
>
> The directory structure is
>
>     *$ pwd*
>     /var/www/flask
>
>     *$tree*
>     |-- planck
>     |   |-- db.py
>     |   |-- __init__.py
>     |   |-- planck.db
>     |   |-- static
>     |   `-- templates
>     |-- planck.wsgi
>     `-- run.py
>
>
> And my run.py is:
>
>     from planck import planck
>     planck.run(debug=True, host="0.0.0.0")
>
>
> However when I execute run.py I get this error:
>
>     *$ python run.py*
>     Traceback (most recent call last):
>       File "run.py", line 1, in <module>
>         from planck import planck
>       File "/var/www/flask/planck/__init__.py", line 9, in <module>
>         import db
>       File "/var/www/flask/planck/db.py", line 1, in <module>
>         from planck import planck
>     ImportError: cannot import name planck
>
> What am I doing wrong here?
>
> Thanks!

Re: [flask] Trying to split a project into multiple files

From:
Faryus
Date:
2015-03-12 @ 12:31
Thanks to Tomek I realized that my code was a bit wrong.
The import needs to be done after the app name declaration.

Good:

    planck = Flask(__name__)
    from planck import db


Bad:

    from planck import db
    planck = Flask(__name__)


Cheers!

On 12.03.2015 14:22, Tomek Sz. wrote:
> Hi!
>
> I've faced same problem some time ago and found two approaches.
>
> 1. 
> http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
> Thus having (__init__ in main app folder) you are able to use app and 
> db (or however you call it:) in any module.
> Remember about something called "circular import" - order of imports 
> matters.
>
> 2. https://github.com/mitsuhiko/flask/wiki/Large-app-how-to
> 
https://www.digitalocean.com/community/tutorials/how-to-structure-large-flask-applications
>
> In some sense, combine 1&2 and use Blueprints - have access to db 
> variable, but attach components using Blueprints.
>
> Disclaimer: I am new in the Flask world, these are only results of my 
> research that I implemented... but it works as expected:).
>
> Cheers,
> TSz
>
> On 03/12/2015 12:56 PM, Faryus wrote:
>> Hello everyone,
>>
>> I'm trying to split up a flask project into several files, following 
>> the material here: http://flask.pocoo.org/docs/0.10/patterns/packages/
>>
>> In my __init__.py file I have this:
>>
>>     from flask import Flask
>>     import planck.db
>>     planck = Flask(__name__)
>>
>>     """ some code here """
>>
>>
>> In my db.py file, I have the following:
>>
>>     from planck import planck
>>
>>     """ some code here """
>>
>>
>> The directory structure is
>>
>>     *$ pwd*
>>     /var/www/flask
>>
>>     *$tree*
>>     |-- planck
>>     |   |-- db.py
>>     |   |-- __init__.py
>>     |   |-- planck.db
>>     |   |-- static
>>     |   `-- templates
>>     |-- planck.wsgi
>>     `-- run.py
>>
>>
>> And my run.py is:
>>
>>     from planck import planck
>>     planck.run(debug=True, host="0.0.0.0")
>>
>>
>> However when I execute run.py I get this error:
>>
>>     *$ python run.py*
>>     Traceback (most recent call last):
>>       File "run.py", line 1, in <module>
>>         from planck import planck
>>       File "/var/www/flask/planck/__init__.py", line 9, in <module>
>>         import db
>>       File "/var/www/flask/planck/db.py", line 1, in <module>
>>         from planck import planck
>>     ImportError: cannot import name planck
>>
>> What am I doing wrong here?
>>
>> Thanks!
>