1

I have the same problem as here:

ImportError: No module named django.core.wsgi Apache + VirtualEnv + AWS + WSGI

but the solution is not working for me.

I actually copied the more refined example offered here:

https://pseudoscripter.wordpress.com/2015/10/23/import-error-no-module-named-django-core-wsgi/

I completely messed up on the previous EC2 instance, so I decided to create a new EC2 instance and start over.

Some commands I ran to setup the server:

yum upgrade -y 

yum update -y

yum search python3 | grep devel

yum install python3-pip.noarch -y 

yum install emacs -y

yum install make automake gcc gcc-c++ kernel-devel -y 

yum install git -y

yum install nginx -y

yum install postgresql-devel  -y 

yum install libtiff-devel libjpeg-devel libzip-devel freetype-devel lcms2-devel libwebp-devel tcl-devel libxslt-devel libxml2-devel -y

yum install openssl-devel -y

yum install python-virtualenv.noarch

amazon-linux-extras install nginx1.12

Then I did some work setting up Nginx:

How to install nginx 1.9.15 on amazon linux disto

sudo chmod 755 /etc/rc.d/init.d/nginx

sudo service nginx start

sudo chkconfig nginx on

Then I used scp to copy our Django app to this path:

/usr/share/lynette-ecomerce-demo

Then, as the regular EC2 user ec2-user I ran:

source bin/activate

pip install Django

pip install gunicorn

If I run python and then this:

>>> import sys
>>> print(sys.prefix)

/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django

I didn't use pip3 because that is what seemed to create all of the problems last time. But now I do:

gunicorn  ecommerce.wsgi:application --bind 0.0.0.0:8000

I got the "ImportError: No module named django.core.wsgi" error so I followed the advice given elsewhere and I opened this file:

ecommerce/wsgi.py

and I edited it like this:

"""                                                                                                                                                                      
WSGI config for ecommerce project.                                                                                                                                       

It exposes the WSGI callable as a module-level variable named ``application``.                                                                                           

For more information on this file, see                                                                                                                                   
https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/                                                                                                             
"""

import os
import sys

sys.path.append("/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/lib/python2.7/site-packages")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ecommerce.settings")


from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ecommerce.settings')

application = get_wsgi_application()

but now if I do this:

gunicorn  ecommerce.wsgi:application --bind 0.0.0.0:8000

I still get this:

[2018-08-21 15:09:27 +0000] [18310] [INFO] Starting gunicorn 19.9.0
[2018-08-21 15:09:27 +0000] [18310] [INFO] Listening at: http://0.0.0.0:8000 (18310)
[2018-08-21 15:09:27 +0000] [18310] [INFO] Using worker: sync
[2018-08-21 15:09:27 +0000] [18314] [INFO] Booting worker with pid: 18314
[2018-08-21 15:09:27 +0000] [18314] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/lib/python2.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
    worker.init_process()
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/lib/python2.7/site-packages/gunicorn/workers/base.py", line 129, in init_process
    self.load_wsgi()
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/lib/python2.7/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/lib/python2.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
    return self.load_wsgiapp()
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/lib/python2.7/site-packages/gunicorn/util.py", line 350, in import_app
    __import__(module)
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/ecommerce/wsgi.py", line 17, in <module>
    from django.core.wsgi import get_wsgi_application
ImportError: No module named django.core.wsgi

Any suggestions?

If I do this:

pip list

I see:

gunicorn (19.9.0)
http (0.02)
pip (1.4.1)
setuptools (0.9.8)
wsgiref (0.1.2)

I could try to add to the PYTHONPATH:

 --pythonpath /usr/share/lynette-ecomerce-demo/ecommerce-site/version1/lib/python3.7/site-packages

So the full command is:

gunicorn  ecommerce.wsgi:application --bind 0.0.0.0:8000  --pythonpath /usr/share/lynette-ecomerce-demo/ecommerce-site/version1/lib/python3.7/site-packages

and when I try that I get:

[2018-08-21 16:26:27 +0000] [18604] [INFO] Starting gunicorn 19.9.0
[2018-08-21 16:26:27 +0000] [18604] [INFO] Listening at: http://0.0.0.0:8000 (18604)
[2018-08-21 16:26:27 +0000] [18604] [INFO] Using worker: sync
[2018-08-21 16:26:27 +0000] [18608] [INFO] Booting worker with pid: 18608
[2018-08-21 16:26:27 +0000] [18608] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/lib/python2.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
    worker.init_process()
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/lib/python2.7/site-packages/gunicorn/workers/base.py", line 129, in init_process
    self.load_wsgi()
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/lib/python2.7/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/lib/python2.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
    return self.load_wsgiapp()
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/lib/python2.7/site-packages/gunicorn/util.py", line 350, in import_app
    __import__(module)
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/ecommerce/wsgi.py", line 17, in <module>
    from django.core.wsgi import get_wsgi_application
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/lib/python3.7/site-packages/django/__init__.py", line 1, in <module>
    from django.utils.version import get_version
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/lib/python3.7/site-packages/django/utils/version.py", line 71, in <module>
    @functools.lru_cache()
AttributeError: 'module' object has no attribute 'lru_cache'
[2018-08-21 16:26:27 +0000] [18608] [INFO] Worker exiting (pid: 18608)
[2018-08-21 16:26:27 +0000] [18604] [INFO] Shutting down: Master
[2018-08-21 16:26:27 +0000] [18604] [INFO] Reason: Worker failed to boot.                            

If I do this:

 find . -name site-packages

I see:

./django/lib/python2.7/site-packages
./django/lib64/python2.7/site-packages
./lib/python3.7/site-packages
JeffGallant
  • 409
  • 2
  • 6
  • 17
  • What is your django version? – Sachin Aug 21 '18 at 15:31
  • Your file path "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/ecommerce/wsgi.py" contains a directory named `django`. If `/usr/share/lynette-ecomerce-demo/ecommerce-site/version1` is in your PYTHONPATH, then that django module is being imported instead of the "real" one. – John Gordon Aug 21 '18 at 15:38
  • did you copy the virtual environment as well? please post the output of `pip list`. – Sachin Aug 21 '18 at 15:45
  • @JohnGordon -- how would i fix that? – JeffGallant Aug 21 '18 at 16:18
  • @SachinKukreja -- I've added the output of `pip list` – JeffGallant Aug 21 '18 at 16:19
  • Django version 2, using Python 3.7 – JeffGallant Aug 21 '18 at 16:20
  • 1
    do you see that there is no `Django` listed in the output of pip list? I think creating a new virtual environment and re installing the requirements will fix it. – Sachin Aug 21 '18 at 16:31
  • @SachinKukreja -- I see that, but I have run "pip install Django" many times. Why is it not there? – JeffGallant Aug 21 '18 at 16:32
  • well, when I copy pasted a virtual environment from one machine to another, it seems to be broken. I didnt explore the reason. I guess you have to search for it to know why that happens. – Sachin Aug 21 '18 at 16:33
  • @SachinKukreja -- also, do you know why things don't work when I put in the path to the Python 3 libraries? My understanding is that Django 2.7 needs Python 3. – JeffGallant Aug 21 '18 at 16:33
  • @SachinKukreja -- I am new to Python, so let me be sure I understand your terms. When you say "delete the virtualenv" do you mean delete the folders `./lib/python2.7/site-packages` and `./lib64/python2.7/site-packages` – JeffGallant Aug 21 '18 at 16:35
  • Reinstalling, should I use "pip" or "pip3"? – JeffGallant Aug 21 '18 at 16:39
  • I mean delete the named directory which contains `bin`, `lib`, etc. – Sachin Aug 21 '18 at 16:44
  • @SachinKukreja -- `/usr/share/lynette-ecomerce-demo/ecommerce-site/version1` contains 4 directories, including `bin`, `lib`, `django` (which holds all the project code) and `include`. I think you are saying that I should delete the `bin` and `lib` and `include`? – JeffGallant Aug 21 '18 at 16:46
  • Yes, the files and directories that are used by virtual env. – Sachin Aug 21 '18 at 16:47
  • Hmm, my project is inside of that directory, so I can not delete the whole thing. – JeffGallant Aug 21 '18 at 16:54
  • I think you have jumbled up your project directory structure. I think you should separate the virtual environment from the django project. For that, you need to take out your entire project files to a separate directory. Delete the old virtual env and create a new one outside of it. Activate and install dependencies. Use gunicorn from `venv_name/bin` directory. Also, create the virtualenv for python3.7, if you are going to use Django >= 2. – Sachin Aug 21 '18 at 17:32
  • How do I create a virtualenv for Python 3.7? I mean, how do I control the versin? – JeffGallant Aug 21 '18 at 17:46

0 Answers0