从命令行以管理员身份运行Python程序

时间:2016-07-04 10:25:55

标签: python python-3.x

我试图从命令行以管理员身份运行我的程序。似乎很容易,但我很挣扎。

我从这开始,从Python IDLE开始工作正常:

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

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

但是出现了如下错误:

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

在一些谷歌搜索后,我在开始时添加了两行:

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

这也可以从Python IDLE中正常工作,但是从命令行运行时会产生很长的错误跟踪...而且我不太了解如何读取跟踪,因此我可以做些什么来修复它。 (另外我不明白为什么它作为非管理员从IDLE起作用,而不是作为管理员从命令行起作用。)任何人都可以帮忙吗?

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

感谢。

1 个答案:

答案 0 :(得分:0)

  

是python的默认值是3吗?如果没有尝试像这样执行:sudo   python3 ... - @Koffee

感谢@Koffee我们有一个答案,这个帖子可以标记为已解决并已关闭。