繁体   English   中英

Django can't import taggit module in apache server, fine in local server, Import from manage.py shell but django can't import it, No virtualenv used

[英]Django can't import taggit module in apache server, fine in local server, Import from manage.py shell but django can't import it, No virtualenv used

正如我所说的一切都在我的系统上运行,但是当我将代码推送到我的服务器时(我将我的应用程序目录替换为新的更新代码目录并在我的已安装应用程序的 settings.py 文件中手动添加“taggit”)之后运行所有需要 cmd 然后我运行“sudo service apache2 reload”。 我收到 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-crispy-forms”时它工作正常。 如果它可以以相同的方式使用 django-crispy-forms,我不明白为什么它找不到“taggit”模块。

任何建议都会有所帮助。 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']

管理.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 服务器,它工作正常。 这不是使用这样的模块的好方法,因为它在我的目录中创建了很多额外的文件,但现在还可以。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM