Django 无法在 apache 服务器中导入 taggit 模块,在本地服务器中很好,manage.py 从 shell 导入但 django 无法导入它,没有使用 virtualenv



正如我所说,我的系统上一切都在运行,但当我将代码推送到服务器时(我用一个新的更新代码目录替换我的应用程序目录,并在settings.py文件中的installeddapp中手动添加"taggit"(,之后运行所有所需的cmd,然后我运行";sudo服务apache2重新加载";。我得到500内部服务器错误。

我读了很多解决方案,但没有什么对我有效。当我撤销更改时,一切都很好。网站上线了。

我使用pip安装一个taggit模块,如下所示:

Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: django-taggit in ./.local/lib/python3.6/site-packages (1.3.0)
Requirement already satisfied: Django>=1.11 in /usr/local/lib/python3.6/dist-packages (from django-taggit) (3.0.7)
Requirement already satisfied: asgiref~=3.2 in /usr/local/lib/python3.6/dist-packages (from Django>=1.11->django-taggit) (3.2.10)
Requirement already satisfied: pytz in /usr/local/lib/python3.6/dist-packages (from Django>=1.11->django-taggit) (2020.1)
Requirement already satisfied: sqlparse>=0.2.2 in /usr/local/lib/python3.6/dist-packages (from Django>=1.11->django-taggit) (0.3.1)

然后我尝试从shell导入模块。它被导入:

ubuntu@ip-172-31-35-150:/var/www/mysite$ python3 manage.py shell
Python 3.6.9 (default, Jul 17 2020, 12:50:27) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from taggit.models import Tag
>>> Tag
<class 'taggit.models.Tag'>

Apache错误日志

ubuntu@ip-172-31-35-150:/var/log/apache2$ tail error.log
[Fri Jul 31 08:29:23.722454 2020] [mpm_event:notice] [pid 26542:tid 139924014398400] AH00489: Apache/2.4.29 (Ubuntu) mod_wsgi/4.5.17 Python/3.6 configured -- resuming normal operations
[Fri Jul 31 08:29:23.722480 2020] [core:notice] [pid 26542:tid 139924014398400] AH00094: Command line: '/usr/sbin/apache2'
ubuntu

这是问题错误日志

ubuntu@ip-172-31-35-150:/var/log/apache2$ tail mysite-error.log
[Fri Jul 31 08:29:37.832247 2020] [wsgi:error] [pid 32108:tid 139923711420160] [remote 162.158.166.21:63794]   File "/usr/local/lib/python3.6/dist-packages/django/apps/config.py", line 90, in create
[Fri Jul 31 08:29:37.832251 2020] [wsgi:error] [pid 32108:tid 139923711420160] [remote 162.158.166.21:63794]     module = import_module(entry)
[Fri Jul 31 08:29:37.832257 2020] [wsgi:error] [pid 32108:tid 139923711420160] [remote 162.158.166.21:63794]   File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
[Fri Jul 31 08:29:37.832261 2020] [wsgi:error] [pid 32108:tid 139923711420160] [remote 162.158.166.21:63794]     return _bootstrap._gcd_import(name[level:], package, level)
[Fri Jul 31 08:29:37.832267 2020] [wsgi:error] [pid 32108:tid 139923711420160] [remote 162.158.166.21:63794]   File "<frozen importlib._bootstrap>", line 994, in _gcd_import
[Fri Jul 31 08:29:37.832274 2020] [wsgi:error] [pid 32108:tid 139923711420160] [remote 162.158.166.21:63794]   File "<frozen importlib._bootstrap>", line 971, in _find_and_load
[Fri Jul 31 08:29:37.832280 2020] [wsgi:error] [pid 32108:tid 139923711420160] [remote 162.158.166.21:63794]   File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
[Fri Jul 31 08:29:37.832297 2020] [wsgi:error] [pid 32108:tid 139923711420160] [remote 162.158.166.21:63794] ModuleNotFoundError: No module named 'taggit'

这就是我如何配置Apache附加的Apache配置文件。Apache服务器配置文件。

我是新来的或第一次问是否需要更多信息,请告诉我。

Update1:我想明确我没有使用虚拟环境;django脆型;它运行良好。我不明白为什么它找不到";塔吉特";模块,如果它可以以同样的方式使用django-crisp形式。

任何建议都会有所帮助。Update2:当我在python shell中检查sys.path时,我发现了一些问题。我没有得到我的项目路径,但在manage.py shell中,我的项目道路在那里,这有一定的意义吗。

Python3

ubuntu@ip-172-31-35-150:~$ python3
Python 3.6.9 (default, Jul 17 2020, 12:50:27) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/home/ubuntu/.local/lib/python3.6/site-packages', '/usr/local/lib/python3.6/dist-packages']

Manage.py shell

ubuntu@ip-172-31-35-150:/var/www/mysite$ python3 manage.py shell
Python 3.6.9 (default, Jul 17 2020, 12:50:27) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> sys.path
['/var/www/mysite', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/home/ubuntu/.local/lib/python3.6/site-packages', '/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages']

我仍然不明白为什么django不能从我的主系统环境中导入taggit,但我知道它是如何工作的。

我将taggit安装在我的django项目目录(/mysite(中,以便django可以使用以下命令从那里访问taggit。

ubuntu@ip-172-31-35-150:/var/www/mysite$ sudo python3 -m pip install --target=/var/www/mysite django-taggit
WARNING: The directory '/home/ubuntu/.cache/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting django-taggit
Downloading django_taggit-1.3.0-py3-none-any.whl (45 kB)
|████████████████████████████████| 45 kB 10.6 MB/s 
Collecting Django>=1.11
Downloading Django-3.0.8-py3-none-any.whl (7.5 MB)
|████████████████████████████████| 7.5 MB 11.2 MB/s 
Collecting pytz
Downloading pytz-2020.1-py2.py3-none-any.whl (510 kB)
|████████████████████████████████| 510 kB 49.7 MB/s 
Collecting asgiref~=3.2
Downloading asgiref-3.2.10-py3-none-any.whl (19 kB)
Collecting sqlparse>=0.2.2
Downloading sqlparse-0.3.1-py2.py3-none-any.whl (40 kB)
|████████████████████████████████| 40 kB 52.4 MB/s 
Installing collected packages: pytz, asgiref, sqlparse, Django, django-taggit
Successfully installed Django-3.0.8 asgiref-3.2.10 django-taggit-1.3.0 pytz-2020.1 sqlparse-0.3.1

然后我只需重新加载apache服务器,它就可以正常工作了。这不是使用这样的模块的好方法,因为它在我的目录中创建了很多额外的文件,但目前还可以。

如果我提出了确切的解决方案,我会在这里更新。