I'm trying to deploy django project on Ubuntu 14.04, but I’ve got import error: No module named ‘Django’ but actually the module exist. I reinstalled it many times but anyway the same problem occurs.
There is wsgy.py file
#wsgi.py
import os
import time
import traceback
import signal
import sys
from django.core.wsgi import get_wsgi_application
# Calculate the path based on the location of the WSGI script.
apache_configuration= os.path.dirname(__file__)
project = os.path.dirname(apache_configuration)
workspace = os.path.dirname(project)
sys.path.append(workspace)
sys.path.append(project)
# Add the path to 3rd party django application and to django itself.
sys.path.append('/var/www/s052d78fe.fastvps-server.com/public_html/myv/lib/python3.4/site-packages')
os.environ['DJANGO_SETTINGS_MODULE'] = 'msite.apache.override'
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
And sys. path
'', '/var/www/s052d78fe.fastvps-server.com/public_html/myv/lib/python3.4',
'/var/www/s052d78fe.fastvps-server.com/public_html/myv/lib/python3.4/plat-x86_64-linux-gnu', '/var/www/s052d78fe.fastvps-server.com/public_html/myv/lib/python3.4/lib-dynload',
'/usr/lib/python3.4', '/usr/lib/python3.4/plat-x86_64-linux-gnu', '/var/www/s052d78fe.fastvps-server.com/public_html/myv/lib/python3.4/site-packages']
Thanks.
I'm trying to deploy a hello-world type app on Elastic Beanstalk. Just about everything seems to work, packages are installed, etc. up to the point where mod_wsgi attempts to retrieve the "application" object from wsgi.py. At that point, the following appears in the logs (once in the logs for each unsuccessfuly HTTP request):
mod_wsgi (pid=6114): Target WSGI script '/opt/python/current/app/myapp/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=6114): Exception occurred processing WSGI script '/opt/python/current/app/myapp/wsgi.py'.
Traceback (most recent call last):
File "/opt/python/current/app/caserails/wsgi.py", line 20, in <module>
application = get_wsgi_application()
File "/opt/python/run/venv/lib/python2.7/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
django.setup()
File "/opt/python/run/venv/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
from django.utils.log import configure_logging
File "/opt/python/run/venv/lib/python2.7/site-packages/django/utils/log.py", line 16, in <module>
from logging import NullHandler # NOQA
ImportError: cannot import name NullHandler
Link to concurrent AWS Forum Post.
The NullHandler is only available on Python version 2.7+. You could create the NullHandler yourself on an ImportError:
import logging
try:
from logging import NullHandler
except ImportError:
class NullHandler(logging.Handler):
def emit(self, record):
pass
logging.getLogger(__name__).addHandler(NullHandler())
More information about logging.NullHandler: https://docs.python.org/3/library/logging.handlers.html#logging.NullHandler.
I had similar problem and in my case the issue was that for some unrelated project I created logging.py file in home folder and when I ran something in home, it was importing this file instead of the real module.
You can check which file is being imported like this:
import logging
print(logging.__file__)
I fixed it by deleting logging.py I created previously.
NullHandler was introduced in version 2.7. Are you sure you are running 2.7 on your server?
After much trial and error, the (immediate) problem was solved by removing python logging from requirements.txt and rebuilding the environment.
I do not yet fully understand why this is a problem. On my local machine, I'm able to install logging and run Django without error. I suspect as vikramls pointed out that something weird is happening in the intersection between mod_wsgi, the baselineenv in which mod_wsgi executes, and the virtualenv in which my app operates.
But, at least for now, I'm fixing this error by not including "logging" in requirements.txt when deploying Django 1.7 on Elastic Beanstalk.
I had this issue while creating a build for python 2.7 with PyInstaller.So, I uninstalled logger from my ENV.else you can also remove the package name from the requirement.txt.
This approach solves my issue [Happy Coding:)]
I went through this example here:
http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html
All my tasks are in files called tasks.py.
After updating celery and adding the file from the example django is throwing the following error, no matter what I try:
ImportError: cannot import name Celery
Is the problem possibly caused by the following?
app.autodiscover_tasks(settings.INSTALLED_APPS, related_name='tasks')
Because it goes through all tasks.py files which all have the following import.
from cloud.celery import app
cloud/celery.py:
from __future__ import absolute_import
import os, sys
from celery import Celery
from celery.schedules import crontab
from django.conf import settings
BROKER_URL = 'redis://:PASSWORD#localhost'
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'cloud.settings')
app = Celery('cloud', broker=BROKER_URL)
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(settings.INSTALLED_APPS, related_name='tasks')
if "test" in sys.argv:
app.conf.update(
CELERY_ALWAYS_EAGER=True,
)
print >> sys.stderr, 'CELERY_ALWAYS_EAGER = True'
CELERYBEAT_SCHEDULE = {
'test_rabbit_running': {
"task": "retail.tasks.test_rabbit_running",
"schedule": 3600, #every hour
},
[..]
app.conf.update(
CELERYBEAT_SCHEDULE=CELERYBEAT_SCHEDULE
)
retail/tasks.py:
from cloud.celery import app
import logging
from celery.utils.log import get_task_logger
logger = get_task_logger('tasks')
logger.setLevel(logging.DEBUG)
#app.task
def test_rabbit_running():
import datetime
utcnow = datetime.datetime.now()
logger.info('CELERY RUNNING')
The error happens, when I try to access a url that is not valid, like /foobar.
Here is the full traceback:
Traceback (most recent call last):
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 126, in handle_request
respiter = self.wsgi(environ, resp.start_response)
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 255, in __call__
response = self.get_response(request)
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 178, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 220, in handle_uncaught_exception
if resolver.urlconf_module is None:
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 342, in urlconf_module
self._urlconf_module = import_module(self.urlconf_name)
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/opt/src/slicephone/cloud/cloud/urls.py", line 52, in
urlpatterns += patterns('', url(r'^search/', include('search.urls')))
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 25, in include
urlconf_module = import_module(urlconf_module)
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/opt/src/slicephone/cloud/search/urls.py", line 5, in
from handlers import SearchHandler
File "/opt/src/slicephone/cloud/search/handlers.py", line 15, in
from places import handlers as placeshandler
File "/opt/src/slicephone/cloud/places/handlers.py", line 23, in
import api as placesapi
File "/opt/src/slicephone/cloud/places/api.py", line 9, in
from djapi import *
File "/opt/src/slicephone/cloud/places/djapi.py", line 26, in
from tasks import add_single_place, add_multiple_places
File "/opt/src/slicephone/cloud/places/tasks.py", line 2, in
from cloud.celery import app
File "/opt/src/slicephone/cloud/cloud/celery.py", line 4, in
from celery import Celery
File "/opt/src/slicephone/cloud/cloud/celery.py", line 4, in
from celery import Celery
ImportError: cannot import name Celery
Adding the following lines to cloud/celery.py:
import celery
print celery.__file__
gave me the file itself and not the celery module from the library. After renaming celery.py to celeryapp.py and adjusting the imports all errors were gone.
Note:
That leads to a change in starting the worker:
celery worker --app=cloud.celeryapp:app
For those running celery==3.1.2 and getting this error:
TypeError: unpack_from() argument 1 must be string or read-only buffer, not memoryview
Apply the patch mentioned here: https://github.com/celery/celery/issues/1637
With Django 1.7.5, Celery 3.1.17, and Python 2.7.6 I found that I was still getting these ImportError: cannot import name Celery. But only when running tests under PyCharm 4.0.4.
I found that a solution was not to rely on from __future__ import absolute_import as described in First Steps with Django. Instead I renamed proj/proj/celery.py to proj/proj/celery_tasks.py and then changed the content of __init__.py to match: from .celery_tasks import app as celery_app. No more multiple instances of files named celery.py to cause import confusion seemed to be a simpler approach.
got the same error
my celery settings filename which was(celery.py) was conflicting with 'celery' package...
so while doing this-> from celery import Celery ,
it was raising error- cannot import name Celery
solution->just change the 'celery.py' to something else like 'celery-settings.py'
In October 2022, importlib-metadata==5.0.0 was released. In python 3.7, this breaks kombu==5.2.4 (see issue#1600) which is used by the current version of celery.
Pin importlib-metadata==4.13.0 or another version less than 5, or update to python 3.8.
Work for me ( some bug after deploy in server ):
Remove all *.pyc files from project and restart him.
Did you add the line:
from __future__ import absolute_import
to the top of your cloud/celery.py module?
Read the breakdown of the example here:
http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html
For someone who want to know what cause this error:
I have meet this problem just now, then I found the problem --- sys.path.
Maybe you add some path to sys.path like me, I add below code in manage.py,
ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
SRC_PATH = os.path.join(ROOT_PATH, 'src')
CONF_PATH = os.path.join(ROOT_PATH, 'conf')
sys.path.insert(0, SRC_PATH)
sys.path.insert(0, CONF_PATH)
so, from celery import Celery would search celery in SRC_PATH and CONF_PATH first, that's the problem.
change to
sys.path.append(SRC_PATH)
sys.path.append(CONF_PATH)
It would search in python's lib and site-packages first. Solved perfectly.
I got the same error. It turns out there was a problem with my Celery version. I upgraded to 3.1 and celeryd is now deprecated for this version (http://celery.readthedocs.org/en/latest/whatsnew-3.1.html). So I had to downgrade to the version 3.0.19 that was the previous stable version used for the project, and it works good so far.
pip install celery==3.0.19
Anyway, if you don't want to downgrade, the replacement for celeryd in the version 3.1 is celery worker. Check here for more info: http://celery.readthedocs.org/en/latest/userguide/workers.html.
Hope this helps! :)
I have face similar type of issue:
from celery import Celery
ImportError: cannot import name 'Celery' from 'celery'
Another simple way to solve this:
If your package have celery configuration in celery.py this is the reason that it is causing problems.
Rename it something like celery_settings.py
Note that older Django projects have the manage.py script in the same directory as the project directory. That is, the structure looks like this:
- proj/
- proj/__init__.py
- proj/celery.py
- proj/urls.py
- proj/manage.py
- proj/settings.py
instead of this:
- proj/
- proj/__init__.py
- proj/celery.py
- proj/settings.py
- proj/urls.py
- manage.py
In this case, you will just have to rename the celery.app file to something different, like celeryapp.py as suggested in the accepted answer above.
I faced the same issue on a FastAPI app running on Python 3.7. None of the solutions from this thread were working for me. I fixed the issue on my app by upgrading Python to 3.8.
If the above error i.e. ImportError: cannot import name 'Celery' from 'celery' (/usr/local/airflow/.local/lib/python3.7/site-packages/celery/__init__.py) is coming and the python version is being used as 3.7.*, then due to a bug in importlib-metadata library because of its latest release.
So, to fix the above issue please use this version in your python dependencies list:
pip install importlib-metadata==4.13.0
Alternately, if you are using Docker file to build the project, change the python version from 3.7 to 3.8 and it should work fine.
For me, it worked. Please use the below link for a detailed look:
Django_celery_python_3.7_issue
I got the same error.
Seems that from __future__ import absolute_import DOES NOT work for Python 2.6.1, still not raising an error.
Upgraded to Python 2.7.5 and it just worked.
In my django application I had the same error. Version Django 4, Celery 5. My my problem is that I run Celery from directory with file celery.py, but it is necessary from directory with manage.py.
Slava Ukraini!
Before posting this, i'ved tried a wide variety of possibilities for getting this custom script to work (with no success). I have a script in a /lib directory which is being run in the crontab every X minutes.
The first few lines of this script are:
# /project/lib/script.py
import os
import sys
def setup_environment():
pathname = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, pathname)
sys.path.insert(0, os.path.abspath(os.path.join(pathname, '..')))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Project.settings")
setup_environment()
from Model.models import Model # This works perfectly fine
The traceback for the exception does not occur in that file but in the Model.py.
#/project/Model/models.py
import os
print os.environ["DJANGO_SETTINGS_MODULE"] # Prints out Project.settings as expec
from django.db import models # ERROR OCCURS ON THIS LINE
Any help will be appreciated.
Here is the trace error:
Traceback (most recent call last):
File "/home/username/webapps/folder/Project/scripts/standalone.py", line 29, in
<module> from Model.models import Model File "/home/username/webapps/folder/
Project/App/models.py", line 15, in <module> from django.db import models
ImportError: No module named django.db
This code works when run from the command line, but fails in crontab.
Something that may be notable. The above ^ process used to work 100%, but after I added a few more scripts to the equation things began to break. I removed everything I had added .. however now it won't load anything django related!! It's not just db.models, any django related code I try to input in models.py is rejected.
Thank you.
Without any code changing, my django app started throwing an exception while loading the WSGI script. I'm using django 1.3 with python 2.7, and the top-level .wsgi is essentially unmodified from the default:
import os
import sys
from django.core.handlers.wsgi import WSGIHandler
os.environ['DJANGO_SETTINGS_MODULE'] = 'api.settings'
application = WSGIHandler()
It started producing these errors on any request, as reported by Apache:
mod_wsgi (pid=3283): Target WSGI script '/home/beder/webapps/api/api.wsgi' cannot be loaded as Python module.
mod_wsgi (pid=3283): Exception occurred processing WSGI script '/home/beder/webapps/api/api.wsgi'.
Traceback (most recent call last):
File "/home/beder/webapps/api/api.wsgi", line 4, in <module>
from django.core.handlers.wsgi import WSGIHandler
File "/home/beder/webapps/api/lib/python2.7/django/core/handlers/wsgi.py", line 10, in <module>
from django import http
File "/home/beder/webapps/api/lib/python2.7/django/http/__init__.py", line 122, in <module>
from django.utils.http import cookie_date
File "/home/beder/webapps/api/lib/python2.7/django/utils/http.py", line 7, in <module>
from email.Utils import formatdate
File "/usr/local/lib/python2.7/email/__init__.py", line 79, in __getattr__
__import__(self.__name__)
File "/usr/local/lib/python2.7/email/utils.py", line 27, in <module>
import random
File "/usr/local/lib/python2.7/random.py", line 47, in <module>
from os import urandom as _urandom
ImportError: cannot import name urandom
I restarted the server, and it now works normally (no errors). I'm at a loss for what to do - I want to make sure this doesn't happen again, but it's not happening now, and I have no idea why that import error appeared.
Are you executing Django inside a virtualenv? Did you updated or upgraded your system? If so and you upgraded to Python 2.7 from Python 2.6 for example you need to regenerate the virtualenv:
$ virtualenv [your-options] [your-django-project-directory]
For anyone using webfaction like Jesse and myself to run his django app but is not using virtualenv, it can happen that after the upgrade, the custom apache installation for the webapp was never restarted. This means that the stdlib changed but apache still uses python 2.7.2 because its the version it has loaded into memory.
The solution in this case is simple: login to your account via ssh and execute:
[username#webXX ~]$ ~/webapps/<webapp-name>/apache2/bin/restart
This restarts the web server and causes apache to reload the new interpreter binary.
I also found the same error when I updated Python 2.6.5 to 2.6.8, building from source. I also had built mod_wsgi from source, and initially forgot to recompile mod_wsgi for the new version of Python, and that led to the same error concerning urandom.