Importing envirophat modules in Django app [SOLVED]

Hello,

First time poster, new Pimoroni customer, decades of development experience but new to Python / Django / Apache / WSGI:

I was able to install the envirophat Python modules (via curl) and can run the temperature demo code successfully. When I try to run the same code from a Django project via Apache / WSGI I get a “No module named ‘envirophat’” error at the “from envirophat import weather” statement. I think my noobishness is showing - I am pretty sure I need to add the path to the envirophat package somewhere but so far the places I have tried are not working. It might be a permissions issue but I would have expected an error for that. Any help would be appreciated, thanks in advance!

What do you see if you slap up a quick script to:

import sys
print(sys.version)
print(sys.path)

Hello - apologies for the delay, I had to set this aside until now - here are the results:

Running it from command line:

    3.4.2 (default, Oct 19 2014, 13:31:11)
    [GCC 4.9.1]
    ['/home/pi/dev', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-arm-linux-gnueabihf', '/usr/lib/python3.4/lib-dynload', '/usr/local/lib/python3.4/dist-packages', '/usr/lib/python3/dist-packages', '/usr/lib/python3.4/dist-packages']

Running it from the Django app under Apache:

3.4.2 (default, Oct 19 2014, 14:03:53) [GCC 4.9.1]
['/home/pi/wwwroot', '/usr/lib/python3/dist-packages/envirophat', '/home/pi/wwwroot/wwwrootenv/lib/python3.4', '/home/pi/wwwroot/wwwrootenv/lib/python3.4/plat-arm-linux-gnueabihf', '/home/pi/wwwroot/wwwrootenv/lib/python3.4/lib-dynload', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-arm-linux-gnueabihf', '/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages']

What’s interesting is that I see the envirophat directory in the Django app but it doesn’t work, and I don’t see it in the command line version but it runs there. I don’t know if the problem is that envirophat is supposed to be under the dist-packages folders or something like that, but I am looking forward to learning something new.

1 Like

Looks like the Django app is running Python within a virtualenv with potentially no access to external libraries.

You should fire up Terminal and then run:

source /home/pi/wwwroot/wwwrootenv/bin/activate

Then you should be able to see installed packages by running: pip freeze

And install Enviro pHAT with pip install envirophat

Then exit the Virtual Environment by running: deactivate.

Possibly useful for ongoing tinkering: http://python-guide-pt-br.readthedocs.io/en/latest/dev/virtualenvs/

Hello gadgetoid,

Thanks for the reply - that helped and I’m getting closer, but now I am getting 500 errors in the web page. In the Apache logs it shows a whole lot of traceback entries starting with this one:

"ImportError: No module named 'smbus'" in /home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/envirophat/i2c_bus.py.

At the end of the traceback is the following, all listed as WSGI errors:

exit("This library requires python3-smbus\\nInstall with: sudo apt-get install python3-smbus")
SystemExit: This library requires python3-smbus
Install with: sudo apt-get install python3-smbus

When I run the apt-get command it says it’s already installed. When I try installing it with pip in a virtualenv it returns:

Could not find a version that satisfies the requirement python3-smbus (from versions: )
No matching distribution found for python3-smbus

I also tried installing just smbus but no luck. I’m not even sure it’s a Python module at this point.

I’m a bit out of my depth here so I am going to re-read through the virtualenv link you sent me and keep digging. I can’t attach the complete logs for the transaction as a text file so I added it below (apologies for the length). Thanks again!

[Tue Jul 18 13:02:25.496512 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814] mod_wsgi (pid=4853): SystemExit exception raised by WSGI script '/home/pi/wwwroot/wwwroot/wsgi.py' ignored.
[Tue Jul 18 13:02:25.498909 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814] Traceback (most recent call last):
[Tue Jul 18 13:02:25.501780 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/envirophat/i2c_bus.py", line 4, in <module>
[Tue Jul 18 13:02:25.503890 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]     import smbus
[Tue Jul 18 13:02:25.505740 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814] ImportError: No module named 'smbus'
[Tue Jul 18 13:02:25.508313 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]
[Tue Jul 18 13:02:25.509691 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814] During handling of the above exception, another exception occurred:
[Tue Jul 18 13:02:25.511776 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]
[Tue Jul 18 13:02:25.513411 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814] Traceback (most recent call last):
[Tue Jul 18 13:02:25.516411 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/core/handlers/wsgi.py", line 157, in __call__
[Tue Jul 18 13:02:25.518030 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]     response = self.get_response(request)
[Tue Jul 18 13:02:25.520966 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/core/handlers/base.py", line 124, in get_response
[Tue Jul 18 13:02:25.522438 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]     response = self._middleware_chain(request)
[Tue Jul 18 13:02:25.525566 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/core/handlers/exception.py", line 41, in inner
[Tue Jul 18 13:02:25.527032 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]     response = get_response(request)
[Tue Jul 18 13:02:25.529824 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/utils/deprecation.py", line 140, in __call__
[Tue Jul 18 13:02:25.530962 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]     response = self.get_response(request)
[Tue Jul 18 13:02:25.533075 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/core/handlers/exception.py", line 41, in inner
[Tue Jul 18 13:02:25.534161 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]     response = get_response(request)
[Tue Jul 18 13:02:25.536311 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/utils/deprecation.py", line 140, in __call__
[Tue Jul 18 13:02:25.537386 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]     response = self.get_response(request)
[Tue Jul 18 13:02:25.539691 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/core/handlers/exception.py", line 41, in inner
[Tue Jul 18 13:02:25.540774 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]     response = get_response(request)
[Tue Jul 18 13:02:25.542905 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/utils/deprecation.py", line 140, in __call__
[Tue Jul 18 13:02:25.543984 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]     response = self.get_response(request)
[Tue Jul 18 13:02:25.546125 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/core/handlers/exception.py", line 41, in inner
[Tue Jul 18 13:02:25.547277 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]     response = get_response(request)
[Tue Jul 18 13:02:25.549500 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/utils/deprecation.py", line 140, in __call__
[Tue Jul 18 13:02:25.550587 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]     response = self.get_response(request)
[Tue Jul 18 13:02:25.552717 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/core/handlers/exception.py", line 41, in inner
[Tue Jul 18 13:02:25.553798 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]     response = get_response(request)
[Tue Jul 18 13:02:25.555928 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/utils/deprecation.py", line 140, in __call__
[Tue Jul 18 13:02:25.557004 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]     response = self.get_response(request)
[Tue Jul 18 13:02:25.559223 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/core/handlers/exception.py", line 41, in inner
[Tue Jul 18 13:02:25.560319 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]     response = get_response(request)
[Tue Jul 18 13:02:25.562470 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/utils/deprecation.py", line 140, in __call__
[Tue Jul 18 13:02:25.563542 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]     response = self.get_response(request)
[Tue Jul 18 13:02:25.565692 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/core/handlers/exception.py", line 41, in inner
[Tue Jul 18 13:02:25.566778 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]     response = get_response(request)
[Tue Jul 18 13:02:25.569000 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/utils/deprecation.py", line 140, in __call__
[Tue Jul 18 13:02:25.570084 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]     response = self.get_response(request)
[Tue Jul 18 13:02:25.572218 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/core/handlers/exception.py", line 41, in inner
[Tue Jul 18 13:02:25.573307 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]     response = get_response(request)
[Tue Jul 18 13:02:25.575460 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/core/handlers/base.py", line 172, in _get_response
[Tue Jul 18 13:02:25.576578 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]     resolver_match = resolver.resolve(request.path_info)
[Tue Jul 18 13:02:25.578252 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/urls/resolvers.py", line 362, in resolve
[Tue Jul 18 13:02:25.579984 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]     for pattern in self.url_patterns:
[Tue Jul 18 13:02:25.582133 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/utils/functional.py", line 35, in __get__
[Tue Jul 18 13:02:25.583297 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]     res = instance.__dict__[self.name] = self.func(instance)
[Tue Jul 18 13:02:25.585564 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/urls/resolvers.py", line 405, in url_patterns
[Tue Jul 18 13:02:25.586688 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]     patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
[Tue Jul 18 13:02:25.590507 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/utils/functional.py", line 35, in __get__
[Tue Jul 18 13:02:25.591543 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]     res = instance.__dict__[self.name] = self.func(instance)
[Tue Jul 18 13:02:25.593731 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/urls/resolvers.py", line 398, in urlconf_module
[Tue Jul 18 13:02:25.594806 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]     return import_module(self.urlconf_name)
[Tue Jul 18 13:02:25.595749 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/importlib/__init__.py", line 109, in import_module
[Tue Jul 18 13:02:25.597537 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]     return _bootstrap._gcd_import(name[level:], package, level)
[Tue Jul 18 13:02:25.599090 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
[Tue Jul 18 13:02:25.601191 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
[Tue Jul 18 13:02:25.602662 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
[Tue Jul 18 13:02:25.604707 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
[Tue Jul 18 13:02:25.606161 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "<frozen importlib._bootstrap>", line 1129, in _exec
[Tue Jul 18 13:02:25.608327 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "<frozen importlib._bootstrap>", line 1471, in exec_module
[Tue Jul 18 13:02:25.609829 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
[Tue Jul 18 13:02:25.612120 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwroot/urls.py", line 3, in <module>
[Tue Jul 18 13:02:25.613237 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]     from wwwroot.views import WwwRootView
[Tue Jul 18 13:02:25.615387 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwroot/views.py", line 8, in <module>
[Tue Jul 18 13:02:25.616465 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]     from envirophat import weather
[Tue Jul 18 13:02:25.618665 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/envirophat/__init__.py", line 1, in <module>
[Tue Jul 18 13:02:25.619809 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]     from .i2c_bus import bus
[Tue Jul 18 13:02:25.621954 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/envirophat/i2c_bus.py", line 9, in <module>
[Tue Jul 18 13:02:25.623065 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814]     exit("This library requires python3-smbus\\nInstall with: sudo apt-get install python3-smbus")
[Tue Jul 18 13:02:25.625224 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814] SystemExit: This library requires python3-smbus
[Tue Jul 18 13:02:25.626349 2017] [wsgi:error] [pid 4853:tid 3037561904] [remote 192.168.1.107:37814] Install with: sudo apt-get install python3-smbus
[Tue Jul 18 13:02:25.693897 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814] mod_wsgi (pid=4853): SystemExit exception raised by WSGI script '/home/pi/wwwroot/wwwroot/wsgi.py' ignored.
[Tue Jul 18 13:02:25.695541 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814] Traceback (most recent call last):
[Tue Jul 18 13:02:25.697300 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/envirophat/i2c_bus.py", line 4, in <module>
[Tue Jul 18 13:02:25.698462 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]     import smbus
[Tue Jul 18 13:02:25.700375 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814] ImportError: No module named 'smbus'
[Tue Jul 18 13:02:25.702294 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]
[Tue Jul 18 13:02:25.703337 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814] During handling of the above exception, another exception occurred:
[Tue Jul 18 13:02:25.704917 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]
[Tue Jul 18 13:02:25.706261 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814] Traceback (most recent call last):
[Tue Jul 18 13:02:25.708597 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/core/handlers/wsgi.py", line 157, in __call__
[Tue Jul 18 13:02:25.709715 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]     response = self.get_response(request)
[Tue Jul 18 13:02:25.711906 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/core/handlers/base.py", line 124, in get_response
[Tue Jul 18 13:02:25.713048 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]     response = self._middleware_chain(request)
[Tue Jul 18 13:02:25.715295 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/core/handlers/exception.py", line 41, in inner
[Tue Jul 18 13:02:25.716394 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]     response = get_response(request)
[Tue Jul 18 13:02:25.718656 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/utils/deprecation.py", line 140, in __call__
[Tue Jul 18 13:02:25.719778 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]     response = self.get_response(request)
[Tue Jul 18 13:02:25.721932 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/core/handlers/exception.py", line 41, in inner
[Tue Jul 18 13:02:25.723024 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]     response = get_response(request)
[Tue Jul 18 13:02:25.725303 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/utils/deprecation.py", line 140, in __call__
[Tue Jul 18 13:02:25.726413 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]     response = self.get_response(request)
[Tue Jul 18 13:02:25.728672 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/core/handlers/exception.py", line 41, in inner
[Tue Jul 18 13:02:25.729770 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]     response = get_response(request)
[Tue Jul 18 13:02:25.731998 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/utils/deprecation.py", line 138, in __call__
[Tue Jul 18 13:02:25.733099 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]     response = self.process_request(request)
[Tue Jul 18 13:02:25.735454 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/middleware/common.py", line 62, in process_request
[Tue Jul 18 13:02:25.736593 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]     if self.should_redirect_with_slash(request):
[Tue Jul 18 13:02:25.738882 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/middleware/common.py", line 80, in should_redirect_with_slash
[Tue Jul 18 13:02:25.740066 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]     not is_valid_path(request.path_info, urlconf) and
[Tue Jul 18 13:02:25.742331 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/urls/base.py", line 158, in is_valid_path
[Tue Jul 18 13:02:25.743472 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]     resolve(path, urlconf)
[Tue Jul 18 13:02:25.745693 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/urls/base.py", line 27, in resolve
[Tue Jul 18 13:02:25.746971 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]     return get_resolver(urlconf).resolve(path)
[Tue Jul 18 13:02:25.749288 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/urls/resolvers.py", line 362, in resolve
[Tue Jul 18 13:02:25.750411 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]     for pattern in self.url_patterns:
[Tue Jul 18 13:02:25.752537 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/utils/functional.py", line 35, in __get__
[Tue Jul 18 13:02:25.753664 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]     res = instance.__dict__[self.name] = self.func(instance)
[Tue Jul 18 13:02:25.755785 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/urls/resolvers.py", line 405, in url_patterns
[Tue Jul 18 13:02:25.756929 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]     patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
[Tue Jul 18 13:02:25.759175 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/utils/functional.py", line 35, in __get__
[Tue Jul 18 13:02:25.760301 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]     res = instance.__dict__[self.name] = self.func(instance)
[Tue Jul 18 13:02:25.762059 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/urls/resolvers.py", line 398, in urlconf_module
[Tue Jul 18 13:02:25.763449 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]     return import_module(self.urlconf_name)
[Tue Jul 18 13:02:25.765625 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/importlib/__init__.py", line 109, in import_module
[Tue Jul 18 13:02:25.766790 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]     return _bootstrap._gcd_import(name[level:], package, level)
[Tue Jul 18 13:02:25.768867 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]   File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
[Tue Jul 18 13:02:25.770316 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]   File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
[Tue Jul 18 13:02:25.772273 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]   File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
[Tue Jul 18 13:02:25.773766 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]   File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
[Tue Jul 18 13:02:25.775793 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]   File "<frozen importlib._bootstrap>", line 1129, in _exec
[Tue Jul 18 13:02:25.777261 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]   File "<frozen importlib._bootstrap>", line 1471, in exec_module
[Tue Jul 18 13:02:25.779370 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]   File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
[Tue Jul 18 13:02:25.781068 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwroot/urls.py", line 3, in <module>
[Tue Jul 18 13:02:25.782180 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]     from wwwroot.views import WwwRootView
[Tue Jul 18 13:02:25.784988 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwroot/views.py", line 8, in <module>
[Tue Jul 18 13:02:25.786063 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]     from envirophat import weather
[Tue Jul 18 13:02:25.788371 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/envirophat/__init__.py", line 1, in <module>
[Tue Jul 18 13:02:25.789484 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]     from .i2c_bus import bus
[Tue Jul 18 13:02:25.791115 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]   File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/envirophat/i2c_bus.py", line 9, in <module>
[Tue Jul 18 13:02:25.792220 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814]     exit("This library requires python3-smbus\\nInstall with: sudo apt-get install python3-smbus")
[Tue Jul 18 13:02:25.794655 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814] SystemExit: This library requires python3-smbus
[Tue Jul 18 13:02:25.795744 2017] [wsgi:error] [pid 4853:tid 2920121392] [remote 192.168.1.107:37814] Install with: sudo apt-get install python3-smbus

Yeah I should have figured smbus would be a pain! grumble

You will need to configure your virtualenv to inherit site packages from your system, rather than being completely self-contained. I don’t use virtualenv enough to know exactly how to accomplish this, but this answer seems about right:

Try removing (or renaming) the file no-global-site-packages.txt in your Lib folder under your virtual environment.

So what happens if you:

ls /home/pi/wwwroot/wwwrootenv/lib/

And then remove the no-global-site-packages.txt from each Python version found in that lib directory.

Okay awesome, that worked, Apache appears to be rendering the page - now I’m getting a Permission Denied error, here is the Django debug, I am thinking I am one or two chmods away from this running:

Environment:

Request Method: GET
Request URL: http://192.168.1.139/

Django Version: 1.11.3
Python Version: 3.4.2
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'wwwroot']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/core/handlers/base.py" in _get_response
  172.         resolver_match = resolver.resolve(request.path_info)

File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/urls/resolvers.py" in resolve
  362.             for pattern in self.url_patterns:

File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/utils/functional.py" in __get__
  35.         res = instance.__dict__[self.name] = self.func(instance)

File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/urls/resolvers.py" in url_patterns
  405.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)

File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/utils/functional.py" in __get__
  35.         res = instance.__dict__[self.name] = self.func(instance)

File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/django/urls/resolvers.py" in urlconf_module
  398.             return import_module(self.urlconf_name)

File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/importlib/__init__.py" in import_module
  109.     return _bootstrap._gcd_import(name[level:], package, level)

File "/home/pi/wwwroot/wwwroot/urls.py" in <module>
  3. from wwwroot.views import WwwRootView

File "/home/pi/wwwroot/wwwroot/views.py" in <module>
  8. from envirophat import weather

File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/envirophat/__init__.py" in <module>
  1. from .i2c_bus import bus

File "/home/pi/wwwroot/wwwrootenv/lib/python3.4/site-packages/envirophat/i2c_bus.py" in <module>
  20.     bus = smbus.SMBus(1)

Exception Type: PermissionError at /
Exception Value: [Errno 13] Permission denied

Okay I think I may have figured it out: I added the www-data account (which is running Apache) to the i2c and gpio groups, and it appears to be working. See https://raspberrypi.stackexchange.com/questions/39139/webserver-gpio-python-script-no-access-to-dev-mem-try-running-as-root for a sideways reference.

Thanks for the help, I’d still be banging my head against my display if you hadn’t gotten me this far :)

1 Like

Brilliant! Glad you’re up and running.