1

I have a Adafruit PWM hat and I am trying to run play around with some servos. However, when I run this line:

from adafruit_servokit import ServoKit

It returns:

No module named 'adafruit_circuitpython_servokit'

I have already run:

pip3 install adafruit-circuitpython-servokit

After running the above command, it states that all requirements have been satisfied so it doesn't seem as though I am missing any module. Any ideas on what the issue could be?

  • Your program looks OK. I did the same and found no problem importing the servoKit thing. You might like to run my bare bone test program (debugged with sample output) to compare. https://penzu.com/p/46a4cc44 – tlfong01 Aug 04 '19 at 14:25
  • Do you have more than one version of Python installed? (This is common and frustrating.) In that case, pip may have installed the package into a different version than the one that you are using. – Brick Aug 04 '19 at 14:26
  • @Brick I have Python 2 and 3.7 installed. Since I used pip3 it should install to Python 3.7. – Richard Wessels Aug 04 '19 at 14:36
  • @tlfong01 Sadly the error occurs as soon as the import statement is run :( – Richard Wessels Aug 04 '19 at 14:37
  • @Brick I just "fixed" the problem. For some reason running python3 instead of sudo python3 worked. I installed the module without the sudo command. Does this mean that the module is not accessible when running the program as root? – Richard Wessels Aug 04 '19 at 14:42
  • @Richard Wessels, Did you mean all import statements in my test program fail, or just only the import servoKit one? I forgot to mention that I am running my test program using Throny. – tlfong01 Aug 04 '19 at 14:50
  • @tlfong01 The program raised on ModuleError on board. The program doesn't break when running the program without sudo. However, nothing happens with a servo I connected to channel 0. I assume you need to run the program in root for it to change. – Richard Wessels Aug 04 '19 at 14:58
  • @Richard Wessels, Thanks for the clarification. Actually I always run python programs using stretch IDLE. Now buster has not IDLE, so I switched to Geany and Throny. – tlfong01 Aug 04 '19 at 15:02
  • @tlfong01 Issue resolved, I installed the module without sudo which (for some reason) made it inaccessible when running root. Thanks for the help :) – Richard Wessels Aug 04 '19 at 15:04
  • This is then off-topic since the issue is a generic one about installing and using python modules and not specific to a Raspberry Pi context. – goldilocks Aug 04 '19 at 17:09
  • @goldilocks It can still be useful to people who may have installed the module for the ServoHat incorrectly. I always heard that installing a module as root was bad hence my mistake. – Richard Wessels Aug 04 '19 at 17:18
  • Sure, but you could reproduce this for every non-core python module (of which there must be tens of thousands) on the pretext that anyone using any of them needs it explained for that particular module, despite the fact the issue is exactly the same. If you are learning to program, you need to learn effective problem solving, and that is the opposite of it. In any case, it would still belong on our larger parent site, Stack Overflow. – goldilocks Aug 04 '19 at 19:17
  • @Richard Wessels, I am a GUI Windows guy and got a bit confused. Question 1: When you say "installing a module as root" do you mean using "sudo apt-get install, ...", or do you mean first log in as root, then install without "sudo"? Question 2: But the MagPi article instructs to use "sudo apt-get install", "sudo pip3 install", but later just "pip3 install". I followed the instructions blindly, then BIG CONFUSION, as you said, I cannot run my program using "sudo python3 myprog (got "import board" error!). I can only run by "python3 my prog". Indeed a big trap for newbies! / continue, ... – tlfong01 Aug 05 '19 at 08:39
  • But I did not notice the problem, because I am using Throny. The newbie friendly Throny seems to have taken care of everything, hiding the ugly traps from the newbies! :) – tlfong01 Aug 05 '19 at 08:42
  • @tlfong01 When I said installing a module as root, I meant that I ran sudo pip3. At first I installed the module as a regular user (no sudo). I heard that installing modules as root isn't the best idea, however, if you are having module errors when running the code as root, just reinstall the modules as root. – Richard Wessels Aug 05 '19 at 09:46
  • 1
    @Richard Wessels, Many thanks for clarifying my mind. – tlfong01 Aug 08 '19 at 03:20

1 Answers1

1

I installed the module as a normal user without sudo, so it was installed to that user's home directory and not the system. Running the script as root via sudo then meant that installation was not in the import path.

goldilocks
  • 58,859
  • 17
  • 112
  • 227
  • After reading servo hat tutorial, I know why mgpi84p44 suggests to sudo install python 3, to make sure even system default python 2, python 3 will install. However, Servokit should be installed in pi's /home/lib. Now of course you just use python3 myporg to run program, no sudo python3. (I only know very basic sudo, so only 80% sure.) – tlfong01 Aug 08 '19 at 03:19