最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【未解决】CentOS中Django用python3 manage.py collectstatic出错:ImportError Couldnt import Django

CentOS crifan 1765浏览 0评论

折腾:

【未解决】Django后台管理页面样式错乱

期间,去尝试运行:

python3 manage.py collectstatic --settings=conf.production.settings

去生成static的文件,结果出错:

[root@x-general-01 xx]# python3 manage.py collectstatic --settings=conf.production.settings
Traceback (most recent call last):
  File "manage.py", line 8, in <module>
    from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 14, in <module>
    ) from exc
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?

结果出错。

貌似没有进入虚拟环境?

之前管理应用用的是:

supervisorctl restart gunicorn

所以去看看:

[root@x-general-01 xx]# supervisorctl status
gunicorn                         RUNNING   pid 16353, uptime 1 day, 0:41:14
redis                            RUNNING   pid 21685, uptime 29 days, 5:13:56
robotDemo                        RUNNING   pid 21683, uptime 29 days, 5:13:56
robotDemo_CeleryBeat             RUNNING   pid 21684, uptime 29 days, 5:13:56
robotDemo_CeleryWorker           RUNNING   pid 21687, uptime 29 days, 5:13:56
[root@x-general-01 x]# supervisorctl status gunicorn
gunicorn                         RUNNING   pid 16353, uptime 1 day, 0:42:42

很明显是:

此处的supervisor管理的app的名字叫做:gunicorn(很弱智的起名,应该改为当前程序的名字x或xx或xxx)

然后要去看看supervisor的gunicorn配置是啥,然后再去看看是否用到了虚拟环境

supervisor 配置文件

找到了是:

[root@x-general-01 x]# cat /etc/supervisord.d/gunicorn.conf
[program:gunicorn]
directory=/root/xxx/web/server/x/
command=gunicorn --workers 4 --bind unix:/root/x/web/server/xServer.sock xServer.wsgi
autostart=false
autorestart=false
stdout_logfile=/root/x/web/server/x/gunicorn.out.log
stderr_logfile=/root/x/web/server/xx/gunicorn.err.log
user=root
group=root

但是还是没看到python用的是哪个版本,是系统的python3还是虚拟环境的?

再去找:

xx.wsgi

发现是:

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "conf.production.settings")

application = get_wsgi_application()

也没什么特殊的,没有找到哪里设置了python解析器的版本

然后重新安装试试:

[root@xx-general-01 xxsServer]# pip3 install -r requirements.txt
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Requirement already satisfied: Django==2.0.6 in /usr/lib/python3.4/site-packages (from -r requirements.txt (line 1)) (2.0.6)
Requirement already satisfied: mysqlclient==1.3.12 in /usr/lib64/python3.4/site-packages (from -r requirements.txt (line 2)) (1.3.12)
Requirement already satisfied: pymongo==3.6.1 in /usr/lib64/python3.4/site-packages (from -r requirements.txt (line 3)) (3.6.1)
Requirement already satisfied: ipython==6.4.0 in /usr/lib/python3.4/site-packages (from -r requirements.txt (line 4)) (6.4.0)
Requirement already satisfied: Werkzeug==0.14.1 in /usr/lib/python3.4/site-packages (from -r requirements.txt (line 5)) (0.14.1)
Requirement already satisfied: django-cors-headers==2.2.0 in /usr/lib/python3.4/site-packages (from -r requirements.txt (line 6)) (2.2.0)
Requirement already satisfied: djangorestframework==3.8.2 in /usr/lib/python3.4/site-packages (from -r requirements.txt (line 7)) (3.8.2)
Requirement already satisfied: djangorestframework-jwt==1.11.0 in /usr/lib/python3.4/site-packages (from -r requirements.txt (line 8)) (1.11.0)
Requirement already satisfied: django-rest-swagger==2.2.0 in /usr/lib/python3.4/site-packages (from -r requirements.txt (line 9)) (2.2.0)
Requirement already satisfied: python-dotenv==0.7.1 in /usr/lib/python3.4/site-packages (from -r requirements.txt (line 10)) (0.7.1)
Requirement already satisfied: django-extensions==1.9.8 in /usr/lib/python3.4/site-packages (from -r requirements.txt (line 11)) (1.9.8)
Requirement already satisfied: gunicorn==19.8.1 in /usr/lib/python3.4/site-packages (from -r requirements.txt (line 12)) (19.8.1)
Requirement already satisfied: python-docx==0.8.6 in /usr/lib/python3.4/site-packages (from -r requirements.txt (line 13)) (0.8.6)
Requirement already satisfied: pytest==3.6.0 in /usr/lib/python3.4/site-packages (from -r requirements.txt (line 14)) (3.6.0)
Requirement already satisfied: nose==1.3.7 in /usr/lib/python3.4/site-packages (from -r requirements.txt (line 15)) (1.3.7)
Requirement already satisfied: Faker==0.8.15 in /usr/lib/python3.4/site-packages (from -r requirements.txt (line 16)) (0.8.15)
Requirement already satisfied: factory-boy==2.11.1 in /usr/lib/python3.4/site-packages (from -r requirements.txt (line 17)) (2.11.1)
Requirement already satisfied: pytest-django==3.2.1 in /usr/lib/python3.4/site-packages (from -r requirements.txt (line 18)) (3.2.1)
Requirement already satisfied: pytest-sugar==0.9.1 in /usr/lib/python3.4/site-packages (from -r requirements.txt (line 19)) (0.9.1)
Requirement already satisfied: raven==6.9.0 in /usr/lib/python3.4/site-packages (from -r requirements.txt (line 20)) (6.9.0)
Requirement already satisfied: django-sendfile==0.3.11 in /usr/lib/python3.4/site-packages (from -r requirements.txt (line 21)) (0.3.11)
Requirement already satisfied: pytz in /usr/lib/python3.4/site-packages (from Django==2.0.6->-r requirements.txt (line 1)) (2018.4)
Requirement already satisfied: pexpect; sys_platform != "win32" in /usr/lib/python3.4/site-packages (from ipython==6.4.0->-r requirements.txt (line 4)) (4.6.0)
Requirement already satisfied: backcall in /usr/lib/python3.4/site-packages (from ipython==6.4.0->-r requirements.txt (line 4)) (0.1.0)
Requirement already satisfied: decorator in /usr/lib/python3.4/site-packages (from ipython==6.4.0->-r requirements.txt (line 4)) (4.3.0)
Requirement already satisfied: pickleshare in /usr/lib/python3.4/site-packages (from ipython==6.4.0->-r requirements.txt (line 4)) (0.7.4)
Requirement already satisfied: setuptools>=18.5 in /usr/lib/python3.4/site-packages (from ipython==6.4.0->-r requirements.txt (line 4)) (39.0.1)
Requirement already satisfied: jedi>=0.10 in /usr/lib/python3.4/site-packages (from ipython==6.4.0->-r requirements.txt (line 4)) (0.12.0)
Requirement already satisfied: pygments in /usr/lib64/python3.4/site-packages (from ipython==6.4.0->-r requirements.txt (line 4)) (2.2.0)
Requirement already satisfied: typing; python_version <= "3.4" in /usr/lib/python3.4/site-packages (from ipython==6.4.0->-r requirements.txt (line 4)) (3.6.4)
Requirement already satisfied: simplegeneric>0.8 in /usr/lib/python3.4/site-packages (from ipython==6.4.0->-r requirements.txt (line 4)) (0.8.1)
Requirement already satisfied: traitlets>=4.2 in /usr/lib/python3.4/site-packages (from ipython==6.4.0->-r requirements.txt (line 4)) (4.3.2)
Requirement already satisfied: prompt-toolkit<2.0.0,>=1.0.15 in /usr/lib/python3.4/site-packages (from ipython==6.4.0->-r requirements.txt (line 4)) (1.0.15)
Requirement already satisfied: PyJWT<2.0.0,>=1.5.2 in /usr/lib/python3.4/site-packages (from djangorestframework-jwt==1.11.0->-r requirements.txt (line 8)) (1.6.4)
Requirement already satisfied: openapi-codec>=1.3.1 in /usr/lib/python3.4/site-packages (from django-rest-swagger==2.2.0->-r requirements.txt (line 9)) (1.3.2)
Requirement already satisfied: coreapi>=2.3.0 in /usr/lib/python3.4/site-packages (from django-rest-swagger==2.2.0->-r requirements.txt (line 9)) (2.3.3)
Requirement already satisfied: simplejson in /usr/lib64/python3.4/site-packages (from django-rest-swagger==2.2.0->-r requirements.txt (line 9)) (3.15.0)
Requirement already satisfied: click>=5.0 in /usr/lib/python3.4/site-packages (from python-dotenv==0.7.1->-r requirements.txt (line 10)) (6.7)
Requirement already satisfied: six>=1.2 in /usr/lib/python3.4/site-packages (from django-extensions==1.9.8->-r requirements.txt (line 11)) (1.11.0)
Requirement already satisfied: lxml>=2.3.2 in /usr/lib64/python3.4/site-packages (from python-docx==0.8.6->-r requirements.txt (line 13)) (4.2.1)
Requirement already satisfied: more-itertools>=4.0.0 in /usr/lib/python3.4/site-packages (from pytest==3.6.0->-r requirements.txt (line 14)) (4.2.0)
Requirement already satisfied: py>=1.5.0 in /usr/lib/python3.4/site-packages (from pytest==3.6.0->-r requirements.txt (line 14)) (1.5.3)
Requirement already satisfied: pluggy<0.7,>=0.5 in /usr/lib/python3.4/site-packages (from pytest==3.6.0->-r requirements.txt (line 14)) (0.6.0)
Requirement already satisfied: atomicwrites>=1.0 in /usr/lib/python3.4/site-packages (from pytest==3.6.0->-r requirements.txt (line 14)) (1.1.5)
Requirement already satisfied: attrs>=17.4.0 in /usr/lib/python3.4/site-packages (from pytest==3.6.0->-r requirements.txt (line 14)) (18.1.0)
Requirement already satisfied: python-dateutil>=2.4 in /usr/lib/python3.4/site-packages (from Faker==0.8.15->-r requirements.txt (line 16)) (2.7.3)
Requirement already satisfied: text-unidecode==1.2 in /usr/lib/python3.4/site-packages (from Faker==0.8.15->-r requirements.txt (line 16)) (1.2)
Requirement already satisfied: termcolor>=1.1.0 in /usr/lib/python3.4/site-packages (from pytest-sugar==0.9.1->-r requirements.txt (line 19)) (1.1.0)
Requirement already satisfied: ptyprocess>=0.5 in /usr/lib/python3.4/site-packages (from pexpect; sys_platform != "win32"->ipython==6.4.0->-r requirements.txt (line 4)) (0.5.2)
Requirement already satisfied: parso>=0.2.0 in /usr/lib/python3.4/site-packages (from jedi>=0.10->ipython==6.4.0->-r requirements.txt (line 4)) (0.2.1)
Requirement already satisfied: ipython-genutils in /usr/lib/python3.4/site-packages (from traitlets>=4.2->ipython==6.4.0->-r requirements.txt (line 4)) (0.2.0)
Requirement already satisfied: wcwidth in /usr/lib/python3.4/site-packages (from prompt-toolkit<2.0.0,>=1.0.15->ipython==6.4.0->-r requirements.txt (line 4)) (0.1.7)
Requirement already satisfied: requests in /usr/lib/python3.4/site-packages (from coreapi>=2.3.0->django-rest-swagger==2.2.0->-r requirements.txt (line 9)) (2.18.4)
Requirement already satisfied: coreschema in /usr/lib/python3.4/site-packages (from coreapi>=2.3.0->django-rest-swagger==2.2.0->-r requirements.txt (line 9)) (0.0.4)
Requirement already satisfied: itypes in /usr/lib/python3.4/site-packages (from coreapi>=2.3.0->django-rest-swagger==2.2.0->-r requirements.txt (line 9)) (1.1.0)
Requirement already satisfied: uritemplate in /usr/lib/python3.4/site-packages (from coreapi>=2.3.0->django-rest-swagger==2.2.0->-r requirements.txt (line 9)) (3.0.0)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /usr/lib/python3.4/site-packages (from requests->coreapi>=2.3.0->django-rest-swagger==2.2.0->-r requirements.txt (line 9)) (3.0.4)
Requirement already satisfied: idna<2.7,>=2.5 in /usr/lib/python3.4/site-packages (from requests->coreapi>=2.3.0->django-rest-swagger==2.2.0->-r requirements.txt (line 9)) (2.6)
Requirement already satisfied: urllib3<1.23,>=1.21.1 in /usr/lib/python3.4/site-packages (from requests->coreapi>=2.3.0->django-rest-swagger==2.2.0->-r requirements.txt (line 9)) (1.22)
Requirement already satisfied: certifi>=2017.4.17 in /usr/lib/python3.4/site-packages (from requests->coreapi>=2.3.0->django-rest-swagger==2.2.0->-r requirements.txt (line 9)) (2018.1.18)
Requirement already satisfied: jinja2 in /usr/lib64/python3.4/site-packages (from coreschema->coreapi>=2.3.0->django-rest-swagger==2.2.0->-r requirements.txt (line 9)) (2.10)
Requirement already satisfied: MarkupSafe>=0.23 in /usr/lib64/python3.4/site-packages (from jinja2->coreschema->coreapi>=2.3.0->django-rest-swagger==2.2.0->-r requirements.txt (line 9)) (1.0)

也没有变化,之前都安装过了。

那为何:

python3 运行还会报错呢?

python3 manage.py ImportError: Couldn’t import Django

ImportError: Couldn’t import Django – Stack Overflow

推荐用虚拟环境

但是此处暂时不想去用,因为之前就没用,懒得去加

python – ImportError: Couldn’t import Django. Are you sure it’s installed and available on your PYTHONPATH environment variable? Did you forget to activ – Stack Overflow

Django笔记:常见故障排除 – zhangjpn – 博客园

Can only get “manage.py runserver” if I use python3 instead of python – Google Groups

【未解决】CentOS中pip3和Python3所在路径不一致

TODO:去换用虚拟环境去解决此处问题

转载请注明:在路上 » 【未解决】CentOS中Django用python3 manage.py collectstatic出错:ImportError Couldnt import Django

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.205 seconds, using 22.22MB memory