librelist archives

« back to archive

Import problem with Python 3 and __main__.py

Import problem with Python 3 and __main__.py

From:
Jon Ribbens
Date:
2014-08-27 @ 11:31
If I have an utterly minimal Flask app using the suggested layout:

    runflaskapp.py
    flaskapp/
        __init__.py

runflaskapp.py contains:

    from flaskapp import app
    app.run(debug=True)

flaskapp/__init__.py contains:

    from flask import Flask
    app = Flask(__name__)

Running this with 'python3 runflaskapp.py' works fine. However it
seems to me that a more Python3onic way of doing this would be to
rename 'runflaskapp.py' as 'flaskapp/__main__.py' and then run
the whole thing as 'python3 -m flaskapp'. Unfortunately this doesn't
work:

    $ python3 -m flaskapp
     * Running on http://127.0.0.1:5000/
     * Restarting with reloader
    Traceback (most recent call last):
      File "/home/username/src/flaskapp/__main__.py", line 1, in <module>
        from flaskapp import app
    ImportError: No module named 'flaskapp'

Does anyone know why and how to fix it?

Re: [flask] Import problem with Python 3 and __main__.py

From:
Daniel Neuhäuser
Date:
2014-08-27 @ 12:18
__main__.py behaves weirdly when it comes to sys.path, you probably have 
to adjust that, at least during development when your package isn't in 
site-packages. 

Re: [flask] Import problem with Python 3 and __main__.py

From:
Jon Ribbens
Date:
2014-08-27 @ 13:29
On Wed, Aug 27, 2014 at 02:18:48PM +0200, Daniel Neuhäuser wrote:
> __main__.py behaves weirdly when it comes to sys.path, you probably
> have to adjust that, at least during development when your package
> isn't in site-packages. 

This appears to be the best answer so far ;-)

__main__.py does indeed appear to be fairly weird. If I put a line
in flaskapp/__main__.py that says "import sys;print(sys.path)" and
run it with "python3 -m flaskapp", sys.path[0] is the empty string.
If I run it with "python3 flaskapp/__main__.py", sys.path[0] is
"/home/username/src/flaskapp" (and hence flaskapp cannot find itself).

So the problem is a combination of the above strangeness, and that
when werkzeug re-execs to "Restart with reloader", it runs
"python3 flaskapp/__main__.py" and not "python3 -m flaskapp".

Re: [flask] Import problem with Python 3 and __main__.py

From:
Johan Sandén
Date:
2014-08-27 @ 12:09
import flaskapp
flaskapp.app.run(debug=True)



On Wed, Aug 27, 2014 at 1:31 PM, Jon Ribbens
<jon-flask@unequivocal.co.uk> wrote:
> If I have an utterly minimal Flask app using the suggested layout:
>
>     runflaskapp.py
>     flaskapp/
>         __init__.py
>
> runflaskapp.py contains:
>
>     from flaskapp import app
>     app.run(debug=True)
>
> flaskapp/__init__.py contains:
>
>     from flask import Flask
>     app = Flask(__name__)
>
> Running this with 'python3 runflaskapp.py' works fine. However it
> seems to me that a more Python3onic way of doing this would be to
> rename 'runflaskapp.py' as 'flaskapp/__main__.py' and then run
> the whole thing as 'python3 -m flaskapp'. Unfortunately this doesn't
> work:
>
>     $ python3 -m flaskapp
>      * Running on http://127.0.0.1:5000/
>      * Restarting with reloader
>     Traceback (most recent call last):
>       File "/home/username/src/flaskapp/__main__.py", line 1, in <module>
>         from flaskapp import app
>     ImportError: No module named 'flaskapp'
>
> Does anyone know why and how to fix it?

Re: [flask] Import problem with Python 3 and __main__.py

From:
Jon Ribbens
Date:
2014-08-27 @ 13:16
On Wed, Aug 27, 2014 at 02:09:04PM +0200, Johan Sandén wrote:
> import flaskapp
> flaskapp.app.run(debug=True)

That just changes the error message to:

    ImportError: No module named 'flaskapp'

Re: [flask] Import problem with Python 3 and __main__.py

From:
boreq
Date:
2014-08-28 @ 18:36
How about
>from . import app

On 08/27/2014 03:16 PM, Jon Ribbens wrote:
> On Wed, Aug 27, 2014 at 02:09:04PM +0200, Johan Sandén wrote:
>> import flaskapp
>> flaskapp.app.run(debug=True)
> That just changes the error message to:
>
>     ImportError: No module named 'flaskapp'

Re: [flask] Import problem with Python 3 and __main__.py

From:
Moritz Beber
Date:
2014-08-27 @ 11:57
Hi,


On Wed, Aug 27, 2014 at 1:31 PM, Jon Ribbens <jon-flask@unequivocal.co.uk>
wrote:

> If I have an utterly minimal Flask app using the suggested layout:
>
>     runflaskapp.py
>     flaskapp/
>         __init__.py
>
> runflaskapp.py contains:
>
>     from flaskapp import app
>     app.run(debug=True)
>
> flaskapp/__init__.py contains:
>
>     from flask import Flask
>     app = Flask(__name__)
>
> Running this with 'python3 runflaskapp.py' works fine. However it
> seems to me that a more Python3onic way of doing this would be to
> rename 'runflaskapp.py' as 'flaskapp/__main__.py' and then run
> the whole thing as 'python3 -m flaskapp'. Unfortunately this doesn't
> work:
>

I recommend not doing that as a matter of principle. When deploying you
will want to install your package (into a venv or similar) and then import
your application into your WSGI handler. This blog post (
blog.ionelmc.ro/2014/05/25/python-packaging/) goes even further and
suggests to place your actual package into an extra directory (like 'src'),
like this:

runflaskapp.py
test/
src/
    flaskapp/
        __init__.py

This best simulates installing your package and then importing it. Of
course, the 'src' directory will have to be on the path then unless your
testing system handles this for you.


>
>     $ python3 -m flaskapp
>      * Running on http://127.0.0.1:5000/
>      * Restarting with reloader
>     Traceback (most recent call last):
>       File "/home/username/src/flaskapp/__main__.py", line 1, in <module>
>         from flaskapp import app
>     ImportError: No module named 'flaskapp'
>
> Does anyone know why and how to fix it?
>

Maybe this SO question can get you started:
http://stackoverflow.com/questions/72852/how-to-do-relative-imports-in-python

Re: [flask] Import problem with Python 3 and __main__.py

From:
Jon Ribbens
Date:
2014-08-27 @ 13:20
On Wed, Aug 27, 2014 at 01:57:01PM +0200, Moritz Beber wrote:
>    I recommend not doing that as a matter of principle. When deploying you
>    will want to install your package (into a venv or similar) and then import
>    your application into your WSGI handler. This blog post
>    ([2]blog.ionelmc.ro/2014/05/25/python-packaging/) goes even further and
>    suggests to place your actual package into an extra directory (like
>    'src'), like this:

That blog post appears to suggest that you should re-install your
package after every single change you make. That seems ridiculous to
me - not to mention it would prevent useful Flask features from
working such as auto-reloading on code changes.

>    Maybe this SO question can get you started:
>    http://stackoverflow.com/questions/72852/how-to-do-relative-imports-in-python

I'm not sure why that would be relevant; I'm not doing any relative imports.

Re: [flask] Import problem with Python 3 and __main__.py

From:
Daniel Neuhäuser
Date:
2014-08-27 @ 13:34

> Am 27.08.2014 um 15:20 schrieb Jon Ribbens <jon-flask@unequivocal.co.uk>:
> 
>> On Wed, Aug 27, 2014 at 01:57:01PM +0200, Moritz Beber wrote:
>>   I recommend not doing that as a matter of principle. When deploying you
>>   will want to install your package (into a venv or similar) and then import
>>   your application into your WSGI handler. This blog post
>>   ([2]blog.ionelmc.ro/2014/05/25/python-packaging/) goes even further and
>>   suggests to place your actual package into an extra directory (like
>>   'src'), like this:
> 
> That blog post appears to suggest that you should re-install your
> package after every single change you make. That seems ridiculous to
> me - not to mention it would prevent useful Flask features from
> working such as auto-reloading on code changes.
> 

This is a feature of the *development* server, which Flask provides. The 
development server is not intended to be used in production nor would it 
be a good idea to do so.

Re: [flask] Import problem with Python 3 and __main__.py

From:
Jon Ribbens
Date:
2014-08-27 @ 14:55
On Wed, Aug 27, 2014 at 03:34:55PM +0200, Daniel Neuhäuser wrote:
> > Am 27.08.2014 um 15:20 schrieb Jon Ribbens <jon-flask@unequivocal.co.uk>:
> >> On Wed, Aug 27, 2014 at 01:57:01PM +0200, Moritz Beber wrote:
> >>   I recommend not doing that as a matter of principle. When deploying you
> >>   will want to install your package (into a venv or similar) and then import
> >>   your application into your WSGI handler. This blog post
> >>   ([2]blog.ionelmc.ro/2014/05/25/python-packaging/) goes even further and
> >>   suggests to place your actual package into an extra directory (like
> >>   'src'), like this:
> > 
> > That blog post appears to suggest that you should re-install your
> > package after every single change you make. That seems ridiculous to
> > me - not to mention it would prevent useful Flask features from
> > working such as auto-reloading on code changes.
> 
> This is a feature of the *development* server, which Flask provides.
> The development server is not intended to be used in production nor
> would it be a good idea to do so.

Er, yes. I didn't say anything about use in production.

Re: [flask] Import problem with Python 3 and __main__.py

From:
Armin Ronacher
Date:
2014-09-07 @ 11:16
Hi,

On 27/08/14 15:20, Jon Ribbens wrote:
> That blog post appears to suggest that you should re-install your
> package after every single change you make. That seems ridiculous to
> me - not to mention it would prevent useful Flask features from
> working such as auto-reloading on code changes.
You can do `pip install --editable .` to instruct the metadata to point 
to the source files instead of copying them over.  Then you do not need 
to install upon doing modifications.


Regards,
Armin

Re: [flask] Import problem with Python 3 and __main__.py

From:
Armin Ronacher
Date:
2014-09-07 @ 11:17
Hi,

On 27/08/14 13:31, Jon Ribbens wrote:
> Does anyone know why and how to fix it?
This is not supported with the reloader.  Flask 1.0 will ship a wrapper 
executable that will solve cases like this but you will still not be 
able to run your flask with "python3 -m whatever" if you want the 
reloader active.

If you disable the reloader it will work (use_reloader=False).


Regards,
Armin

Re: [flask] Import problem with Python 3 and __main__.py

From:
Jon Ribbens
Date:
2014-09-07 @ 14:41
On Sun, Sep 07, 2014 at 01:17:26PM +0200, Armin Ronacher wrote:
> On 27/08/14 13:31, Jon Ribbens wrote:
> > Does anyone know why and how to fix it?
> This is not supported with the reloader.  Flask 1.0 will ship a wrapper 
> executable that will solve cases like this but you will still not be 
> able to run your flask with "python3 -m whatever" if you want the 
> reloader active.
> 
> If you disable the reloader it will work (use_reloader=False).

Thanks for your response. It seems to me the main problem is that               
there is no way for a Python 3 program to work out that it was
started by a '-m module' command-line, and so there is nothing
Flask/wwerkzeug can do to fix it, so I am not surprised.
Will the new wrapper solve the case where if you save a file with

a syntax error, the reloader crashes out and has to be manually
restarted?


(Also if 1.0 could fix the "in debug mode request.form["nonexistent"]

does not show a traceback" issue that would be fabulous ;-) )