Django无法在apache服务器中导入taggit模块,在本地服务器中正常,从manage.py shell导入,但Django无法导入,未使用virtualenv

时间:2020-08-01 09:52:50

标签: python django apache virtualenv django-taggit

正如我所说的,一切都在系统上正常运行,但是当我将代码推送到服务器上时(我将应用程序目录替换为新的更新代码目录,并在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 server config file

我是新来的还是第一次问是否需要更多信息,请让我知道。

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']

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']

1 个答案:

答案 0 :(得分:0)

我还是不明白为什么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服务器,它就可以正常工作。 这不是使用这样的模块的好方法,因为它会在我的目录中创建很多额外的文件,但现在要优化。

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