Working through this example in the Pyramid cookbook: http://docs.pylonsproject.org/projects/pyramid/en/latest/tutorials/modwsgi/index.html
This stanza is throwing an error: (when adapted to my circumstances anturally)
from pyramid.paster import get_app, setup_logging
ini_path = '/Users/chrism/modwsgi/env/myapp/production.ini'
setup_logging(ini_path)
application = get_app(ini_path, 'main')
The error message:
mod_wsgi (pid=27548): Target WSGI script '/home/rsadmin/modwsgi/env/hydra/hydra.wsgi' cannot be loaded as Python module.
mod_wsgi (pid=27548): Exception occurred processing WSGI script '/home/rsadmin/modwsgi/env/hydra/hydra.wsgi'.
Traceback (most recent call last):
File "/home/rsadmin/modwsgi/env/hydra/hydra.wsgi", line 1, in <module>
from pyramid.paster import get_app, setup_logging
ImportError: No module named pyramid.paster
I suspect what has happened is that pyramid.paster has been refactored since this doc was written, and no one has had time to correct it for the newest release.
Can anyone tell me what ought to be there instead, nowadays ?
TIA,
Erik
I had the hydra.wsgi script in the wrong directory. It needed to be one up from the path it was in: should be here:
/home/rsadmin/modwsgi/env/hydra.wsgi
The example in the cookbook is correct.
Related
I am puzzled by no module named app importerror. When I use name FlaskApp, everything works, when I change name to xyzApp, it does not and I am getting ImportError.
Here is the error code:
Target WSGI script '/var/www/xyz/xyzApp.wsgi' cannot be loaded as Python module.
Exception occurred processing WSGI script '/var/www/xyz/xyzApp.wsgi'.
Traceback (most recent call last):
File "/var/www/xyz/xyzApp.wsgi", line 11, in <module>
from xyzApp import app as application
ImportError: No module named 'xyzApp'
Target WSGI script '/var/www/xyz/xyzApp.wsgi' cannot be loaded as Python module.
Exception occurred processing WSGI script '/var/www/xyz/xyzApp.wsgi'.
Traceback (most recent call last):
File "/var/www/xyz/xyzApp.wsgi", line 11, in <module>
from xyzApp import app as application
ImportError: No module named 'xyzApp'
The Structure of the Flask applications looks as follows:
/var/www/xyz/
|--xyzApp.wsgi
|--/instance/config.py
|--/xyzApp
|--/static
|--/templates
|--__init__.py
When I do application with similar structure but with FlaskApp folders, it works. Why is that?
The structure that works is as follows:
/var/www/FlaskApp/
|--flaskapp.wsgi
|--/FlaskApp
|--/static
|--/templates
|--__init__.py
The content of flaskapp.wsgi file is the same except for xyzApp:
#! /usr/bin/python3
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp")
from FlaskApp import app as application
I am trying to write a test for my GAE programme which uses the datastore.
Following Google's Documentation, I see that I should be adding the path to my SDK into my PYTHONPATH.
I did this using:
import sys
sys.path.remove('/usr/local/lib/python2.7/dist-packages') # Has a 'google' module, which I want to be sure isn't interfering.
sys.path.insert(1,'/home/olly/google-cloud-sdk/platform/google_appengine')
sys.path.insert(1, '/home/olly/google-cloud-sdk/platform/google_appengine/lib/yaml/lib')
Then when the file is run:
Traceback (most recent call last):
File "myapp_tests.py", line 20, in <module>
from google.appengine.ext import ndb
ImportError: No module named appengine.ext
I have installed the SDK in the location above, and looking in /home/olly/google-cloud-sdk/platform/google_appengine/ I found the google folder, which has an __init__.py in it, along with appengine. Basically, the folder structure looks good to me, with them all being named correctly and having __init__.py files.
In an interactive console, after running the commands above, I found that I could run:
import google
no problem, but when I tried
import google.appengine
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named appengine
It was my understanding that having the __init__.py() files in the directories meant that they could be imported as above.
I also did a sudo find / --name "google", and the only thing that showed up that is also in my PYTHONPATH was the /usr/local/lib/python2.7/dist-packages, which I explicitly removed, and also inserted the rest of my paths in front of anyway.
I tried using GAE's own method of:
import dev_appserver
dev_appserver.fix_sys_path()
which added a whole lot of paths to sys.path, but still didn't help me make it work.
I also found that when I add '/home/olly/Servers/google_appengine/google' to my paths, I can run:
import appengine.ext
but running:
from appengine.ext import ndb
causes:
Traceback (most recent call last):
File "booking_function_tests.py", line 16, in <module>
from appengine.ext import ndb
File "/home/olly/Servers/google_appengine/google/appengine/ext/ndb/__init__.py", line 7, in <module>
from tasklets import *
File "/home/olly/Servers/google_appengine/google/appengine/ext/ndb/tasklets.py", line 69, in <module>
from .google_imports import apiproxy_stub_map
File "/home/olly/Servers/google_appengine/google/appengine/ext/ndb/google_imports.py" , line 11, in <module>
from google3.storage.onestore.v3 import entity_pb
ImportError: No module named google3.storage.onestore.v3
Am I missing something really obvious? How should I go about importing ndb?
EDIT:
I'm running the latest SDK (1.9.34), but I have the following code in my google_imports.py:
try:
from google.appengine.datastore import entity_pb
normal_environment = True
except ImportError:
try:
from google3.storage.onestore.v3 import entity_pb
normal_environment = False
except ImportError:
# If we are running locally but outside the context of App Engine.
try:
set_appengine_imports()
from google.appengine.datastore import entity_pb
normal_environment = True
except ImportError:
raise ImportError('Unable to find the App Engine SDK. '
'Did you remember to set the "GAE" environment '
'variable to be the path to the App Engine SDK?')
Also, google.__path__ gives me the '/usr/local/lib/python2.7/dist-packages' path which I thought I removed earlier. Here is an excerpt of how I'm removing it:
import sys
sys.path.insert(1, '/home/olly/Servers/google_appengine')
sys.path.insert(1, '/home/olly/Servers/google_appengine/lib/yaml/lib')
sys.path.remove('/usr/local/lib/python2.7/dist-packages')
import google
print google.__path__
print sys.path
['/usr/local/lib/python2.7/dist-packages/google']
['/home/olly/Servers/google_appengine/myapp', '/home/olly/Servers/google_appengine/lib/yaml/lib', '/home/olly/Servers/google_appengine/google', '/home/olly/Servers/google_appengine', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client', '/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode']
So my sys.path is updated, but import google seems to still be importing from the no-longer-there path, which would be the crux of my problem I guess. Do I need to reload the path or something?
I run into these problems a lot less by always running inside a virtualenv.
I agree with snakecharmerrb you should get print google.__file__ or google.__path_ to figure out exactly what you're importing.
This snippet might also solve your problem:
import google
gae_dir = google.__path__.append('/path/to/appengine_sdk//google_appengine/google')
sys.path.insert(0, gae_dir) # might not be necessary
import google.appengine # now it's on your import path`
Which version of app engine SDK are you using? I am using the latest SDK (1.9.34). I find the below snippet in my ~/google_appengine/google/appengine/ext/ndb/google_imports.py file
try:
from google3.storage.onestore.v3 import entity_pb
normal_environment = False
except ImportError:
# If we are running locally but outside the context of App Engine.
try:
set_appengine_imports()
from google.appengine.datastore import entity_pb
normal_environment = True
except ImportError:
raise ImportError('Unable to find the App Engine SDK. '
'Did you remember to set the "GAE" environment '
'variable to be the path to the App Engine SDK?')
But in your stack trace, after google3.storage import it doesn't seem to go inside the except clause.
So try the same code with latest SDK.
I'm moving a web application from a Windows environment to CentOS 5.11 and Apache, and with everything installed, I'm receiving a 500 when I try to load up the site. The error log shows this:
mod_wsgi (pid=5461): Target WSGI script '/usr/local/treehouse/wsgi/index.wsgi' cannot be loaded as Python module.
mod_wsgi (pid=5461): Exception occurred processing WSGI script '/usr/local/treehouse/wsgi/index.wsgi'.
Traceback (most recent call last):
File "/usr/local/treehouse/wsgi/index.wsgi", line 11, in <module>
from django.core.wsgi import get_wsgi_application
ImportError: No module named 'django.core.wsgi'
I found this question (and the related ones), which appears to be similar but none of the answers there fix the issue. I'm not running in a virtualenv, and django appears to be installed correctly, as I can run the offending statement:
>>> from django.core.wsgi import get_wsgi_application
in the interactive Python interpreter and it works just fine. Here's the script causing the error (it's just the default index.wsgi that you see everywhere you look for this stuff):
import os, sys
sys.path.append('/usr/local/treehouse/apple')
sys.path.append('/usr/local/treehouse/apple/apple')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "apple.settings")
from django.core.wsgi import get_wsgi_application application = get_wsgi_application()
Obviously the last line causes the error, but here's the fun part: the last line remains the problem, even if I add this just above it:
import django.core
import django.core.handlers
Both of the underlying packages import without a hitch (django.core.wsgi is just a thin wrapper around some functionality in django.core.handlers.wsgi). It's only when I try to access the wsgi packages that problems occur. Inspecting sys.path within this script shows all the right directories (/usr/local/lib/python3.4/site-packages, etc.). And again, running the exact same code from the interactive interpreter causes no errors.
I tried an uninstall/reinstall of django (pip install django==1.6.5), then I reset the VM to a totally clean snapshot and rebuilt/reinstalled everything, and still I get the exact same behavior.
What is going on?
Some of the django source files pulled down by pip are saved with Windows line endings: that is, they end with \r\n rather than \n. On *nix systems, this has the effect of breaking imports in affected files, because instead of looking for django.core.wsgi, it tries to import django.core.wsgi\r.
This is why django.core.handlers can still be imported: the __init__.py file is empty, so there are no line endings to corrupt.
To fix it, run dos2unix on affected files. I'm not sure how many files are actually affected (except that it's many of them) so I just hit all of them with a quick Python script from the interpreter:
import os
from os.path import abspath, join
for root, _, files in os.walk('/usr/local/lib/python3.4/site-packages/django'):
for f in files:
os.system('dos2unix %s' % abspath(join(root, f)))
Et voilĂ , no more import errors!
Storytime
I stumbled on this issue after I began hacking on the django source files in desperation. I edited django/core/__init__.py (normally empty) by adding this:
from . import wsgi
I modified index.wsgi to include this:
import django.core
django.core.wsgi # no-op to see if we can access it
and ended up with a fascinating new error:
Traceback (most recent call last):
File "/usr/local/treehouse/wsgi/index.wsgi", line 11, in <module>
import django.core
File "/usr/local/lib/python3.4/site-packages/django/core/__init__.py", line 1, in <module>
from . import wsgi
File "/usr/local/lib/python3.4/site-packages/django/core/wsgi.py", line 1, in <module>
from django.core.handlers.wsgi import WSGIHandler
ImportError: No module named 'django.core.handlers.wsgi'
So from within django/core/__init__.py, I can access django/core/wsgi.py. But django.core.handlers.wsgi is out of reach. I deleted the changes to reproduce the original error--but the error had changed. Now I was getting the No module named 'django.core.handlers.wsgi' even without the explicit relative import.
This was when it hit me. I opened up django/core/handlers/wsgi.py in gedit, clicked the end of a line, pressed the spacebar, deleted the insertion, and saved the file. It should have been a no-op. But when I restarted the server, suddenly the import error had moved on to a different django import. The only logical explanation was that the line endings were wrong, and by re-saving the file in gedit, I was silently fixing them.
Is CherryPy broken? I just set it up and tried to use the routes dispatcher but it has an import error, my code is as follows:
import cherrypy
mapper = cherrypy.dispatch.RoutesDispatcher()
The error is:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/jwesonga/environments/cherrypy/lib/python2.6/site-packages/CherryPy-3.2.2-py2.6.egg/cherrypy/_cpdispatch.py", line 463, in __init__
import routes
ImportError: No module named routes
I'm on a Mac and I tried both 3.2.2 and 3.0 using virtualenv for the latter.
I have successfully used CherryPy with the routes dispatcher under OS X.
The error you've shown is:
ImportError: No module named routes
This is pretty clear -- Python can't find the routes modules. Have you installed it? This is not part of CherryPy, it's a separate module that you will need to install. If you're using MacPorts, you should be able to:
port install py-routes
(Or py25-routes or py26-routes depending on which Python you're using). If you're using virtualenv, you can simply run:
easy_install routes
I'm using Ubuntu 11.04 (natty). I have been using Suds to consume a SOAP web service. Everything was working fine... until it wasn't. I can no longer import Suds. I've uninstalled and re-installed Suds from the Ubuntu repositories but still get the same import error (see IDLE traceback below). I'm using Python 2.7.1 and Suds 0.4.1-2. Does anyone have any ideas on how to solve this problem??
>>> import suds
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import suds
File "/usr/lib/pymodules/python2.7/suds/__init__.py", line 154, in <module>
import client
File "/usr/lib/pymodules/python2.7/suds/client.py", line 23, in <module>
import suds.metrics as metrics
File "/usr/lib/pymodules/python2.7/suds/metrics.py", line 23, in <module>
from logging import getLogger
ImportError: cannot import name getLogger
>>>
logging is a standard module of Python. There are several possible reasons why Python can't find it anymore:
The is another logging module in the path (print sys.path to get a list of paths Python will search)
Someone changed PYTHONPATH (the default Python search path)
Someone messed with the Python installation (deleted the logging module)