librelist archives

« back to archive

Flask with Buildout

Flask with Buildout

From:
Oleksandr Lobunets
Date:
2011-07-18 @ 13:45
Hello!

I'm using Flask inside of a buildout. No virtualenv or systemwide Flask 
installation. 
Running with debug=False causes no issues, all good excepting a 
requirement to restart the server while developing.

When specifying app.debug= True, the application crashes with:

$ bin/python source/project/main.py
 * Running on http://127.0.0.1:5000/
 * Restarting with reloader...
Traceback (most recent call last):
  File "source/project/main.py", line 12, in <module>
    from flask import Flask
ImportError: No module named flask

Which is a typical issue for other web frameworks I used in the past 
(excepting, probably, Django - it works fine inside of the buildout).
Any fixes/solutions available?

Thank you in advance,
Alex.

Re: [flask] Flask with Buildout

From:
Simon Sapin
Date:
2011-07-18 @ 14:10
 On Mon, 18 Jul 2011 15:45:22 +0200, Oleksandr Lobunets wrote:
> Hello!
>
> I'm using Flask inside of a buildout. No virtualenv or systemwide
> Flask installation.
> Running with debug=False causes no issues, all good excepting a
> requirement to restart the server while developing.
>
> When specifying app.debug= True, the application crashes with:
>
> $ bin/python source/project/main.py
>  * Running on http://127.0.0.1:5000/
>  * Restarting with reloader...
> Traceback (most recent call last):
>   File "source/project/main.py", line 12, in <module>
>     from flask import Flask
> ImportError: No module named flask

 Hi,

 I’ve found the 'python' script created by buildout to be incompatible 
 with the Werkzeug reloader (or the reverse, depending on your point of 
 view). 'bin/python' removes itself from sys.argv while Werkzeug uses 
 sys.argv to start a new process:

 https://github.com/mitsuhiko/werkzeug/blob/master/werkzeug/serving.py#L504

 Try using a custom script with buildout instead of bin/python:

     [foo]
     recipe = zc.recipe.egg:scripts
     entry-points = script_name=package.module:function  # function() 
 calls app.run()

 If you really want bin/python, try adding "sys.executable = __file__" 
 where appropriate inside it. (This might break other things, and you’ll 
 have to do it again every time buildout rewrites the file.)

 If neither work, you’ll have to disable the reloader:  
 app.run(use_reloader=False)
 Of course, you’ll then need to restart you server for every change in 
 Python code.

 Or, you know, use virtualenv.

 Regards,
-- 
 Simon Sapin

Re: [flask] Flask with Buildout

From:
Oleksandr Lobunets
Date:
2011-07-18 @ 16:23
Thanks for the hint with zc.recipe.egg:scripts! Works perfectly for me!

Best,
Alex.

On Jul 18, 2011, at 4:10 PM, Simon Sapin wrote:

> On Mon, 18 Jul 2011 15:45:22 +0200, Oleksandr Lobunets wrote:
>> Hello!
>> 
>> I'm using Flask inside of a buildout. No virtualenv or systemwide
>> Flask installation.
>> Running with debug=False causes no issues, all good excepting a
>> requirement to restart the server while developing.
>> 
>> When specifying app.debug= True, the application crashes with:
>> 
>> $ bin/python source/project/main.py
>> * Running on http://127.0.0.1:5000/
>> * Restarting with reloader...
>> Traceback (most recent call last):
>>  File "source/project/main.py", line 12, in <module>
>>    from flask import Flask
>> ImportError: No module named flask
> 
> Hi,
> 
> I’ve found the 'python' script created by buildout to be incompatible 
> with the Werkzeug reloader (or the reverse, depending on your point of 
> view). 'bin/python' removes itself from sys.argv while Werkzeug uses 
> sys.argv to start a new process:
> 
> https://github.com/mitsuhiko/werkzeug/blob/master/werkzeug/serving.py#L504
> 
> Try using a custom script with buildout instead of bin/python:
> 
>     [foo]
>     recipe = zc.recipe.egg:scripts
>     entry-points = script_name=package.module:function  # function() 
> calls app.run()
> 
> If you really want bin/python, try adding "sys.executable = __file__" 
> where appropriate inside it. (This might break other things, and you’ll 
> have to do it again every time buildout rewrites the file.)
> 
> If neither work, you’ll have to disable the reloader:  
> app.run(use_reloader=False)
> Of course, you’ll then need to restart you server for every change in 
> Python code.
> 
> Or, you know, use virtualenv.
> 
> Regards,
> -- 
> Simon Sapin

Re: [flask] Flask with Buildout

From:
Baiju M
Date:
2011-07-18 @ 14:54
On Mon, Jul 18, 2011 at 7:40 PM, Simon Sapin <simon.sapin@exyr.org> wrote:
[...snip...]
>  Try using a custom script with buildout instead of bin/python:
>
>     [foo]
>     recipe = zc.recipe.egg:scripts
>     entry-points = script_name=package.module:function  # function()
>  calls app.run()

I also use this approach with host and port passed as arguments:

[config]
host = 0.0.0.0
port = 8080

[myscript]
recipe = z3c.recipe.scripts
eggs = mydistro
entry-points = myscript=mypkg.main:run
interpreter = python
script-initialization =
    host = "${config:host}"
    port = ${config:port}
arguments = host, port

To run:

./bin/myscript

--
Baiju M