4

I'm trying to get my cookiecutter-django app running under Apache with mod_wsgi installed via pip.

python3 manager.py runserver works.

But after running it in Apache, I got an error saying Module config not found. So I pip install config. (It is not installed on my development system.)

That put a config.py file in my /usr/local/pulseenv/lib/python3.5/site-packages.

That file had various syntax errors which I fixed. The errors were due to changes between python 2.7 and 3.5 as far as I can tell.

But now I get this error and I'm stuck where to go from here:

ImportError: No module named 'config.settings'; 'config' is not a package

I don't see any clues in the error.log on how to further trace the error. So what is this module? Why do I need it? And how to figure out my problem is?

EDIT:

Here's my vhost file (/etc/apache2/sites-available/000-default.conf):

<VirtualHost *:80>

        ServerAdmin webmaster@localhost
        #DocumentRoot /var/www/html
        DocumentRoot /var/www/pulsemanager/pulsemanager


            Alias /static /var/www/pulsemanager/pulsemanager/static
            <Directory /var/www/pulsemanager/pulsemanager/static>
                Require all granted
            </Directory>

            <Directory /var/www/pulsemanager/config>
                <Files wsgi.py>
                    Require all granted
                </Files>
            </Directory>

           WSGIScriptAlias / /var/www/pulsemanager/config/wsgi.py
           WSGIDaemonProcess pulsemanager
           WSGIProcessGroup pulsemanager
           WSGIApplicationGroup %{GLOBAL}

    </VirtualHost>

My project is layed out as:

ubuntu@ip-172-31-84-213:/var/www/pulsemanager$ ls
conf              docs         instantdudiobook.ipynb  package.json         pytest.ini  reqs.txt      setup.cfg
config            env.example  LICENSE                 pulsemanager         README.md   reqs.txt.sav  survey2.txt
CONTRIBUTORS.txt  gulpfile.js  manage.py               pulsenotebook.ipynb  README.rst  requirements  utility
ubuntu@ip-172-31-84-213:/var/www/pulsemanager$ cd pulsemanager/
ubuntu@ip-172-31-84-213:/var/www/pulsemanager/pulsemanager$ ls
contrib  locale  lsrc3  static  surveys  templates  users
ubuntu@ip-172-31-84-213:/var/www/pulsemanager/pulsemanager$

and here's my virutal environment:

ubuntu@ip-172-31-84-213:/usr/local/pulseenv$ ls
bin  include  lib  lib64  pip-selfcheck.json  pyvenv.cfg  share
ubuntu@ip-172-31-84-213:/usr/local/pulseenv$ cd lib
ubuntu@ip-172-31-84-213:/usr/local/pulseenv/lib$ pip3 list
Package                       Version
----------------------------- ----------------------
apturl                        0.5.2
argon2                        0.1.10
argon2-cffi                   18.1.0
arrow                         0.12.1
beautifulsoup4                4.4.1
binaryornot                   0.4.4
blinker                       1.3
Brlapi                        0.6.4
cairocffi                     0.8.0
certifi                       2018.1.18
cffi                          1.11.5
chardet                       3.0.4
checkbox-support              0.22
click                         6.7
config                        0.3.9
cloud-init                    18.2
command-not-found             0.3
configobj                     5.0.6
cookiecutter                  1.6.0
cryptography                  1.2.3
cssselect2                    0.2.1
cycler                        0.10.0
defer                         1.0.6
defusedxml                    0.5.0
Django                        2.0.3
django-admin-tools            0.8.1
django-allauth                0.35.0
django-autoslug               1.9.3
django-crispy-forms           1.7.2
django-debug-toolbar          1.9.1
django-environ                0.4.4
django-extensions             2.0.6
django-language-field         0.0.3
django-model-utils            3.1.1
feedparser                    5.1.3
future                        0.16.0
guacamole                     0.9.2
hibagent                      1.0.1
httplib2                      0.9.1
idna                          2.6
Jinja2                        2.8
jinja2-time                   0.2.0
jsonpatch                     1.10
jsonpointer                   1.9
language-selector             0.1
louis                         2.6.4
lxml                          3.5.0
Mako                          1.0.3
MarkupSafe                    0.23
oauthlib                      1.0.3
onboard                       1.2.0
padme                         1.1.1
pdfrw                         0.4
pexpect                       4.0.1
pip                           10.0.1
plainbox                      0.25
poyo                          0.4.1
prettytable                   0.7.2
ptyprocess                    0.5
pyasn1                        0.1.9
pycparser                     2.18
pycups                        1.9.73
pycurl                        7.43.0
pygobject                     3.20.0
PyJWT                         1.3.0
pyparsing                     2.0.3
Pyphen                        0.9.4
pyserial                      3.0.1
python-apt                    1.1.0b1+ubuntu0.16.4.1
python-dateutil               2.7.0
python-debian                 0.1.27
python-systemd                231
python3-openid                3.1.0
pytz                          2018.3
pyxdg                         0.25
PyYAML                        3.11
reportlab                     3.3.0
requests                      2.18.4
requests-oauthlib             0.8.0
sessioninstaller              0.0.0
setuptools                    39.1.0
six                           1.10.0
sqlparse                      0.2.4
ssh-import-id                 5.5
system-service                0.3
tinycss2                      0.6.1
ubuntu-drivers-common         0.0.0
ufw                           0.35
unattended-upgrades           0.1
unity-scope-calculator        0.1
unity-scope-chromiumbookmarks 0.1
unity-scope-colourlovers      0.1
unity-scope-devhelp           0.1
unity-scope-firefoxbookmarks  0.1
unity-scope-gdrive            0.7
unity-scope-manpages          0.1
unity-scope-openclipart       0.1
unity-scope-texdoc            0.1
unity-scope-tomboy            0.1
unity-scope-virtualbox        0.1
unity-scope-yelp              0.1
unity-scope-zotero            0.1
urllib3                       1.22
usb-creator                   0.3.0
webencodings                  0.5.1
wheel                         0.29.0
whichcraft                    0.4.1
xdiagnose                     3.8.4.1
xkit                          0.0.0
XlsxWriter                    0.7.3

EDIT 2:

I do have a config directory:

ubuntu@ip-172-31-84-213:~$ cd /var/www/pulsemanager/config/
ubuntu@ip-172-31-84-213:/var/www/pulsemanager/config$ ls
__init__.py  __pycache__  settings  urls.py  wsgi.py
ubuntu@ip-172-31-84-213:/var/www/pulsemanager/config$ cd settings
ubuntu@ip-172-31-84-213:/var/www/pulsemanager/config/settings$ ls
base.py  __init__.py  local.py  production.py  __pycache__  test.py
ubuntu@ip-172-31-84-213:/var/www/pulsemanager/config/settings$

And here's my wsgi.py. Seems like DJANGO_SETTINGS_MODULE should be defaulting to config.settings.production (/var/pulsemanager/config/settings/production.py):

"""
WSGI config for pulsemanager project.


"""
import os
import sys

from django.core.wsgi import get_wsgi_application

# This allows easy placement of apps within the interior
# pulsemanager directory.
app_path = os.path.dirname(os.path.abspath(__file__)).replace('/config', '')
sys.path.append(os.path.join(app_path, 'pulsemanager'))

if os.environ.get('DJANGO_SETTINGS_MODULE') == 'config.settings.production':
    from raven.contrib.django.raven_compat.middleware.wsgi import Sentry

# We defer to a DJANGO_SETTINGS_MODULE already in the environment. This breaks
# if running multiple sites in the same mod_wsgi process. To fix this, use
# mod_wsgi daemon mode with each site in its own daemon process, or use
# os.environ["DJANGO_SETTINGS_MODULE"] = "config.settings.production"
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.production")

# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
application = get_wsgi_application()
if os.environ.get('DJANGO_SETTINGS_MODULE') == 'config.settings.production':
    application = Sentry(application)
# Apply WSGI middleware here.
# from helloworld.wsgi import HelloWorldApplication
# application = HelloWorldApplication(application)

EDIT 3:

I removed the config package. I'm pretty sure that was a red herring. I see several references to 'config', e.g os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.production") and app_path = os.path.dirname(os.path.abspath(__file__)).replace('/config', '') in wsgi.py. Is one of these things where my problem is?

I added export DJANGO_SETTINGS_MODULE=config.settings.production to config/__init__.py and even tried putting os.environ["DJANGO_SETTINGS_MODULE"] = "config.settings.production" near the top of wsgi.py.

I updated WSGIDaemonProcess to:

WSGIDaemonProcess pulsemanager python-home=/usr/local/pulseenv/  python-path=/var/www/pulsemanager/pulsemanager:/usr/local/pulseenv/lib/python3.5/site-packages 

but I'm still getting:

ImportError: No module named 'config'

Here's the more of the error.log

    [Tue May 08 16:58:33.360155 2018] [mpm_event:notice] [pid 7420:tid 139690171062144] AH00489: Apache/2.4.18 (Ubuntu) OpenSSL/1.0.2g mod_wsgi/4.6.4 Python/3.5 configured -- resuming normal operations
    [Tue May 08 16:58:33.360248 2018] [core:notice] [pid 7420:tid 139690171062144] AH00094: Command line: '/usr/sbin/apache2'
    [Tue May 08 16:58:33.414142 2018] [wsgi:info] [pid 7423:tid 139690171062144] mod_wsgi (pid=7423): Attach interpreter ''.
    [Tue May 08 16:58:33.437248 2018] [wsgi:info] [pid 7423:tid 139690171062144] mod_wsgi (pid=7423): Adding '/var/www/pulsemanager/pulsemanager' to path.
    [Tue May 08 16:58:33.441845 2018] [wsgi:info] [pid 7423:tid 139690171062144] mod_wsgi (pid=7423): Adding '/usr/local/pulseenv/lib/python3.5/site-packages' to path.
    [Tue May 08 16:58:33.449915 2018] [wsgi:info] [pid 7423:tid 139690171062144] mod_wsgi (pid=7423): Imported 'mod_wsgi'.
    [Tue May 08 16:58:33.454029 2018] [wsgi:info] [pid 7423:tid 139690021156608] [remote 172.31.6.98:39831] mod_wsgi (pid=7423, process='pulsemanager', application=''): Loading Python script file '/var/www/pulsemanager/config/wsgi.py'.
    [Tue May 08 16:58:33.683108 2018] [wsgi:error] [pid 7423:tid 139690021156608] [remote 172.31.6.98:39831] mod_wsgi (pid=7423): Failed to exec Python script file '/var/www/pulsemanager/config/wsgi.py'.
    [Tue May 08 16:58:33.683161 2018] [wsgi:error] [pid 7423:tid 139690021156608] [remote 172.31.6.98:39831] mod_wsgi (pid=7423): Exception occurred processing WSGI script '/var/www/pulsemanager/config/wsgi.py'.
   ...

    [Tue May 08 16:59:13.003234 2018] [wsgi:error] [pid 7423:tid 139689928836864] [remote 172.31.93.15:41324] ImportError: No module named 'config'
Brad Rhoads
  • 1,828
  • 3
  • 29
  • 52
  • Can you put the structure of your project? instead of the same logs. – Frank AK May 07 '18 at 01:32
  • @FrankAK I added more details. Does that help or I am I still missing something? Thanks for the help. – Brad Rhoads May 07 '18 at 01:51
  • Seems you haven't create the `config` folder in your project! – Frank AK May 07 '18 at 02:24
  • Are you sure Apache is even starting with that configuration. The WSGIPythonHomen and WSGIPythonPath shouldn’t be allowed inside of VirtualHost. Use daemon mode of mod_wsgi instead and use python-home and python-path on WSGIDaemonProcess instead. See mod_wsgi docs on using virtual environment. – Graham Dumpleton May 07 '18 at 02:36
  • @GrahamDumpleton Sorry, I corrected that but posted an old version. – Brad Rhoads May 07 '18 at 03:25
  • @FrankAK Please see my EDIT 2. – Brad Rhoads May 07 '18 at 03:25
  • So as I said, use python-home on WSGIDaemonProcess to specify the location of any virtual environment and python-path to specify the location of your project code. – Graham Dumpleton May 07 '18 at 04:17
  • Per latest edit, not that it will make a difference, but you should not have ``/usr/local/pulseenv/lib/python3.5/site-packages`` in ``python-path``. See http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html – Graham Dumpleton May 10 '18 at 15:38

4 Answers4

2

Your DJANGO_SETTINGS_MODULE needs to specify a package or module that exists in the project root folder.

[pulsemanager]/
├── [config]/
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py

virtual environments

Using mod_wsgi Daemon Mode

WSGIDaemonProcess pulsemanager python-home=/path/to/venv/
WSGIProcessGroup pulsemanager

For multiple sites run in a single mod_wsgi process, all of them will use the settings of whichever one happens to run first. This can be solved by changing:

os.environ["DJANGO_SETTINGS_MODULE"] = "{{ project_name }}.settings.{{env}}"

Updated wsgi.py

import os
import sys

from django.core.wsgi import get_wsgi_application

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pulsemanager.settings.production")

if os.environ.get("DJANGO_SETTINGS_MODULE") == "pulsemanager.settings.production":
    from raven.contrib.django.raven_compat.middleware.wsgi import Sentry
    application = Sentry(get_wsgi_application())
else:
    get_wsgi_application()
jackotonye
  • 3,537
  • 23
  • 31
  • Please see my EDIT 2. – Brad Rhoads May 07 '18 at 03:23
  • Make sense you'll need an `__init__.py` in the settings package and export the `DJANGO_SETTINGS_MODULE=config.settings.production` – jackotonye May 07 '18 at 05:47
  • Might need to update the python path `WSGIDaemonProcess python-path=path/to/lib/python3.5/site-packages:/project/path` – jackotonye May 07 '18 at 05:51
  • You should not use ``python-path`` to add ``site-packages``, use ``python-home`` to set location of virtual environment. See: http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html – Graham Dumpleton May 07 '18 at 16:42
  • Thanks the `WSGIPythonHome` should be the directive to venv python packages. – jackotonye May 07 '18 at 17:11
  • May 07 20:01:50 ip-172-31-84-213 apache2[32731]: WSGIPythonHome cannot occur within section – Brad Rhoads May 07 '18 at 20:07
  • If the WSGI application is being setup within the context of an Apache VirtualHost, the WSGIPythonHome cannot be placed inside of the VirtualHost. Instead it must be placed outside of all VirtualHost definitions. This is because it applies to the whole Apache instance and not just the single VirtualHost. – jackotonye May 07 '18 at 21:46
  • Update to using the `WSGIDaemonProcess` http://modwsgi.readthedocs.io/en/develop/configuration-directives/WSGIDaemonProcess.html – jackotonye May 07 '18 at 21:53
  • @Graham Dumpleton, Thanks for the help guys. Please see EDIT 3. – Brad Rhoads May 08 '18 at 18:29
  • Looks like the project name is `pulsemanager` not `config` If you renamed the project to `config` check https://www.techinfected.net/2016/08/how-to-change-your-django-project-name.html to update the settings for the project. – jackotonye May 08 '18 at 18:50
  • I hope we're making some progress! I renamed config.settings.production to pulsemanager.settings.production and /config to /pulsemanger. But now i'm getting ImportError: No module named 'pulsemanager' :(. – Brad Rhoads May 08 '18 at 20:42
  • Can you checkout your `sys.path` for the settings package that exists I will suggest removing `app_path = os.path.dirname(os.path.abspath(__file__)).replace('/config', '') sys.path.append(os.path.join(app_path, 'pulsemanager'))` and updating the settings `ROOT_URLCONF = 'config.urls' WSGI_APPLICATION = 'config.wsgi.application'` – jackotonye May 09 '18 at 01:42
  • I edited your answer as I think you intended it to be (pulsemanager.settings.production). And I tried the suggestion. But I still get the same error. (I'm not sure if I'm breaking the rules or not, but if you want to make a bit of $ to get my server working, email bdrhoa @ gmail dot com. Or skype braddrhoads.) I really appreciate your efforts. – Brad Rhoads May 09 '18 at 03:53
  • @BradRhoads can you take a look at this repo https://bitbucket.org/jacktonye/tdd_django/src/default/ has a dummy project using apache – jackotonye May 09 '18 at 15:36
  • The example was very helpful. After all this, I sort of ended up where I started. But I learned a lot. It turns out that cookiecutter_django apps only work on nginx unless you're using AWS Elastic Beanstalk. – Brad Rhoads May 11 '18 at 03:50
0

I was having the same problem when importing from a local folder without an init file as that would make it a cyclic import.

Try echo $PYTHONPATH, resetting this variable to blank worked for me.

Simas Joneliunas
  • 2,890
  • 20
  • 28
  • 35
Avinash
  • 1
  • 2
0

Run django from linux terminal, not vs code terminal

  • I had this crazy experience only when using vs code terminal
  • activate your virtual env and run from the default linux terminal
  • (app_venv)machinery@001:~/Desktop/yourprj$ ./manage.py runserver
7guyo
  • 3,047
  • 1
  • 30
  • 31
-1

I once had a similar issue to the above:

$ py -3 bloody.py
Traceback (most recent call last):
  File "bloody.py", line 1, in <module>
    import requests
ModuleNotFoundError: No module named 'requests'

This occurred even though I had followed the instructions on the Cisco Devnet website, which suggested:

pip install requests

What I ended up running was:

py -2 -m pip install requests

py -3 -m pip install requests

This installed requests for Python 3 as well as Python 2.

interfect
  • 2,665
  • 1
  • 20
  • 35