简体   繁体   中英

Run Python program as administrator from command line

I'm trying to run my program as an administrator from the command line. Seems quite easy but I'm struggling.

I started with this, which works fine launched from the Python IDLE:

import dropbox, pymysql, shlex, shutil, subprocess
import logging
import sshtunnel
from sshtunnel import SSHTunnelForwarder

# Main program starts here
print("Hello World!")

but that gave an error as follows:

pi@jsmd:~ $ sudo python /home/pi/Documents/iot_pm2/iot.py
Traceback (most recent call last):
  File "/home/pi/Documents/iot_pm2/iot.py", line 3, in <module>
    import dropbox, pymysql, shlex, shutil, subprocess
ImportError: No module named pymysql

After some Googling I added two lines at the start:

import sys
sys.path.append("/usr/local/lib/python3.4/dist-packages/")

This also works fine from the Python IDLE but gives a long error trace when run from the command line... and I'm not good enough at understanding how to read the trace and hence what I can do to fix it. (Plus I don't understand why it works from the IDLE as a non-admin but not from the command line as an admin.) Can anyone help?

pi@jsmd:~ $ sudo python /home/pi/Documents/iot_pm2/iot.py
Traceback (most recent call last):
  File "/home/pi/Documents/iot_pm2/iot.py", line 5, in <module>
    import sshtunnel
  File "/usr/local/lib/python3.4/dist-packages/sshtunnel.py", line 25, in <module>
    import paramiko
  File "/usr/local/lib/python3.4/dist-packages/paramiko/__init__.py", line 30, in <module>
    from paramiko.transport import SecurityOptions, Transport
  File "/usr/local/lib/python3.4/dist-packages/paramiko/transport.py", line 50, in <module>
    from paramiko.dsskey import DSSKey
  File "/usr/local/lib/python3.4/dist-packages/paramiko/dsskey.py", line 26, in <module>
    from Crypto.PublicKey import DSA
  File "/usr/local/lib/python3.4/dist-packages/Crypto/PublicKey/DSA.py", line 89, in <module>
    from Crypto import Random
  File "/usr/local/lib/python3.4/dist-packages/Crypto/Random/__init__.py", line 29, in <module>
    from Crypto.Random import _UserFriendlyRNG
  File "/usr/local/lib/python3.4/dist-packages/Crypto/Random/_UserFriendlyRNG.py", line 38, in <module>
    from Crypto.Random.Fortuna import FortunaAccumulator
  File "/usr/local/lib/python3.4/dist-packages/Crypto/Random/Fortuna/FortunaAccumulator.py", line 39, in <module>
    from . import FortunaGenerator
  File "/usr/local/lib/python3.4/dist-packages/Crypto/Random/Fortuna/FortunaGenerator.py", line 35, in <module>
    from Crypto.Util import Counter
  File "/usr/local/lib/python3.4/dist-packages/Crypto/Util/Counter.py", line 59, in <module>
    from Crypto.Util import _counter
ImportError: cannot import name _counter

Thanks.

is the default of python is 3 ? If not try to execute like this: sudo python3 ... – @Koffee

Thanks to @Koffee we have an answer and this thread can be marked as solved and closed.

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