ImportError: No module named django.core.wsgi - python

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.

Related

Import errors between some python moduls/functions/variables

This code worked on Friday without problems and still running on a colleagues laptop, but I cannot run it anymore.
As you can see in the screenshot, my editor doesnt find some moduls anymore and the pylint Error "E0401: Unable to import" occurs.
The missing file exists in the folder Settings, as you can see in the Explorer on the left side.
Today I deactivated/activated pylint, reinstalled vs code and python, added the init.py to Settings folder, tried the same code in eclipse, modified the Path enviroment variable and created the PYTHONPATH enviroment variable. All this with no success:/
I am greatful for each hint, which provide me to solve this problem.
The error output as text:
Windows PowerShell
Copyright (C) Microsoft Corporation. Alle Rechte vorbehalten.
PS C:\Users\Evgenij\Desktop\Desktop\Eth_Test_Dev> & C:/Python27/python.exe c:\Users\Evgenij\Desktop\Desktop\Eth_Test_Dev\Code\__TC__Template.py
Traceback (most recent call last):
File "c:\Users\Evgenij\Desktop\Desktop\Eth_Test_Dev\Code\__TC__Template.py", line 36, in <module>
from Lib.IHR_EthApi import EthApi as ETH
File "c:\Users\Evgenij\Desktop\Desktop\Eth_Test_Dev\Code\Lib\IHR_EthApi.py", line 6, in <module>
from IHR_GeneralApi import GeneralApi as SYS
File "c:\Users\Evgenij\Desktop\Desktop\Eth_Test_Dev\Code\Lib\IHR_GeneralApi.py", line 4, in <module>
import IHR_TestSuiteConfig.py
ImportError: No module named IHR_TestSuiteConfig.py
PS C:\Users\Evgenij\Desktop\Desktop\Eth_Test_Dev>
In your code you have the line:
import IHR_TestSuiteConfig.py
That won't work because you don't specify modules to import by file name but by module name, e.g.:
import IHR_TestSuiteConfig
But looking at your screenshot you have a bigger issue of the code being kept in a Settings directory at the same level as your Lib directory containing the code you are importing into.
You need to either anchor all of your code up a level so you can do:
from ..Settings import IHR_TestSuiteConfig
Or you need to manipulate your PYTHONPATH environment variable to put Settings directly on to sys.path (in VS Code you can create a .env file to do this, but it won't' affect running Python from the terminal, only when VS Code runs e.g. Pylint).

absolute_import not working correctly (in celery project)

I am trying to setup a simple celery project. As per the official documentation, the layout is as follow:
clemux#melody ~/dev/debian/debsources/debsources
% find new_updater -name "*.py"
new_updater/tasks.py
new_updater/updater.py
new_updater/__init__.py
new_updater/celery.py
In celery.py, I import celery.Celery this way:
from __future__ import absolute_import
from celery import Celery
In IPython, I can import new_updater.celery without problems:
In [2]: from debsources.new_updater import celery
In [3]: celery?
Type: module
String form: <module 'debsources.new_updater.celery' from '/home/clemux/dev/debian/debsources/debsources/new_updater/celery.pyc'>
However, when trying to run new_updater.updater, I run into the following error:
clemux#melody ~/dev/debian/debsources
% python debsources/new_updater/updater.py
Traceback (most recent call last):
File "debsources/new_updater/updater.py", line 6, in <module>
from debsources.new_updater.tasks import print_package
File "/home/clemux/dev/debian/debsources/debsources/new_updater/tasks.py", line 3, in <module>
from debsources.new_updater.celery import app
File "/home/clemux/dev/debian/debsources/debsources/new_updater/celery.py", line 3, in <module>
from celery import Celery
File "/home/clemux/dev/debian/debsources/debsources/new_updater/celery.py", line 3, in <module>
from celery import Celery
ImportError: cannot import name Celery
What could be going on here?
I know I could simply rename celery.py to, e.g. celery_config.py (this is the standard answer to this kind of question on SO), but I'd prefer actually fixing this and not stray away from celery's official documentation.
EDIT: I printed out sys.path in new_updater/updater.py, here's the result:
['/home/clemux/dev/debian/debsources/debsources/new_updater',
'/home/clemux/dev/debian/debsources',
'/home/clemux/.virtualenvs/debsources/lib/python2.7',
<snip>
Deleting sys.path[0] before the other import 'solves' the issue, but I don't understand why that is in the path. How I got that:
mkvirtualenv test
python setup.py develop at the root of my project
EDIT2: it's the same outside a virtualenv, with celery installed from debian, and my PYTHONPATH set this way:
export PYTHONPATH=/usr/lib/python2.7/dist-packages:~/dev/debian/debsources
About how that first line got into sys.path:
A list of strings that specifies the search path for modules.
Initialized from the environment variable PYTHONPATH, plus an
installation-dependent default. As initialized upon program startup,
the first item of this list, path[0], is the directory containing the
script that was used to invoke the Python interpreter.
from docs
Anyway, you shouldn't name your files as libraries you use, even if off. docs do so. Will help avoid many possible errors.

Dreamhost - ImportError: No module named _weakrefset

Dreamhost upgraded a number of servers this weekend, including the one I was on. It broke my configuration, so as recommended I attempted to delete the virtual environment it was running on and attempted to re-set it up. However, when I try to navigate to the site, I get this:
Traceback (most recent call last):
File "/home/thesp/mysite.com/env/lib/python2.7/site-packages/site.py", line 74, in <module>
__boot()
File "/home/thesp/mysite.com/env/lib/python2.7/site-packages/site.py", line 2, in __boot
import sys, os, os.path
File "/home/thesp/lib/python2.7/os.py", line 400, in <module>
import UserDict
File "/home/thesp/lib/python2.7/UserDict.py", line 83, in <module>
import _abcoll
File "/home/thesp/lib/python2.7/_abcoll.py", line 11, in <module>
from abc import ABCMeta, abstractmethod
File "/home/thesp/lib/python2.7/abc.py", line 8, in <module>
from _weakrefset import WeakSet
ImportError: No module named _weakrefset
I've got Python 2.7.8 installed and running, and from shell access, both in and out of my virtual environment when I run Python, I'm pulling up the correct version (which is different from the native version installed, so it's finding my setup). Other posts which reference this error message seem to think it's a problem with not having an upgraded version of virtualenv, but its version is higher than the troublesome version. (I'm running 1.11.6.)
Weirder still, I can shell into Python, type from _weakrefset import WeakSet, and I don't get import errors. I'm running Django 1.6 and I can python manage.py runserver with no errors, but the web server is throwing up before it ever sees Django.
In the traceback, the first two lines are pulling from my virtual environment, but the remaining ones don't seem to be, and I have no idea why, or even if that's relevant.
Any advice on what I should do next? I've about pulled my hair out on this one! I can post any additional information that would help troubleshoot. Thanks!
Well, I feel silly now. I went to the /home/thesp/lib/python2.7/ directory and downloaded _weakrefset.py, like so:
wget http://svn.python.org/projects/python/trunk/Lib/_weakrefset.py
...and now everything seems to be running fine. There was a _weakrefset.pyo file in the directory, so I'm not sure why _weakrefset.py never made it in, but this seems to have done the trick.
Now, that doesn't solve the mystery of why the stack trace switches directories like it does, but it's running now, so I'll take it for now!

Import and python source file in folders

I have some troubles understanding how python import works.
I just forked this repository and I am going to play with it but i am having troubles understanding how python import works with file in directories.
The directory structure is the following:
fitbit/
__init__.py
gather_keys_cli.py
api.py
exceptions.py
utils.py
As a preliminary step I need to run gather_keys_cli.py. It depends on api.py, which in turn depends on exceptions.py.
If I try to run it from the python interpreter everything works as expected. If I try to run it from the command line (As suggested in the documentation) I have obtain the following exception.
$./fitbit/gather_keys_cli.py KEY SECRET
Traceback (most recent call last):
File "./fitbit/gather_keys_cli.py", line 34, in <module>
from api import FitbitOauthClient
File "/Users/mariosangiorgio/FitBitHacks/python-fitbit/fitbit/api.py", line 9, in <module>
from fitbit.exceptions import (BadResponse, DeleteError, HTTPBadRequest,
ImportError: No module named fitbit.exceptions
My understanding is that when I invoke the command line the base path is set to the folder containing it. At this point, the scripts correctly imports the oauth client because it uses this instruction from api import FitbitOauthClient, which does not contain any reference to the fitbit directory.
On the opposite, api.py uses this instruction to import exceptions from fitbit.exceptions import (LIST_OF_CLASSES). This instruction contains the reference to the fitbit directory and hence I got the error.
I am not familiar with python so I'd like to understand what is the pythonic way to solve this issue.
I have the feeling that there is something clearly wrong going on and I found a workaround by moving gather_keys_cli.py and changing the api import to import fitbit.api. It works but I am not sure it is the right way to solve the issue I'm experiencing.

Pyramid cookbook WSGI example out of date?

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.

Categories