当我想在当前用户下将模块导入Python时,导入工作正常。但是当我想以root用户身份或突然导入同一模块时,找不到该模块。
这是证明:
root@raspberrypi:/home/pi/Programs# whoami
root
root@raspberrypi:/home/pi/Programs# python3
Python 3.7.3 (default, Apr 3 2019, 05:39:12)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import paho.mqtt.client as paho
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'paho'
>>>
root@raspberrypi:/home/pi/Programs# su pi
pi@raspberrypi:~/Programs $ python3
Python 3.7.3 (default, Apr 3 2019, 05:39:12)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import paho.mqtt.client as paho
>>>
pi@raspberrypi:~/Programs $
我已经尝试通过将.bashrc
添加到模块来将PYTHONPATH
修改为root用户,但是没有成功。我想念什么?
这是作为普通用户输出的:
pi@raspberrypi:~/Programs $ whoami
pi
pi@raspberrypi:~/Programs $ python3 -c 'import sys; print(sys.path)'
['', '/home/pi/Programs', '/usr/local/lib/python3.7/dist-packages/Adafruit_PCA9685', '/usr/local/lib/python3.7/dist-packages/Adafruit_GPIO', '/home/pi/.local/lib/python3.7/site-packages/paho', '/home/pi/Programs/usr/local/lib/python3.7/site-packages/paho', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload', '/home/pi/.local/lib/python3.7/site-packages', '/usr/local/lib/python3.7/dist-packages', '/usr/local/lib/python3.7/dist-packages/rpi_ws281x-1.0.0-py3.7-linux-armv7l.egg', '/usr/lib/python3/dist-packages']
这是作为根的输出:
pi@raspberrypi:~/Programs $ su
root@raspberrypi:/home/pi/Programs# python3 -c 'import sys; print(sys.path)'
['', '/home/pi/Programs', '/usr/local/lib/python3.7/dist-packages/Adafruit_PCA9685', '/usr/local/lib/python3.7/dist-packages/Adafruit_GPIO', '/home/pi/.local/lib/python3.7/site-packages/paho', '/home/pi/Programs/usr/local/lib/python3.7/site-packages/paho', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload', '/usr/local/lib/python3.7/dist-packages', '/usr/local/lib/python3.7/dist-packages/rpi_ws281x-1.0.0-py3.7-linux-armv7l.egg', '/usr/lib/python3/dist-packages']
在两个版本中,到paho的路径似乎都是相同的!???