wsgi script calling py scripts

73 views
Skip to first unread message

Chip Munk

unread,
Oct 23, 2012, 5:28:09 AM10/23/12
to mod...@googlegroups.com
Hello,

after a successful hello world I am now trying to do a bit more complex stuff..

(but all i find are the hello world issues...)
so here are my questions:

1) If I have several .py scripts that are doing different calculations, how do i run them from the wsgi script?
When I ad "import whatever" in the wsgi script, I get an error " No module named whatever". and the module is in the same
folder as the wsgi script. Or, should I turn all my .py scripts in a wsgi scripts??

2) how do I give arguments in the wsgi script? for example if I want to sum two numbers in my wsgi script,
how do I give these two numbers by url or any other way?

3) I want to use the wsgi script to get the requests and run appropriate .py scripts and return the results, is that
the best way to go? should I use some other technology?

Please help, I cant seem to find clear answers out there... Or point me to some 
other group if you find it more appropriate. 

Thank you a lot!

Graham Dumpleton

unread,
Oct 23, 2012, 6:25:05 AM10/23/12
to mod...@googlegroups.com
On 23 October 2012 20:28, Chip Munk <chip...@gmail.com> wrote:
> Hello,
>
> after a successful hello world I am now trying to do a bit more complex
> stuff..
>
> (but all i find are the hello world issues...)
> so here are my questions:
>
> 1) If I have several .py scripts that are doing different calculations, how
> do i run them from the wsgi script?
> When I ad "import whatever" in the wsgi script, I get an error " No module
> named whatever".

That is by design. The directory the WSGI script file is in, is not
automatically in the Python module search path. There are various
reasons why but if I explain them it will just confuse you as it ins't
straight forward and needs some knowledge of how Python import system
works.

> and the module is in the same
> folder as the wsgi script. Or, should I turn all my .py scripts in a wsgi
> scripts??

I would very strongly recommend at this point that you stop going down
the path of having separate WSGI script files in the one directory.
When you do that they by default all run in separate sub interpreters
of the same process. This will be inefficient.

I would strongly recommend right now that you go learn how to use
Flask (flask.pocoo.org), a Python micro framework which will teach you
a better way of creating a Python web application than working at raw
WSGI level. Part of what you will learn is how to structure the code
for your application so that it is out of harms way and not easily
downloadable by the users of your site, but such that modules will be
found when you import them.

> 2) how do I give arguments in the wsgi script? for example if I want to sum
> two numbers in my wsgi script,
> how do I give these two numbers by url or any other way?

Go learn Flask. Read up how it handles HTML form data if these
arguments are meant to come from a web page.

> 3) I want to use the wsgi script to get the requests and run appropriate .py
> scripts and return the results, is that
> the best way to go? should I use some other technology?

Usually you would do it all in process rather than execute separate
scripts. Go learn Flask first though before you make such a decision.

Also note that to learn Flask you can start by using its inbuilt
development server. You do not need to run it up under mod_wsgi to
learn it and trying to do so will only complicate your life. Worry
about mod_wsgi when you actually need to deploy your site so that
other people can use it.

Graham

> Please help, I cant seem to find clear answers out there... Or point me to
> some
> other group if you find it more appropriate.
>
> Thank you a lot!
>
> --
> You received this message because you are subscribed to the Google Groups
> "modwsgi" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/modwsgi/-/ov2zKDgJtHkJ.
> To post to this group, send email to mod...@googlegroups.com.
> To unsubscribe from this group, send email to
> modwsgi+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/modwsgi?hl=en.

Chip Munk

unread,
Oct 23, 2012, 6:43:11 AM10/23/12
to mod...@googlegroups.com
On Tuesday, October 23, 2012 12:25:07 PM UTC+2, Graham Dumpleton wrote:
That is by design. The directory the WSGI script file is in, is not 
automatically in the Python module search path. There are various
reasons why but if I explain them it will just confuse you as it ins't
straight forward and needs some knowledge of how Python import system
works.

ok
 
> and the module is in the same
> folder as the wsgi script. Or, should I turn all my .py scripts in a wsgi
> scripts??

I would very strongly recommend at this point that you stop going down
the path of having separate WSGI script files in the one directory.
When you do that they by default all run in separate sub interpreters
of the same process. This will be inefficient.

ok I see
 
I would strongly recommend right now that you go learn how to use
Flask (flask.pocoo.org ), a Python micro framework which will teach you
a better way of creating a Python web application than working at raw
WSGI level. Part of what you will learn is how to structure the code
for your application so that it is out of harms way and not easily
downloadable by the users of your site, but such that modules will be
found when you import them.

I was looking at Flask, and came to two problems:
1) not for python 3.x
2) some guys on other forums believe that it would be an overkill to use flask for what I need, and recommender wsgi scripts
 
Go learn Flask. Read up how it handles HTML form data if these 
arguments are meant to come from a web page.

:) ok, I get your point! 
but what about python 3?
 
Also note that to learn Flask you can start by using its inbuilt 
development server. You do not need to run it up under mod_wsgi to
learn it and trying to do so will only complicate your life. Worry
about mod_wsgi when you actually need to deploy your site so that
other people can use it.

I am doing this as a part of the small project (that should expand soon)... 
so for several reasons I should stay with apache server.. and the deployment 
of the initial simple version will be soon, so...

 All i need to do is to make it possible to call some scripts .py and return the results... i have never
imagined it would be so complicated... (?)  

Graham Dumpleton

unread,
Oct 23, 2012, 6:57:58 AM10/23/12
to mod...@googlegroups.com
On 23 October 2012 21:43, Chip Munk <chip...@gmail.com> wrote:
> On Tuesday, October 23, 2012 12:25:07 PM UTC+2, Graham Dumpleton wrote:
>> I would strongly recommend right now that you go learn how to use
>> Flask (flask.pocoo.org ), a Python micro framework which will teach you
>> a better way of creating a Python web application than working at raw
>> WSGI level. Part of what you will learn is how to structure the code
>> for your application so that it is out of harms way and not easily
>> downloadable by the users of your site, but such that modules will be
>> found when you import them.
>
> I was looking at Flask, and came to two problems:
> 1) not for python 3.x

Why does it have to be Python 3? The recommended version for
developing Python web applications is still Python 2.

Even you really feel it has to be Python 3, then go look at Bottle
then, another very simple micro framework.

> 2) some guys on other forums believe that it would be an overkill to use
> flask for what I need, and recommender wsgi scripts

The short of it is that to write raw WSGI applications and get it
correct and ensure it is secure is going to be harder than using a
simple micro framework which handles the hard stuff for you.

I would always recommend a micro framework even for simple stuff
because once you get your head around the micro framework, which will
not take much, you will be much more productive and will produce
better code that is more likely to work properly.

Graham

Chip Munk

unread,
Oct 23, 2012, 7:05:03 AM10/23/12
to mod...@googlegroups.com
On Tuesday, October 23, 2012 12:58:01 PM UTC+2, Graham Dumpleton wrote:
Why does it have to be Python 3? The recommended version for 
developing Python web applications is still Python 2.

Even you really feel it has to be Python 3, then go look at Bottle
then, another very simple micro framework.

ok, great, thanks for the tip
 
> 2) some guys on other forums believe that it would be an overkill to use
> flask for what I need, and recommender wsgi scripts

The short of it is that to write raw WSGI applications and get it
correct and ensure it is secure is going to be harder than using a
simple micro framework which handles the hard stuff for you.

I would always recommend a micro framework even for simple stuff
because once you get your head around the micro framework, which will
not take much, you will be much more productive and will produce
better code that is more likely to work properly.

that makes sense! thank you, I will definitely go in that direction.

thanks a lot!


Reply all
Reply to author
Forward
0 new messages