简体   繁体   中英

ImportError: No module named web

I am getting this error in my apache error log file when I try to run my web.py application using mod_wsgi . I have installed web.py successfully on my shared hosting and I can confirm that I am able to import it locally:

>>> import web
>>> web.application(('/', 'test'), globals())
<web.application.application instance at 0x1f8d3b0>

I am also able to run the built in server and serve pages to my site successfully.

I can confirm that the mod_wsgi module is working in apache as well since I am able to serve pages using manual coding of a wsgi app.

I tried the suggested method for the ImportError: No module named web error message on the web.py documentation http://webpy.org/install#apachemodwsgi , ie, adding:

abspath = os.path.dirname(__file__)
sys.path.append(abspath)
os.chdir(abspath)

import web

I also added the Files tag suggested in the http.conf file which seems redundant since I have the htdocs dir set up already, but anyway. My httpd.conf file is below, I have restarted apache and am still getting the import error message.

ServerRoot "/home/usr1/webapps/test/apache2"

LoadModule dir_module        modules/mod_dir.so
LoadModule env_module        modules/mod_env.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule mime_module       modules/mod_mime.so
LoadModule rewrite_module    modules/mod_rewrite.so
LoadModule setenvif_module   modules/mod_setenvif.so
LoadModule wsgi_module       modules/mod_wsgi.so

LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog /home/usr1/webapps/test/logs combined
DirectoryIndex index.py
DocumentRoot /home/usr1/webapps/test/htdocs
ErrorLog /home/usr1/webapps/test/apache2/logs/error_test.log
KeepAlive Off
Listen 21708
MaxSpareThreads 3
MinSpareThreads 1
ServerLimit 1
SetEnvIf X-Forwarded-SSL on HTTPS=1
ThreadsPerChild 5
WSGIDaemonProcess test processes=5 python-path=/home/usr1/webapps/test/lib/python3.1 threads=1
WSGIProcessGroup test
WSGIRestrictEmbedded On
WSGILazyInitialization On

<Directory /home/usr1/webapps/test/htdocs>
    AddHandler wsgi-script .py
</Directory>

<Files /home/usr1/webapps/test/htdocs/index.py>
   SetHandler wsgi-script
   Options ExecCgi FollowSymLinks
</Files>

First of all, your apache mod_wsgi seems to be compiled with python 3, that is not supported by web.py.

Your apache conf seems a lot like the one I have on Webfaction, if there is an installer for mod_wsgi with python 2.7, you have to choose it instead of python 3.

This is what my typical conf looks like:

ServerRoot "/home/username/webapps/projectname/apache2"

LoadModule log_config_module modules/mod_log_config.so
LoadModule mime_module       modules/mod_mime.so
LoadModule rewrite_module    modules/mod_rewrite.so
LoadModule alias_module modules/mod_alias.so
LoadModule wsgi_module       modules/mod_wsgi.so

LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog /home/username/logs/user/access_projectname.log combined
ErrorLog /home/username/logs/user/error_projectname.log
KeepAlive Off
Listen 21708
MaxSpareThreads 3
MinSpareThreads 1
ServerLimit 1
WSGIPythonOptimize 2
ThreadsPerChild 5
WSGIDaemonProcess projectname processes=5 threads=1
WSGIPythonHome /home/username/lib/python2.7 # your python home dir where libraries are installed
WSGIProcessGroup projectname
WSGIRestrictEmbedded On
WSGILazyInitialization On
WSGIScriptAlias / /home/username/webapps/projectname/htdocs/code.py/
Alias /static /home/username/webapps/projectname/htdocs/static

And this is example code.py

#!/usr/bin/env python
import os
import sys
sys.path.append(os.path.abspath(os.path.dirname(__file__)))
from app import app

if __name__ == "__main__":
    app.run()
else:
    application = app.wsgifunc()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM