Reputation: 21
I was given a raspberry pifor my birthday and decided to make an alarm clock out of one. I wrote all the code on my PC, works completely fine and expected but I'm having slight issues with installing packages on the raspberry pi.
When I open the terminal, I'm in the directory "home/pi".
I then run the command
sudo easy_install -U schedule
which installs fine, I then try to run my code which is stored in "home/pi", but get an error on:
Traceback (most recent call last):
File "/home/pi/LED.py", line 1, in <module>
import schedule
ImportError: No module named 'schedule'
any tips? I've also installed schedule via pip in the same directory - pip install schedule which installs perfectly fine.
#!/usr/bin/python
import schedule
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.output(18, GPIO.HIGH)
time.sleep(5)
GPIO.output(18, GPIO.LOW)
GPIO.cleanup()
Upvotes: 2
Views: 21077
Reputation: 5
i came across this when searching for an answer. and then i was just testing something that got me thinking about where the packages. i have had probles using "sudo python something.py". getting the "ImportError: No module named "some module"
i have always been using "pip install" or "pip3 install", and always could have used it as my user, but been as i wrote, problem when i "sudo python something.py"
what fixed it in my case, is install the module again with "sudo pip install" or sudo pip3 install". though then the modules would be installed 2 places i guess. though i havent tested how it would be reachable for a normal user yet...
Upvotes: 0
Reputation: 11
just ran into the same problem and on my raspberry 4 it helped when I used the following from the command line where the python script was launched: sudo python3 example.py Note the "3" at the end of "python" above.
Upvotes: 1
Reputation: 15423
Python searches the packages in all directories in the python path
For instance, these directories for me are :
>>> import sys
>>> sys.path
['', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/usr/lib/python3.6/site-packages']
Note that the first path is "the directory containing the script that was used to invoke the Python interpreter", often the current directory when you run your python script.
Also note that pip should install the packages in the site-packages
directory. (The last path in my sys.path
in my previous example). There should be one of these directory per python installation.
A simple command line like find / -name site-packages
should be enough to find them all. But keep in mind that not all python interpreter will use the same sys.path
: obviously, if you install some package for python2, you won't be able to access it from a python3 interpreter. Same thing if you use virtualenvs.
Upvotes: 4