Python代码在交互式提示中运行,但不作为脚本运行

时间:2017-02-15 22:07:19

标签: python libusb-win32

我正在尝试使用海风(seabreeze)和pyseabreeze(pyseabreeze)与海洋光学光谱仪接口。

如果我从Windows中的命令提示符(cmd)输入python并逐行输入以下代码,则可以正常工作。但是,如果我将它放在脚本(spec_test.py)中并尝试使用'python spec_test.py'从cmd.exe运行它,它将无法正常工作。

我经常搜索类似的问题,但似乎没有人能解决我遇到的问题。我正在运行Windows 7 64位,使用anaconda安装python 3.5.2。我还必须安装pyusb和libusb才能使用pyseabreeze。

代码:

import seabreeze
seabreeze.use('pyseabreeze')
import seabreeze.spectrometers as sb

devs = sb.list_devices()
print(devs)
spec = sb.Spectrometer(devs[0])
print(spec.model)

并收到错误消息:

[<SeaBreezeDevice USB2000PLUS:FLMS02379>]
Traceback (most recent call last):
  File "C:\Users\Raman Lab\AppData\Local\Continuum\Anaconda3\lib\site-packages\seabreeze\pyseabreeze\interfaces\common.py", line 14, in decorated_func
    return func(*args, **kwargs)
  File "C:\Users\Raman Lab\AppData\Local\Continuum\Anaconda3\lib\site-packages\seabreeze\pyseabreeze\interfaces\spectrometer.py", line 46, in open
    self.open_device(device.handle)
  File "C:\Users\Raman Lab\AppData\Local\Continuum\Anaconda3\lib\site-packages\seabreeze\pyseabreeze\interfaces\communication.py", line 37, in open_device
    device.set_configuration()
  File "C:\Users\Raman Lab\AppData\Local\Continuum\Anaconda3\lib\site-packages\usb\core.py", line 869, in set_configuration
    self._ctx.managed_set_configuration(self, configuration)
  File "C:\Users\Raman Lab\AppData\Local\Continuum\Anaconda3\lib\site-packages\usb\core.py", line 102, in wrapper
    return f(self, *args, **kwargs)
  File "C:\Users\Raman Lab\AppData\Local\Continuum\Anaconda3\lib\site-packages\usb\core.py", line 148, in managed_set_configuration
    self.backend.set_configuration(self.handle, cfg.bConfigurationValue)
  File "C:\Users\Raman Lab\AppData\Local\Continuum\Anaconda3\lib\site-packages\usb\backend\libusb0.py", line 493, in set_configuration
    _check(_lib.usb_set_configuration(dev_handle, config_value))
  File "C:\Users\Raman Lab\AppData\Local\Continuum\Anaconda3\lib\site-packages\usb\backend\libusb0.py", line 431, in _check
    raise USBError(errmsg, ret)
usb.core.USBError: [Errno None] b'libusb0-dll:err [set_configuration] could not set config 1: win error: The parameter is incorrect.\r\n'
Traceback (most recent call last):

  File "<ipython-input-9-ead886eb3666>", line 1, in <module>
    runfile('C:/Users/Raman Lab/Python code/Spectrometers/spec_testing.py', wdir='C:/Users/Raman Lab/Python code/Spectrometers')

  File "C:\Users\Raman Lab\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)

  File "C:\Users\Raman Lab\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/Raman Lab/Python code/Spectrometers/spec_testing.py", line 7, in <module>
    spec = sb.Spectrometer(devs[0])

  File "C:\Users\Raman Lab\AppData\Local\Continuum\Anaconda3\lib\site-packages\seabreeze\spectrometers.py", line 62, in __init__
    self._open_device(device)

  File "C:\Users\Raman Lab\AppData\Local\Continuum\Anaconda3\lib\site-packages\seabreeze\spectrometers.py", line 90, in _open_device
    lib.device_open(self._dev)

  File "C:\Users\Raman Lab\AppData\Local\Continuum\Anaconda3\lib\site-packages\seabreeze\pyseabreeze\wrapper.py", line 81, in device_open
    return device.interface.open(device)

  File "C:\Users\Raman Lab\AppData\Local\Continuum\Anaconda3\lib\site-packages\seabreeze\pyseabreeze\interfaces\common.py", line 23, in decorated_func
    raise SeaBreezeError(msg)

SeaBreezeError: An error occured during opening.

感谢您的帮助!

编辑: 出于某种原因,我想出了这个,认为这可能有助于描述这个问题。如果我从cmd.exe进入交互式python并粘贴代码而不是手动输入代码,我会得到同样的错误。这让我觉得(很可能是我错了)它与进口速度有某种联系。我尝试在第3行和第5行之间添加一个睡眠几秒钟,以模拟在输入提示时发生的情况,但这没有帮助。我希望这足以描述。

1 个答案:

答案 0 :(得分:1)

你最初的猜测并不遥远。但是,在使用Python 2.7的Windows 7 32位上,我遇到了完全相同的问题。链接到的示例代码gchaks运行正常。 当我尝试快速连续几次执行脚本时,错误消息已更改并指向空设备列表。

后我加了2秒的延迟

devs = sb.list_devices()

这解决了这个问题。你的代码在Linux机器上运行得很好。

另一个提示:如果您的代码应该运行一次,但是您遇到另一条错误消息,请确保关闭设备连接或断开并重新连接光谱仪。