ImportError: No module named web - python in MAMP - python

I am running an apache server with MAMP in my Macbook pro. And I have added a python file named File.py to the CGI-Executable directory.
Contents of File.py:
#!/usr/bin/python
import web
import json
from xml.etree.ElementTree import Element, SubElement, Comment, tostring
urls = (
'/json', 'get_json'
)
app = web.application(urls, globals())
class get_json:
def GET(self):
pyDict = {'one':1,'two':2}
web.header('Content-Type', 'application/json')
return json.dumps(pyDict)
if __name__ == "__main__":
app.run()
I have installed web.py (by running pip install web.py) in my Mac. Now when I go to browser and enter http://localhost:8888/cgi-bin/File.py/json it is showing error 500 internal error, I checked the log file, log is
[Thu Mar 17 00:31:20 2016] [error] [client ::1] Traceback (most recent call last):
[Thu Mar 17 00:31:20 2016] [error] [client ::1] File " path-to-CGI-Executables/File.py", line 3, in <module>
[Thu Mar 17 00:31:20 2016] [error] [client ::1] import web
[Thu Mar 17 00:31:20 2016] [error] [client ::1] ImportError: No module named web
[Thu Mar 17 00:31:20 2016] [error] [client ::1] Premature end of script headers: File.py
Now I tried running the python file from terminal like
$ python File.py 127.0.0.1
Then in browser http://127.0.0.1:8080/json its working fine and showing {"two": 2, "one": 1} as result.
Whats happening if I add this to MAMP CGI? How to make installed modules available within MAMP?
UPDATE:
MAMP version : Version 3.5 (3.5)
Python version : Python 2.7.11 Mac
OS X version : 10.10.5 (14F1605)

Related

'module' object has no attribute 'lru_cache'

I'm getting this error in my logs with django app. I thought such errors occur when using Python 2.x, but in my virtual env Python - 3.6.5. Django - 2.0.7. VESTA Control Panel with wsgi. I will be grateful for the help.
Error log:
[Thu Jul 26 19:02:48 2018] [error] [client 86.32.36.143] File "/home/project/private/django/project/env/lib/python3.6/site-packages/django/utils/version.py", line 61, in
[Thu Jul 26 19:02:48 2018] [error] [client 86.32.36.143] #functools.lru_cache()
[Thu Jul 26 19:02:48 2018] [error] [client 86.32.36.143] AttributeError: 'module' object has no attribute 'lru_cache'
django.wsgi
import os, sys
sys.path.insert(0, '/home/user/web/project/private/django/project/env/lib/python3.6/site-packages')
sys.path.insert(0, '/home/user/web/project/private/django/project/project/src/shared/')
sys.path.insert(0, '/home/user/web/project/private/django/project/project/src/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'main.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I had the same issue but then found out that my site was built with python3 and apache was configured for python2.7. The following link helped:
Target WSGI script cannot be loaded as Python module

Import errors with WSGI, Flask, and Apache

Totally new to python-based web apps, so I'm somewhat lost. Here is my apache error:
[Wed May 08 22:41:47 2013] [error] [client 64.56.91.45] mod_wsgi (pid=23704): Target WSGI script '/home/http/public/hello/hello.wsgi' cannot be loaded as Python module.
[Wed May 08 22:41:47 2013] [error] [client 64.56.91.45] mod_wsgi (pid=23704): Exception occurred processing WSGI script '/home/http/public/hello/hello.wsgi'.
[Wed May 08 22:41:47 2013] [error] [client 64.56.91.45] Traceback (most recent call last):
[Wed May 08 22:41:47 2013] [error] [client 64.56.91.45] File "/home/http/public/hello/hello.wsgi", line 3, in <module>
[Wed May 08 22:41:47 2013] [error] [client 64.56.91.45] from hello import app as application
[Wed May 08 22:41:47 2013] [error] [client 64.56.91.45] File "/home/http/public/hello/hello.py", line 1, in <module>
[Wed May 08 22:41:47 2013] [error] [client 64.56.91.45] from flask import Flask
[Wed May 08 22:41:47 2013] [error] [client 64.56.91.45] ImportError: No module named 'flask'
[Wed May 08 22:41:47 2013] [error] [client 64.56.91.45] File does not exist: /home/http/public/favicon.ico
Apparently, it cannot find the flask module. I've looked this up, and it seems that most people get it to work by appending the project directory to the path, like so: (hello.wsgi)
import sys
sys.path.insert(0, "/home/http/public/hello")
from hello import app as application
And here is hello.py:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run(host='0.0.0.0')
Works great when ran as python hello.py, however, throws a 500 error when browsing to host/hello. Here's the Apache configuration:
WSGIDaemonProcess hello user=http group=http threads=5
WSGIScriptAlias /hello "/home/http/public/hello/hello.wsgi"
<Directory /home/http/public/hello/>
WSGIProcessGroup hello
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
I'm somewhat lost.
The mod_wsgi module is built against a specific Python version and can only work with that version. You cannot force it to use a different version. Thus if you have multiple versions installed, you need to make sure that you are installing your packages and running your code in development against the same version.
You can determine which Python version/installation mod_wsgi is using by running the test:
http://code.google.com/p/modwsgi/wiki/CheckingYourInstallation#Python_Installation_In_Use

I upgraded Python 2.5 to 2.6 but now get "ImportError: No module named tagging.views"

THE ISSUE
The problem: it seems like the tagging module which I have in my /srv/python-environments/saltycrane/lib/python2.5/site-packages/tagging is not being included because I upgraded the system to 2.6, despite my assumption of relying on 2.5 in virtualenv mode.
Here's the error in /var/log/apache2/error.log:
[Thu Mar 24 00:08:35 2011] [error] [client 127.0.0.1] mod_wsgi (pid=10470): Exception occurred processing WSGI script '/srv/workarounds/apache/django.wsgi'.
[Thu Mar 24 00:08:35 2011] [error] [client 127.0.0.1] Traceback (most recent call last):
[Thu Mar 24 00:08:35 2011] [error] [client 127.0.0.1] File "/usr/lib/pymodules/python2.6/django/core/handlers/wsgi.py", line 241, in __call__
[Thu Mar 24 00:08:35 2011] [error] [client 127.0.0.1] response = self.get_response(request)
[Thu Mar 24 00:08:35 2011] [error] [client 127.0.0.1] File "/usr/lib/pymodules/python2.6/django/core/handlers/base.py", line 141, in get_response
[Thu Mar 24 00:08:35 2011] [error] [client 127.0.0.1] return self.handle_uncaught_exception(request, resolver, sys.exc_info())
[Thu Mar 24 00:08:35 2011] [error] [client 127.0.0.1] File "/usr/lib/pymodules/python2.6/django/core/handlers/base.py", line 176, in handle_uncaught_exception
[Thu Mar 24 00:08:35 2011] [error] [client 127.0.0.1] if resolver.urlconf_module is None:
[Thu Mar 24 00:08:35 2011] [error] [client 127.0.0.1] File "/usr/lib/pymodules/python2.6/django/core/urlresolvers.py", line 239, in _get_urlconf_module
[Thu Mar 24 00:08:35 2011] [error] [client 127.0.0.1] self._urlconf_module = import_module(self.urlconf_name)
[Thu Mar 24 00:08:35 2011] [error] [client 127.0.0.1] File "/usr/lib/pymodules/python2.6/django/utils/importlib.py", line 35, in import_module
[Thu Mar 24 00:08:35 2011] [error] [client 127.0.0.1] __import__(name)
[Thu Mar 24 00:08:35 2011] [error] [client 127.0.0.1] File "/srv/workarounds/urls.py", line 3, in <module>
[Thu Mar 24 00:08:35 2011] [error] [client 127.0.0.1] from tagging.views import tagged_object_list
[Thu Mar 24 00:08:35 2011] [error] [client 127.0.0.1] ImportError: No module named tagging.views
MY SETUP
Firstly, here's my setup:
$ python --version
Python 2.6.6
$ python
>>> import django
django.VERSION
(1, 2, 3, 'final', 0)
My django projects live in /srv. My virtual envs live in /srv/python-environments
If I cd into /srv/ and do:
source python-environments/saltycrane/bin/activate
The python version becomes:
$ python --version
Python 2.5.2
My project, /srv/workarounds uses /srv/workarounds/apache/django.wsgi, which is composed of:
import os, sys, site
virtualenv = '/srv/python-environments/saltycrane/'
ALLDIRS = [os.path.join(virtualenv, 'lib', 'python%s' % sys.version[:3], 'site-packages')]
# Remember original sys.path.
prev_sys_path = list(sys.path)
# Add project directory
sys.path.append( '/srv/' )
sys.path.append( '/srv/workarounds/' )
for directory in ALLDIRS:
site.addsitedir( directory )
new_sys_path = []
for item in list(sys.path):
if item not in prev_sys_path:
new_sys_path.append(item)
sys.path.remove(item)
sys.path[:0] = new_sys_path
os.environ['DJANGO_SETTINGS_MODULE'] = 'workarounds.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
The code basically adds /srv/ and /srv/workarounds to the python path when the wsgi is run by Apache.
SO....
How can I adjust my wsgi to keep using Python 2.5.2 instead of 2.6? Or if not, how can I update my virtualenv to use python 2.6 packages with pip?
THINGS IM NOT SURE ABOUT
Perhaps it runs on 2.6 and does grab the tagging module, but the tagging module or a part of it isn't compatible, therefore that's why it complains of No module named tagging.views ?
Maybe this has nothing to do with 2.5 to 2.6 and was always present? But I doubt this very much because I did several restarts with that code in place, and it worked fine.
It could have been a Django subtle version update, I might have updated 1.1 or 1.2beta to 1.2.3 final.
EDIT: If I have the right mod_wsgi.so, it's compiled for 2.6.
/usr/lib/apache2/modules$ ldd mod_wsgi.so
linux-gate.so.1 => (0xf57fe000)
libpython2.6.so.1.0 => /usr/lib/libpython2.6.so.1.0 (0xb760f000)
libpthread.so.0 => /lib/libpthread.so.0 (0xb75f6000)
libdl.so.2 => /lib/libdl.so.2 (0xb75f2000)
libutil.so.1 => /lib/libutil.so.1 (0xb75ed000)
libm.so.6 => /lib/libm.so.6 (0xb75c7000)
libc.so.6 => /lib/libc.so.6 (0xb7482000)
libssl.so.0.9.8 => /usr/lib/i686/cmov/libssl.so.0.9.8 (0xb7438000)
libcrypto.so.0.9.8 => /usr/lib/i686/cmov/libcrypto.so.0.9.8 (0xb72e0000)
libz.so.1 => /usr/lib/libz.so.1 (0xb72cc000)
/lib/ld-linux.so.2 (0xb788e000)
I think if you changed this it might work:
ALLDIRS = [os.path.join(virtualenv, 'lib', 'python%s' % sys.version[:3], 'site-packages')]
to
ALLDIRS = [os.path.join(virtualenv, 'lib', 'python2.5', 'site-packages')]
Setting your WSGIPythonHome is what is suggested when using virtualenv with modwsgi:
virtualenv --no-site-packages --python=python2.5 BASELINE
and setup modwsgi to use that as the wsgipythonhome
Baseline Environment
The first step in using virtual
environments with mod_wsgi is to point
mod_wsgi at a baseline Python
environment. This step is actually
optional and if not done the main
Python installation for the system,
usually that which mod_wsgi was
compiled for, would be used as the
baseline environment.
Although the main Python installation
can be used, especially in a shared
environment where daemon mode of
mod_wsgi is used to host WSGI
applications for different users, it
is better to make the baseline
environment a virgin environment with
an effectively empty 'site-packages'
directory. This way there is no
possibility of conflicts between
modules and packages in a users
individual Python virtual environment
and the baseline environment.
To create a virgin environment using
the 'virtualenv' program, the
'--no-site-packages' option should be
supplied when creating the
environment.
$ cd /usr/local/pythonenv
$ virtualenv --no-site-packages BASELINE
New python executable in BASELINE/bin/python
Installing setuptools............done.
Note that the version of Python from
which this baseline environment is
created must be the same version of
Python that mod_wsgi was compiled for.
It is not possible to mix environments
based on different major/minor
versions of Python.
Once the baseline Python environment
has been created, the WSGIPythonHome
directive should be defined within the
global part of the main Apache
configuration files. The directive
should refer to the top level
directory for the baseline environment
created by the 'virtualenv' script.
WSGIPythonHome /usr/local/pythonenv/BASELINE
This Python environment will now be
used as the baseline environment for
all WSGI applications running under
mod_wsgi, whether they be run in
embedded mode or daemon mode.
There is no need to set the
WSGIPythonHome directive if you want
to use the main Python installation as
the baseline environment.

mod_wsgi cannot load mysql

First I will list my environment and status:
Environment:
System: Mac OS 10.6
Python: 2.6
Apache: 2.2
mod_wsgi: 3.3
mysql: 5.x
php: 3.5
trac:0.12
Status:
I can run trac as alone-server without problem.
Now I integrate trac to apache, problem come out. mod_wsgi can run normal wsgi page correctly, but cannot load mysql connection. I test it in piece of code, put "import MySQLdb" in wsgi page, it still cannot. I also can access trac through apache, but it shows me Cannot load Python bindings for MySQL
I refer to the apache log:
[Sun Mar 13 13:36:44 2011] [error] [client ::1] mod_wsgi (pid=37060): Target WSGI script /Users/alex/Library/apache2/htdocs/sql.wsgi' cannot be loaded as Python module.
[Sun Mar 13 13:36:44 2011] [error] [client ::1] mod_wsgi (pid=37060): Exception occurred processing WSGI script '/Users/alex/Library/apache2/htdocs/sql.wsgi'.
[Sun Mar 13 13:36:44 2011] [error] [client ::1] Traceback (most recent call last):
[Sun Mar 13 13:36:44 2011] [error] [client ::1] File "/Users/alex/Library/apache2/htdocs/sql.wsgi", line 2, in <module>
[Sun Mar 13 13:36:44 2011] [error] [client ::1] import MySQLdb
[Sun Mar 13 13:36:44 2011] [error] [client ::1] File "build/bdist.macosx-10.6-universal/egg/MySQLdb/__init__.py", line 19, in <module>
[Sun Mar 13 13:36:44 2011] [error] [client ::1] File "build/bdist.macosx-10.6-universal/egg/_mysql.py", line 7, in <module>
[Sun Mar 13 13:36:44 2011] [error] [client ::1] File "build/bdist.macosx-10.6-universal/egg/_mysql.py", line 6, in __bootstrap__
[Sun Mar 13 13:36:44 2011] [error] [client ::1] ImportError: dlopen(/Users/alex/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.16.dylib
[Sun Mar 13 13:36:44 2011] [error] [client ::1] Referenced from: /Users/alex/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so
[Sun Mar 13 13:36:44 2011] [error] [client ::1] Reason: image not found
[Sun Mar 13 13:36:57 2011] [error] [client ::1] mod_wsgi (pid=37061): Target WSGI script
This looks like mod_wsgi cannot find libmysqlclient.16.dylib. I encounter similar problem when I install mysqldb for python, so I add export
DYLD_LIBRARY_PATH=/Users/alex/Library/mysql-5.x/lib
SO python -c "import MySQLdb" can work, but mod_wsgi cannot work.
So I put
os.environ['DYLD_LIBRARY_PATH'] = '/Users/alex/Library/mysql-5.x/lib'
into trac.ini.
It doesn't work for trac in apache still.
So anyone has this problem before?
Thanks
Try LD_LIBRARY_PATH instead:
export LD_LIBRARY_PATH=/usr/local/mysql/lib/mysql:$LD_LIBRARY_PATH
The problem is that your Apache is running as 64 bit and you are using 32 bit version of MySQL, or Apache is running as 32 bit and you are using 64 bit version of MySQL.
Run the 'file' command on 'libmysqlclient.16.dylib' and it will tell you what architectures it is built for. See similar issues for the actual Python extension at:
http://code.google.com/p/modwsgi/wiki/InstallationOnMacOSX#Missing_Code_For_Architecture
You need to get hold of a far MySQL library binary or force Apache to run as same architecture. The linked documentation has some guidelines for what can be done.

trying to run werkzeug on apache (wsgi error)

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.)

Categories