1

我正在尝试在 Ubuntu 12.04 中使用单个参数从命令行运行 python 文件。如果我只是从 IDE 运行它并在代码中传递参数,该程序就可以工作。但是,如果我在命令提示符下调用“python readFromSerial1.py 3”,我会得到:

Traceback (most recent call last):
  File "readFromSerial1.py", line 62, in <module>
    main()   
  File "readFromSerial1.py", line 6, in main
    readDataFromUSB(time)
  File "readFromSerial1.py", line 9, in readDataFromUSB
    import usb.core
ImportError: No module named usb.core

如果我从 IDE 运行,模块导入正确,我有点困惑。我下载了 pyUSB 模块并将其解压缩(它的文件名是 pyusb-1.0.0a3)。然后我将此文件复制到 /usr/local/lib/python2.7/site-packages/ 中。这是正确的程序吗?我感觉问题是由于 python 根本无法找到 USB 模块,我需要将它放在正确的位置。我的代码如下,任何帮助将不胜感激:

readFromSerial1.py

import sys

def main():
    time = sys.argv[1]
    #time = 1
    readDataFromUSB(time)

def readDataFromUSB(time):
    import usb.core


    #find device
    dev = usb.core.find(idVendor=0x099e, idProduct=0x0001) #GPS info

    #Was the device found?
    if dev is None:
        raise ValueError('Device not found')
    else:
        print "Device found!"

    #Do this to avoid 'Errno16: Resource is busy'
    if dev.is_kernel_driver_active(0):
        try:
            dev.detach_kernel_driver(0)
        except usb.core.USBError as e:
            sys.exit("Could not detach kernel driver: %s" % str(e))

    #Sets default config
    dev.set_configuration()

    #Gets default endpoint
    endpoint = dev[0][(0,0)][0]



    writeObject = open("InputData.txt", "w")

    #iterate for time purposes
    for i in range(0, (time*6)):    #sys.argv is command line variable for time input
        data = dev.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize, 0, 100000)
        sret = ''.join([chr(x) for x in data])

        writeObject.write(sret);

        print sret
        '''
        newStr = ''.join(sret[7:14])

        compareStr = ",*4F"

        if (newStr == compareStr):
            print "The GPS is not reading in any values right now. Try going somewhere else with better reception."

        else:
            print sret[7:14]

        '''

    writeObject.close()


main()   
4

0 回答 0