简体   繁体   中英

Python on RasPi can't find installed module

I feel like this has to have been asked and solved already, but I couldn't find a solution that works for me. I pip3'd a python library, and verify it is indeed on my system.

pi@raspberrypi:~/Desktop $ pip3 install pyftpdlib
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: pyftpdlib in /home/pi/.local/lib/python3.7/site-packages (1.5.6)

Then I try to import it but raspi cant find it...

pi@raspberrypi:~/Desktop $ sudo python3 FTPserver2.py
Traceback (most recent call last):
  File "FTPserver2.py", line 1, in <module>
    import pyftpdlib
ModuleNotFoundError: No module named 'pyftpdlib'

Huh?

When you run pip3 install without sudo , the package gets installed under /home/pi/.local/lib/python3.7/site-packages which is a user-specific location and packages installed there will not be accessible system-wide. Then you run sudo python3 which makes you execute python3 as the root user which is a different user.

Below I assume you do need to run the command with sudo . If you're not sure, try dropping the sudo - then the import should work (but maybe other stuff will not - I don't know what's in your script).

One method of installing a package for use by root would be to do sudo pip3 install pyftpdlib but this not recommended as it could break the Python installation used by the OS (some packages could have to be updated in order to be compatible with pyftpdlib , but they could then become incompatible with other stuff, and then you're in a lot of trouble).

It is better to use a virtual environment. For example:

# create the virtual environment
$ python3 -m venv env-ftp

# install the package into it
$ env-ftp/bin/python -m pip install pyftpdlib

# run a script using the Python installation contained within the virtual environment
$ sudo env-ftp/bin/python -m Desktop/FTPserver2.py

You could also choose to source env-ftp/bin/activate in order to temporarily switch to using python and pip specific to this environment until you deactivate .

Virtual environments are useful for creating isolated Python installations with their own separate sets of packages, which allows you eg to simultaneously use applications that have incompatible sets of dependencies (suppose 1 application requires requests==2.22.0 and another one requires requests<=2.21.0 and won't work with requests==2.22.0 ).

Can you try running this in the command line without errors?:

python3 -c "import pyftpdlib"

If this returns no error, that means your python interpreter is different. Make sure you are not running different python versions and/or have created different images and have not used sudo privileges to install packages.

As you can see, the pip3 has installed it in the site packages of /home/pi/.local/lib/python3.7/

Run this in command line

python3 -c "import site; print(site.getsitepackages())"

and check if it returns the same path as pip.

PS: It is always recommended to run pip3 install --user instead of sudo pip3 install .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM