AutomationPhat - Cannot Import AutomationHat library in Python

UPDATE (10/16/5019):
I was able to get python to see the AutomationHat Library by setting the virtualenv to use external package (virtualenv --system-site-packages venv),

I have a new error saying I either dont have permission or can’t find SMBUS (“This library requires python-smbus\nInstall with: sudo apt install python-smbus”)

It’s got to be a permissions thing, because if I run it using Sudo, I can actually run it. The problem is I don’t know how to run uWSGI as sudo and I’m not sure I want it to require root priviledges to run… Thoughts?

Hey all,
I have an odd one here.
I think it’s one of two possibilities, but I’m not sure what the fix might be.

Background:

  • I have a RaspberryPiZero W
  • I have the AuthomationPhat attached
  • I developed an application that works 100% fine under the Pi user, and no VirtualEnv.
  • I used the curl command, from the installation instruction, to install the library.
  • Here, I CAN load the library in both python command line, and in a .py file, without any issue.
  • The app is then hosted in Flask. It recognizes hardware IO changes just fine, and displays it to the website.

Problem:

  • Now that I have the prototype ready, I am setting up the Pi for a “Production” scenario.
  • I created a new User on the same Pi, with tighter privileges restrictions (Firewall, Read/Write, etc.) , but still has Sudo privileges for now while I set it up.
  • I created a new Python Virtual Environment to containerize the project. (this part was new to me, I assume I did it correctly)

Here is the error I am getting:
With the new user and new virtualenv –
When trying to Import the AuomationHat library in the exact same .py file I prototyped, it says it can’t find it.
When trying it from the Python command line, it says it can’t find it.
Within the VirtualEnv. I have tried both curl install, and the pip-install versions without much luck.
Pip tells me it is already installed.

When checking the virtualenv/bin folder, I do NOT see the automationHat library (I do see Flask and uWSGI, so I know the virtualenv is working correctly "In Theory"™ ).
When checking the ~/Pimoroni/AutomationHat directory for the new user, I DO see the library there from the curl install… but it still can’t see it in Python.

My best guess is that it’s either

  1. The VirtualEnv, is causing issues (this is my gut feeling)
  2. The second user is causing issues (maybe because it sees it already installed for another user…).
    But between the two of them, I’m not sure how the two AutomationHat Installation methods interacts with either of them…

Has anyone run into something similar? How did you fix it? Since I CAN see the library in the ~/pimoroni/automationhat directory… I’m sure this is not the preferred method, but is there a way to just point the virtualenv outside itself, to just use the dang thing, and call it a day?? I’d rather it be within the virtualenv, to “Do it right”… but failing that, I just want it to work. :P

Hey All,
So I think I finally figured it out. Took me quite a while of trial and error, and googling, but here are the two things I came up with… It ended up being a bit of both the virtual env, and being a new user.

  1. The first problem I ran into is that the installation using the curl command put all the libraries in the global packages. That fine, but the virtual environment then needs to be configured to allow global packages. After that it should find the AutomationHat and it should find the smBus libraries.
  • To do this, you have to remove the file “no-global-site-packages.txt” from your virtual environment
  • rm ~/<projectName>/<virtualEnvName>/lib/python<version>/no-global-site-packages.txt
  1. The second problem I ran into was that it didn’t allow my new user to use the smBUS. Not 100% sure what that is. From what I discovered the default Pi user has a bunch of extra permissions that a “New User” doesn’t get by default. Once I added my new user to all the groups Pi was a part of, it started working again.
  • For example, Pi is part of these groups: sudo plugdev users input netdev gpio i2c spi pi

To add a new user to a group:

  • usermod -g <comma seperated list of groups, with no spaces. ie. i2c,spi,pi,netdev> <userName>

Hope this helps anyone trying to use the the automationhat with either a New User, or a Virtual Environment. Good luck and happy programming!