Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
688 views
in Technique[技术] by (71.8m points)

pip - Cannot run Python script using sudo

I have a simple script which is using signalr-client-py as an external module.

from requests import Session
from signalr import Connection
import threading

When I try to run my script using the sudo python myScriptName.py I get an error:

Traceback (most recent call last):
  File "buttonEventDetectSample.py", line 3, in <module>
    from signalrManager import *
  File "/home/pi/Desktop/GitRepo/DiatAssign/Main/signalrManager.py", line 2, in <module>
    from signalr import Connection
ImportError: No module named signalr

If I run my script typing only python myScriptName.py it works perfectly fine but I need to have the sudo in front because later on in my other scripts (that use this one) I perform write operation on the File system.

I am quite new to Python and that's why I need to know how I can handle this situation. If I type pydoc modules I get a list which contains:

signalr
signalrManager

If I type pip freeze I can see there listed:

signalr-client==0.0.7
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

By default sudo runs commands in different environment. You can ask sudo to preserve environment with -E switch.

sudo -E python myScriptName.py

It comes with it's own security risks. So be careful


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...