Python中Django使用uWSGI启动时遇到import错误,求指导

最开始试用的 django rest 没有问题跑了一段时间。新加了 celery 模块部署到服务器一直报下面的错误。现在使用 runserver 启动也是没问题的,使用 uwsgi 启动就会报错。
from . import sasl
ImportError: cannot import name sasl



下面是启动报错信息。
uwsgi --ini mysite_uwsgi.ini
[uWSGI] getting INI configuration from mysite_uwsgi.ini
*** Starting uWSGI 2.0.15 (64bit) on [Fri Jul 14 08:48:30 2017] ***
compiled with version: 4.4.7 20120313 (Red Hat 4.4.7-18) on 14 July 2017 08:26:28
os: Linux-2.6.32-696.1.1.el6.x86_64 #1 SMP Tue Apr 11 17:13:24 UTC 2017
nodename: jccs_no-pt-hc-vm1
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 2
current working directory: /usr/local/tutorial
detected binary path: /usr/local/bin/uwsgi
uWSGI running as root, you can use --uid/–gid/–chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
chdir() to /usr/local/tutorial
your processes number limit is 39168
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address /usr/local/tutorial/tutorial/mysite.sock fd 3
Python version: 2.7.13 (default, Mar 13 2017, 10:23:38) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0xa2fc70
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 800448 bytes (781 KB) for 10 cores
*** Operational MODE: preforking ***
Traceback (most recent call last):
File “/usr/local/tutorial/tutorial/wsgi.py”, line 21, in <module>
application = get_wsgi_application()
File “/usr/local/lib/python2.7/site-packages/django/core/wsgi.py”, line 13, in get_wsgi_application
django.setup(set_prefix=False)
File “/usr/local/lib/python2.7/site-packages/django/init.py”, line 22, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File “/usr/local/lib/python2.7/site-packages/django/conf/init.py”, line 53, in getattr
self._setup(name)
File “/usr/local/lib/python2.7/site-packages/django/conf/init.py”, line 41, in _setup
self._wrapped = Settings(settings_module)
File “/usr/local/lib/python2.7/site-packages/django/conf/init.py”, line 97, in init
mod = importlib.import_module(self.SETTINGS_MODULE)
File “/usr/local/lib/python2.7/importlib/init.py”, line 37, in import_module
import(name)
File “/usr/local/tutorial/tutorial/init.py”, line 5, in <module>
from .celery import app as celery_app
File “/usr/local/tutorial/tutorial/celery.py”, line 3, in <module>
from celery import Celery
File “/usr/local/lib/python2.7/site-packages/celery/local.py”, line 509, in getattr
module = import(self._object_origins[name], None, None, [name])
File “/usr/local/lib/python2.7/site-packages/celery/app/init.py”, line 5, in <module>
from celery import _state
File “/usr/local/lib/python2.7/site-packages/celery/_state.py”, line 15, in <module>
from celery.utils.threads import LocalStack
File “/usr/local/lib/python2.7/site-packages/celery/utils/init.py”, line 10, in <module>
from .nodenames import worker_direct, nodename, nodesplit
File “/usr/local/lib/python2.7/site-packages/celery/utils/nodenames.py”, line 7, in <module>
from kombu.entity import Exchange, Queue
File “/usr/local/lib/python2.7/site-packages/kombu/entity.py”, line 6, in <module>
from .abstract import MaybeChannelBound, Object
File “/usr/local/lib/python2.7/site-packages/kombu/abstract.py”, line 6, in <module>
from .connection import maybe_channel
File “/usr/local/lib/python2.7/site-packages/kombu/connection.py”, line 15, in <module>
from kombu import exceptions
File “/usr/local/lib/python2.7/site-packages/kombu/exceptions.py”, line 6, in <module>
from amqp import ChannelError, Connecti onError, ResourceError
File “/usr/local/lib/python2.7/site-packages/amqp/init.py”, line 47, in <module>
from .connection import Connection # noqa
File “/usr/local/lib/python2.7/site-packages/amqp/connection.py”, line 27, in <module>
from . import sasl
ImportError: cannot import name sasl
unable to load app 0 (mountpoint=’’) (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 14535)
spawned uWSGI worker 1 (pid: 14539, cores: 1)
spawned uWSGI worker 2 (pid: 14540, cores: 1)
spawned uWSGI worker 3 (pid: 14541, cores: 1)
spawned uWSGI worker 4 (pid: 14542, cores: 1)
spawned uWSGI worker 5 (pid: 14543, cores: 1)
spawned uWSGI worker 6 (pid: 14544, cores: 1)
spawned uWSGI worker 7 (pid: 14545, cores: 1)
spawned uWSGI worker 8 (pid: 14546, cores: 1)
spawned uWSGI worker 9 (pid: 14547, cores: 1)
spawned uWSGI worker 10 (pid: 14548, cores: 1)
^CSIGINT/SIGQUIT received…killing workers…
worker 1 buried after 1 seconds
worker 2 buried after 1 seconds
worker 3 buried after 1 seconds
worker 4 buried after 1 seconds
worker 5 buried after 1 seconds
worker 6 buried after 1 seconds
worker 7 buried after 1 seconds
worker 8 buried after 1 seconds
worker 9 buried after 1 seconds
worker 10 buried after 1 seconds
goodbye to uWSGI.
Python中Django使用uWSGI启动时遇到import错误,求指导


20 回复

from celery import Celery 这个文件不要叫 celery.py   改成_celery.py


这个问题我遇到过,典型的uWSGI环境配置问题。核心原因是uWSGI的Python解释器环境和你开发环境不一致。

先看一个完整的uWSGI配置文件示例:

# uwsgi.ini
[uwsgi]
# 项目目录
chdir = /path/to/your/project
# Django的wsgi模块路径
module = your_project.wsgi:application
# 虚拟环境路径(关键!)
home = /path/to/your/venv
# 主进程
master = true
# 进程数
processes = 4
# 线程数
threads = 2
# socket文件位置
socket = /tmp/your_project.sock
# socket权限
chmod-socket = 664
# 退出时清理环境
vacuum = true
# 日志文件
daemonize = /var/log/uwsgi/your_project.log

最重要的就是home参数,必须指向你的虚拟环境路径。如果没有这个,uWSGI会用系统Python,自然找不到Django和其他包。

检查步骤:

  1. 确认虚拟环境激活且安装了所有依赖:pip freeze > requirements.txt
  2. 在uWSGI配置中正确设置虚拟环境路径
  3. 确保uWSGI和项目使用相同Python版本:uwsgi --python-version

如果还是有问题,可以在启动时添加--py-autoreload=1方便调试,但生产环境要去掉。

总结:检查uWSGI配置中的虚拟环境路径是否正确。

谢谢。改了 还是同样的报错。我在 mac 上使用 uwsgi 跑一直没问题。但是放到 centos 就是这个报错

from .celery import app as celery_app  这一句也要改一下  from ._celery import app as celery_app

嗯 改了 还是同样的报错

ImportError,这种类型的错,挺好定位的呀。

检查包是否有安装,与开发环境的版本是否相同。以及上下文的环境是否相同。

还有就是 uwsgi 的 chdir 这个配置是否正确。

你需要 Docker

与我 mac 开发环境代码相同 uwsgi 版本相同 有检查办法吗 求赐教一个

第三方的一些包安装方式都一样么?都是 pip 安装的么?以及 uwsgi 的 chdir 配置

是 pip 离线安装的,chdir 是对的 在我的 mac 上跑没问题

[’/usr/local/tutorial’, ‘.’, ‘’, ‘/usr/local/lib/python27.zip’, ‘/usr/local/lib/python2.7’, ‘/usr/local/lib/python2.7/plat-linux2’, ‘/usr/local/lib/python2.7/lib-tk’, ‘/usr/local/lib/python2.7/lib-old’, ‘/usr/local/lib/python2.7/lib-dynload’, ‘/usr/local/lib/python2.7/site-packages’]

from amqp import ChannelError, Connecti onError, ResourceError
单独的交互式的 python shell 里。。执行 import 会报错么

这样是没问题的 实在不行重装机器 这次没用 venv

换了机器 装了 env 还是不行 ,我觉得我要崩溃了

python 或者 uwsgi 版本有没有问题?

manager.py runserver 跑没有问题

uwsgi 版本 2.0.15 不行 换了 2.0.14 可以 非常感谢

uwsgi 版本 2.0.15 不行 换了 2.0.14 可以 非常感谢

回到顶部