How to get worked mod_wsgi with .pyo files only (without .py)?

294 views
Skip to first unread message

Ruslan

unread,
Sep 13, 2011, 8:52:52 AM9/13/11
to modwsgi
I'm trying to start apache + mod_wsgi and I have strong restriction to
put source .py files onto server, so I compile them first and put .pyo
files onto server.

That's what I have in apache conf:

LoadModule wsgi_module modules/mod_wsgi.so
WSGIPythonPath /var/wwwroot/
WSGIPythonOptimize 1
WSGIScriptAlias /home /var/wwwroot/home.pyo

But mod_wsgi raises the error (cut from apache error log):

Failed to parse WSGI script file '/var/wwwroot/home.pyo'.
Exception occurred processing WSGI script '/var/wwwroot/home.pyo'.
File "/var/wwwroot/home.pyo", line 1
m\xf2
^
SyntaxError: invalid syntax

I tried to google but couldn't find an answer for does mod_wsgi work
with pre-compiled python code.

Ben Timby

unread,
Sep 13, 2011, 9:15:30 AM9/13/11
to mod...@googlegroups.com
On Tue, Sep 13, 2011 at 8:52 AM, Ruslan <ruslan.k...@gmail.com> wrote:
> I'm trying to start apache + mod_wsgi and I have strong restriction to
> put source .py files onto server, so I compile them first and put .pyo
> files onto server.

You will have to at least provide the source to the wsgi script file.
For instance, the WSGIScriptAlias would have to be home.py, not
home.pyo. The extension in this case does not change the behavior of
mod_wsgi, in fact, most examples I have seen using the .wsgi extension
for the WSGIScriptAlias file (home.wsgi) even though it contains
python code. You can then import the rest of your code from the
bootstrap, and only provide the .pyc/pyo files for the imported
modules. So, if the code you wish to hide is in the wsgi script
(home.py) move it elsewhere and import it.

Graham Dumpleton

unread,
Sep 13, 2011, 5:38:09 PM9/13/11
to mod...@googlegroups.com
Ben is correct, only the main WSGI script file needs to be source code
so it can just be a level of indirection to import code from .pyo
files elsewhere.

FWIW, a .wsgi extension is used because although it contains Python
code, it doesn't follow the normal conventions for module naming and
relative module importing.

So,if you look as __name__ of module it isn't basename of code file
but a magic calculated string. This is necessary to allow same code
file name to be used in multiple path locations mapped to with
different URLs from Apache. If used basename of code file, they would
all clash with each other.

Further, if use .py extension, someone could import the WSGI script
file by basename and that would cause a duplicate because in that case
would use basename. That would cause problems if accessing global data
in it, as wouldn't be the same data.

For module importing, an import from in the code file doesn't look in
current directory like with normal modules or packages and if you
wanted to that you would need to setup Python module search path your
self. Do that though and you do open yourself up to multiple import
problem above if use .py extension.

That therefore is the rational for suggesting .wsgi extension of .py.

Graham

> --
> You received this message because you are subscribed to the Google Groups "modwsgi" group.
> 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.
>
>

Ben Timby

unread,
Sep 13, 2011, 6:43:59 PM9/13/11
to mod...@googlegroups.com
On Tue, Sep 13, 2011 at 5:38 PM, Graham Dumpleton
<graham.d...@gmail.com> wrote:
> FWIW, a .wsgi extension is used because although it contains Python
> code, it doesn't follow the normal conventions for module naming and
> relative module importing.

Very informative. Thank you Graham.

Reply all
Reply to author
Forward
0 new messages