My data_site.wsgi file:
import main
application = application()
Error i get at apache:
[Thu Apr 29 07:07:41 2010] [error] [client 81.167.201.136] Traceback (most recent call last):
[Thu Apr 29 07:07:41 2010] [error] [client 81.167.201.136] File "/var/www/vhosts/data.oddprojects.net/htdocs/data_site.wsgi", line 1, in <module>
[Thu Apr 29 07:07:41 2010] [error] [client 81.167.201.136] import main
[Thu Apr 29 07:07:41 2010] [error] [client 81.167.201.136] ImportError: No module named main
Paths:
htdocs
data_site.wsgi
main.py
The PYTHONPATH under mod_wsgi doesn't include the directory the .wsgi is in. I often use something like the below in my .wsgi files.
import os, sys; sys.path.append(os.path.dirname(__file__))
(You might opt for .insert(0, ...) instead of .append(...) if that works better for you.)
Related
I am trying to use Bottle.py and mod_wsgi together but I am getting a strange error that I cannot fix.
When I try to make a curl request ("curl http://localhost/myapi/hello1") I get the error: "ImportError: No module named server". However, if I take out the "import server" line in my app.wsgi file and try "curl http://localhost/myapi/hello2" it works just fine. I have tried adding to the WSGIPythonPath, WSGIDaemonProcess, etc. and nothing has worked. Does anyone see anything I may be doing wrong?
Note: The "/usr/local/www/wsgi-script" path is successfully being added to sys.path, so that isn't the issue. I also do have a init.py file in the "/usr/local/www/wsgi-scripts" directory and the permissions are all 777.
[root#my_vm httpd]# ls -lt /usr/local/www/wsgi-scripts/
total 8
-rwxrwxrwx. 1 root root 95 Mar 21 14:42 server.py
-rwxrwxrwx. 1 root root 318 Mar 21 14:37 app.wsgi
-rwxrwxrwx. 1 root root 0 Mar 20 19:53 __init__.py
Using Versions:
Apache/2.4.6 (Red Hat Enterprise Linux)
Python 2.7.5
Bottle 0.12.9
/usr/local/www/wsgi-scripts/server.py
import bottle
from bottle import route
#route('/hello1')
def hello():
return "Hello World!"
/usr/local/www/wsgi-scripts/app.wsgi
import os
import sys
import bottle
from bottle import route
server_loc = "/usr/local/www/wsgi-scripts"
if not server_loc in sys.path:
sys.path.insert(0, server_loc)
os.chdir(os.path.dirname(__file__))
print(sys.path)
#route('/hello2')
def hello():
return "Hello World!\n"
import server
application = bottle.default_app()
/etc/httpd/conf.d/myapi.conf
<VirtualHost *:80>
ServerName localhost
WSGIScriptAlias /myapi /usr/local/www/wsgi-scripts/app.wsgi
<Directory /usr/local/www/wsgi-scripts>
Require all granted
</Directory>
</VirtualHost>
EDIT: Adding python traceback
/etc/httpd/logs/error_log
[Thu Mar 22 13:39:18.234573 2018] [core:notice] [pid 18172] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
[Thu Mar 22 13:39:29.628352 2018] [:error] [pid 18174] ['/usr/local/www/wsgi-scripts', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib/python2.7/site-packages']
[Thu Mar 22 13:39:29.628514 2018] [:error] [pid 18174] [client ::1:35838] mod_wsgi (pid=18174): Target WSGI script '/usr/local/www/wsgi-scripts/app.wsgi' cannot be loaded as Python module.
[Thu Mar 22 13:39:29.628525 2018] [:error] [pid 18174] [client ::1:35838] mod_wsgi (pid=18174): Exception occurred processing WSGI script '/usr/local/www/wsgi-scripts/app.wsgi'.
[Thu Mar 22 13:39:29.628539 2018] [:error] [pid 18174] [client ::1:35838] Traceback (most recent call last):
[Thu Mar 22 13:39:29.628556 2018] [:error] [pid 18174] [client ::1:35838] File "/usr/local/www/wsgi-scripts/app.wsgi", line 18, in <module>
[Thu Mar 22 13:39:29.628625 2018] [:error] [pid 18174] [client ::1:35838] import server
[Thu Mar 22 13:39:29.628645 2018] [:error] [pid 18174] [client ::1:35838] ImportError: No module named server
Adding the solution that I found worked for me:
When I moved the server.py into another directory inside the directory (aka /usr/local/www/wsgi-scripts/server.py --> /usr/local/www/wsgi-scripts/inner_dir/server.py), and do "import inner_dir.server" instead of "import server", it started working.
I am getting below error when I start the httpd server and access the app url -
[Tue Feb 21 22:01:51 2017] [error] [client 10.209.33.139] mod_wsgi (pid=15425): Target WSGI script '/scratch/gdudwadk/python-projects/newblog/app/wsgi.py' cannot be loaded as Python module.
[Tue Feb 21 22:01:51 2017] [error] [client 10.209.33.139] mod_wsgi (pid=15425): Exception occurred processing WSGI script '/scratch/gdudwadk/python-projects/newblog/app/wsgi.py'.
[Tue Feb 21 22:01:51 2017] [error] Traceback (most recent call last):
[Tue Feb 21 22:01:51 2017] [error] File "/scratch/gdudwadk/python-projects/newblog/app/wsgi.py", line 7, in <module>
[Tue Feb 21 22:01:51 2017] [error] from app import app as application
[Tue Feb 21 22:01:51 2017] [error] File "/scratch/gdudwadk/python-projects/newblog/app/app.py", line 1, in <module>
[Tue Feb 21 22:01:51 2017] [error] from flask import Flask
[Tue Feb 21 22:01:51 2017] [error] File "/scratch/gdudwadk/python-projects/newblog/venv/lib/python2.6/site-packages/flask/__init__.py", line 17, in <module>
[Tue Feb 21 22:01:51 2017] [error] from werkzeug.exceptions import abort
[Tue Feb 21 22:01:51 2017] [error] File "/scratch/gdudwadk/python-projects/newblog/venv/lib/python2.6/site-packages/werkzeug/__init__.py", line 17, in <module>
[Tue Feb 21 22:01:51 2017] [error] from types import ModuleType
[Tue Feb 21 22:01:51 2017] [error] ImportError: No module named types
This started happening after my machine was replaced with new one. Previously app was working fine.
I found one reference to python2.7 in /etc/httpd/conf.d/wsgi.conf and that's what was causing the issue. After removing that, the app was up and running.. Thanks!
I am developing a website and want to put some files on the server. The problem is that I can only import python modules ( e.g. "import os"), but somewhy, can not import my own modules:
test1.py:
import test2
test2.py:
print ("Content-type:text/html\n\n")
print ("<html>")
print ("<head>")
print ("<title>Error</title>")
print ("</head>")
print ("<body>")
print (" hello world 2")
print ("</body>")
print ("</html>")
If I click www.mywebsite.com/test2.py, then I recieve "hello world" on the screen.
However, if I click www.mywebsite.com/test1.py, I get "500 internal server error." I found out that it is some problem with not being able to import my modules.
p.s. since I am on a shared server, I can not changes sys path etc....
Here is a trace I got in errors.log:
[Thu Jun 11 00:18:09 2015] [error] [client 85.65.174.249] mod_python (pid=17816, interpreter='delekulator.co.il', phase='PythonHandler', handler='mod_python.cgihandler'): Application error
[Thu Jun 11 00:18:09 2015] [error] [client 85.65.174.249] ServerName: 'delekulator.co.il'
[Thu Jun 11 00:18:09 2015] [error] [client 85.65.174.249] DocumentRoot: '/var/www/vhosts/mywebsite.com/httpdocs'
[Thu Jun 11 00:18:09 2015] [error] [client 85.65.174.249] URI: '/test1.py'
[Thu Jun 11 00:18:09 2015] [error] [client 85.65.174.249] Location: None
[Thu Jun 11 00:18:09 2015] [error] [client 85.65.174.249] Directory: '/var/www/vhosts/mywebsite.com/httpdocs/'
[Thu Jun 11 00:18:09 2015] [error] [client 85.65.174.249] Filename: '/var/www/vhosts/mywebsite.com/httpdocs/test1.py'
[Thu Jun 11 00:18:09 2015] [error] [client 85.65.174.249] PathInfo: ''
[Thu Jun 11 00:18:09 2015] [error] [client 85.65.174.249] Traceback (most recent call last):
[Thu Jun 11 00:18:09 2015] [error] [client 85.65.174.249] File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 1537, in HandlerDispatch\n default=default_handler, arg=req, silent=hlist.silent)
[Thu Jun 11 00:18:09 2015] [error] [client 85.65.174.249] File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 1229, in _process_target\n result = _execute_target(config, req, object, arg)
[Thu Jun 11 00:18:09 2015] [error] [client 85.65.174.249] File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 1128, in _execute_target\n result = object(arg)
[Thu Jun 11 00:18:09 2015] [error] [client 85.65.174.249] File "/usr/lib/python2.6/dist-packages/mod_python/cgihandler.py", line 96, in handler\n imp.load_module(module_name, fd, path, desc)
[Thu Jun 11 00:18:09 2015] [error] [client 85.65.174.249] File "/var/www/vhosts/mywebsite.com/httpdocs/test1.py", line 1, in <module>\n import test2
[Thu Jun 11 00:18:09 2015] [error] [client 85.65.174.249] ImportError: No module named test2
In the last line, you can see that the server writes "ImportError: No module named test2"
How can I fix it?
From your description, your program did not found your own module, because you did not give your module's path. Now there are two methods to solve it:
Assume your module's path is: /srv/modules/xxxx.py
First you should import your module in your program: import xxxx
command line:Before you run your program with command line, give the path first. command:
export PYTHONPATH=/srv/modules.Then run your program.
add two lines of codes in your program:
import sys
sys.path.append("/srv/modules")
Use any one way above, you should run your program success.I hope this can help you.
Modules should be in the Python path or in the same directory where the executable is.
Then you do like usually
import test2
test2.some_function() # or whatever.
I am getting "Internal Server Error" when I try to access the django website.
I am using Django 1.8, Python 2.7.10, centos 6.5 and apache.
In apache log I am getting the following error:
mod_wsgi (pid=23866): Target WSGI script '/abc/abc/abc/wsgi.py' cannot be loaded as Python module.
[Mon May 25 14:40:47 2015] [error] [client xyz] mod_wsgi (pid=23866): Exception occurred processing WSGI script '/abc/abc/abc/wsgi.py'.
[Mon May 25 14:40:47 2015] [error] [client xyz] Traceback (most recent call last):
[Mon May 25 14:40:47 2015] [error] [client xyz] File "/abc/abc/abc/wsgi.py", line 12, in <module>
[Mon May 25 14:40:47 2015] [error] [client xyz] from django.core.wsgi import get_wsgi_application
[Mon May 25 14:40:47 2015] [error] [client xyz] File "/abc/lib/python2.7/site-packages/django/__init__.py", line 1, in <module>
[Mon May 25 14:40:47 2015] [error] [client xyz] from django.utils.version import get_version
[Mon May 25 14:40:47 2015] [error] [client xyz] File "/abc/lib/python2.7/site-packages/django/utils/version.py", line 7, in <module>
[Mon May 25 14:40:47 2015] [error] [client xyz] from django.utils.lru_cache import lru_cache
[Mon May 25 14:40:47 2015] [error] [client xyz] File "/abc/lib/python2.7/site-packages/django/utils/lru_cache.py", line 28
[Mon May 25 14:40:47 2015] [error] [client xyz] fasttypes = {int, str, frozenset, type(None)},
[Mon May 25 14:40:47 2015] [error] [client xyz] ^
[Mon May 25 14:40:47 2015] [error] [client xyz] SyntaxError: invalid syntax
Thanks
Although you say you are running Python 2.7, this error indicates that in fact you are using 2.6, as set literals were only introduced in 2.7.
The issue is that mod_wsgi is compiled against a specific Python version, which in your case is the one that comes with Centos 6.5. You will need to recompile it for 2.7, or find a version already compiled against that.
You need to start using virtualenv for your django project. Here's a nice tutorial. For a makeshift solution, you can just run django with python 2.7 by using python2.7 manage.py runserver. However, it's highly recommended to look into virtualenv. Makes life much easier.
With regards to making mod_wsgi use python 2.7, you need to recompile it. For that, just download the source and execute:
$ ./configure
$ make
$ sudo make install
They have a quick installation guide that you can access here.
I have successfully installed trac 1.0 . It is running fine when in standalone mode .
I am getting the following error after doing the integration with Apache mod_wsgi
mod_wsgi (pid=23895): Exception occurred processing WSGI script '/var/www/cgi-bin/trac_pcbb.trac.wsgi'.
[Tue Sep 30 19:40:27 2014] [error] [client 192.168.10.218] Traceback (most recent call last):
[Tue Sep 30 19:40:27 2014] [error] [client 192.168.10.218] File "/var/www/cgi-bin/trac_pcbb.trac.wsgi", line 30, in application
[Tue Sep 30 19:40:27 2014] [error] [client 192.168.10.218] from trac.web.main import dispatch_request
[Tue Sep 30 19:40:27 2014] [error] [client 192.168.10.218] ImportError: No module named trac.web.main