django apache在ubuntu上部署时发生importerror: No Module named 'django'错误

环境:

ubuntu16.04.1 LTS

Python 3.5.2

Apache/2.4.18 (Ubuntu)

OpenSSL/1.0.2g

mod_wsgi/4.3.0

Python/3.5.2

Django 2.1.2

问题:在部署完成后apache的error.log报错ImportError: No module named 'django',同时服务器500

查看详细信息发现python环境与安装django时一样排除python环境版本不正确的问题


[Tue Oct 30 09:54:13.482587 2018] [wsgi:warn] [pid 13678:tid 140365118388096] mod_wsgi: Compiled for Python/3.5.1+.
[Tue Oct 30 09:54:13.482593 2018] [wsgi:warn] [pid 13678:tid 140365118388096] mod_wsgi: Runtime using Python/3.5.2.
[Tue Oct 30 09:54:13.483150 2018] [mpm_event:notice] [pid 13678:tid 140365118388096] AH00489: Apache/2.4.18 (Ubuntu) OpenSSL/1.0.2g mod_wsgi/4.3.0 Python/3.5.2 configured -- resuming normal operations
[Tue Oct 30 09:54:13.483170 2018] [core:notice] [pid 13678:tid 140365118388096] AH00094: Command line: '/usr/sbin/apache2'
[Tue Oct 30 09:54:16.845781 2018] [wsgi:error] [pid 13681:tid 140364824405760] [client 101.90.255.187:26560] mod_wsgi (pid=13681): Target WSGI script '/home/ubuntu/django_project/mysite/mysite/wsgi.py' cannot be loaded as Python module.
[Tue Oct 30 09:54:16.845840 2018] [wsgi:error] [pid 13681:tid 140364824405760] [client 101.90.255.187:26560] mod_wsgi (pid=13681): Exception occurred processing WSGI script '/home/ubuntu/django_project/mysite/mysite/wsgi.py'.
[Tue Oct 30 09:54:16.846037 2018] [wsgi:error] [pid 13681:tid 140364824405760] [client 101.90.255.187:26560] Traceback (most recent call last):
[Tue Oct 30 09:54:16.846059 2018] [wsgi:error] [pid 13681:tid 140364824405760] [client 101.90.255.187:26560]   File "/home/ubuntu/django_project/mysite/mysite/wsgi.py", line 11, in <module>
[Tue Oct 30 09:54:16.846064 2018] [wsgi:error] [pid 13681:tid 140364824405760] [client 101.90.255.187:26560]     from django.core.wsgi import get_wsgi_application
[Tue Oct 30 09:54:16.846078 2018] [wsgi:error] [pid 13681:tid 140364824405760] [client 101.90.255.187:26560] ImportError: No module named 'django'

 查看django安装的位置

$ pip3 install django
Requirement already satisfied: django in ./.local/lib/python3.5/site-packages (2.1.2)
Requirement already satisfied: pytz in ./.local/lib/python3.5/site-packages (from django) (2018.5)

查看wsgi.py运行时的包检索路径

['/usr/lib/python35.zip', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-x86_64-linux-gnu', '/usr/lib/python3.5/lib-dynload', '/usr/local/lib/python3.5/dist-packages', '/usr/lib/python3/dist-packages']

发现没有django的安装路径

将该路径加入sys.path

import sys
sys.path.append(r"/home/ubuntu/.local/lib/python3.5/site-packages")

依然报错500

考虑权限问题,测试发现Permission denied

PermissionError: [Errno 13] Permission denied: '/home/ubuntu/.local/lib/python3.5/site-packages'

执行以下命令

cd /home/ubuntu/.local/lib/python3.5
sudo chmod -R 777 site-packages/

问题解决

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值