title: deploy django application with pipenv on apache
body: <p>I have created a python web application in <code>Django 2.0</code> using <code>pipenv</code> virtualenv</p>
  
  <p>Now, I have to host it on apache server. I have installed <code>libapache2-mod-wsgi-py3</code> and <code>python-setuptools</code> in the server.</p>
  
  <p>The structure of my application is like</p>
  
  <pre><code>myapp_dir
   |- myapp
      |- settings
         |- __init__.py
         |- production.py
      |- __init__.py
      |- urls.py
      |- wsgi.py
   |- otherapp
   |- templates
   |- static_my_project
   |- manage.py
   |- Pipfile
   |- Pipfile.lock
  </code></pre>
  
  <p>the path of the application to put on</p>
  
  <pre><code>/home/user/app.application.com/
  </code></pre>
  
  <p>I have moved all files to the directory and installed all dependencies from <code>Pipfile</code> by running in the directory </p>
  
  <pre><code>pipenv install
  </code></pre>
  
  <p>This has created a virtualenv and installed all required modules and the path of <code>pipenv --venv</code> gives</p>
  
  <pre><code># pipenv --venv
  /home/user/.local/share/virtualenvs/app.application.com-IuTkL8w_
  </code></pre>
  
  <p>My <code>VirtualHost</code> configuration looks like</p>
  
  <pre><code>ServerName app.application.com
  ServerAlias app.application.com
  
  ErrorLog /home/user/error.log
  CustomLog /home/user/custom.log combined
  
  Alias /static /home/user/app.application.com/static_my_project
  &lt;Directory /home/user/app.application.com/static_my_project&gt;
      Require all granted
  &lt;/Directory&gt;
  
  &lt;Directory /home/user/app.application.com/pricearbitrase&gt;
      &lt;Files wsgi.py&gt;
          Require all granted
      &lt;/Files&gt;
  &lt;/Directory&gt;
  
  WSGIScriptAlias / /home/user/app.application.com/myapp/wsgi.py
  </code></pre>
  
  <p>VirtualInclude contains</p>
  
  <pre><code>&lt;IfModule mod_wsgi&gt;
  
      WSGIPythonHome /home/user/.local/share/virtualenvs/app.application.com-IuTkL8w_
  
  &lt;/IfModule&gt;
  </code></pre>
  
  <p><strong>But on accessing <code>http://app.application.com</code> it gives <code>Internal server error</code> and the log file generated contains</strong> </p>
  
  <pre><code>[wsgi:error] [pid 60730] mod_wsgi (pid=60730): Target WSGI script '/home/amzitrage/app.amzitrage.com/pricearbitrase/wsgi.py' cannot be loaded as Python module.
  [wsgi:error] [pid 60730] mod_wsgi (pid=60730): Exception occurred processing WSGI script '/home/amzitrage/app.amzitrage.com/pricearbitrase/wsgi.py'.
  [wsgi:error] [pid 60730] Traceback (most recent call last):
  [wsgi:error] [pid 60730]   File "/home/amzitrage/app.amzitrage.com/pricearbitrase/wsgi.py", line 12, in &lt;module&gt;
  [wsgi:error] [pid 60730]     from django.core.wsgi import get_wsgi_application
  [wsgi:error] [pid 60730] ImportError: No module named django.core.wsgi
  </code></pre>
  
  <blockquote>
    <p>Edit 2</p>
  </blockquote>
  
  <p><strong>app/wsgi.py</strong>
  modified <code>wsgi.py</code> file to activate <code>virtual environment</code></p>
  
  <pre><code>activate_this = '/home/user/.local/share/virtualenvs/app.application.com-IuTkL8w_/bin/activate_this.py'
  exec(compile(open(activate_this,"rb").read(),activate_this, 'exec'), dict(__file__=activate_this))
  
  import os
  
  from django.core.wsgi import get_wsgi_application
  
  os.environ.setdefault("DJANGO_SETTINGS_MODULE", "application.settings")
  
  application = get_wsgi_application()
  </code></pre>
  
  <p>Also, <code>ls -l /path_to_pipenv_venv/bin</code> gives</p>
  
  <p><a href="https://i.stack.imgur.com/9bXoR.png" rel="noreferrer"><img src="https://i.stack.imgur.com/9bXoR.png" alt="enter image description here"></a></p>