简体   繁体   中英

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

As I said everything is working on my system, but when I pushed code to my server (I replace my app directory with a new updated code directory and manually add "taggit" in my installedapp in settings.py file) after that run all the required cmd then I run "sudo service apache2 reload". I got 500 Internal server error.

I read lots of solutions but nothing works for me. When I undo the changes every thing work fine website comes live.

I install a taggit module using pip as shown below:

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)

Then I try to import the module from shell It get imported:

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 error log

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

Here is the problem error log

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'

This is how I configure Apache attached apache config file. Apache server config file .

I am new or first time asking If any more info required plz let me know.

Update1: I want to make it clear that I am not using a virtual environment and when I installed "django-crispy-forms" it works fine. I didn't get it why it can't find the "taggit" module if it can use the django-crispy-forms in the same way.

Any suggestions will be helpful. Update2: I figured something when I check sys.path in python shell I don't get my project path but in manage.py shell my project path is there does it make some sense.

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

Still I didn't get it why the django can't import the taggit from my main system environment but some how I made it to work.

I install the taggit in my django project directory(/mysite) so that django can access taggit from there using the following command.

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

then I just reload the apache server and itworks fine. This is not the good way to use module like this because it create a lots of extra files in my directory butfine for now.

If I comeup with the exact solution I will update I here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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