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



我正在尝试使用seabreeze(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:UsersRaman LabAppDataLocalContinuumAnaconda3libsite-packagesseabreezepyseabreezeinterfacescommon.py", line 14, in decorated_func
    return func(*args, **kwargs)
  File "C:UsersRaman LabAppDataLocalContinuumAnaconda3libsite-packagesseabreezepyseabreezeinterfacesspectrometer.py", line 46, in open
    self.open_device(device.handle)
  File "C:UsersRaman LabAppDataLocalContinuumAnaconda3libsite-packagesseabreezepyseabreezeinterfacescommunication.py", line 37, in open_device
    device.set_configuration()
  File "C:UsersRaman LabAppDataLocalContinuumAnaconda3libsite-packagesusbcore.py", line 869, in set_configuration
    self._ctx.managed_set_configuration(self, configuration)
  File "C:UsersRaman LabAppDataLocalContinuumAnaconda3libsite-packagesusbcore.py", line 102, in wrapper
    return f(self, *args, **kwargs)
  File "C:UsersRaman LabAppDataLocalContinuumAnaconda3libsite-packagesusbcore.py", line 148, in managed_set_configuration
    self.backend.set_configuration(self.handle, cfg.bConfigurationValue)
  File "C:UsersRaman LabAppDataLocalContinuumAnaconda3libsite-packagesusbbackendlibusb0.py", line 493, in set_configuration
    _check(_lib.usb_set_configuration(dev_handle, config_value))
  File "C:UsersRaman LabAppDataLocalContinuumAnaconda3libsite-packagesusbbackendlibusb0.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.rn'
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:UsersRaman LabAppDataLocalContinuumAnaconda3libsite-packagesspyderutilssitesitecustomize.py", line 866, in runfile
    execfile(filename, namespace)
  File "C:UsersRaman LabAppDataLocalContinuumAnaconda3libsite-packagesspyderutilssitesitecustomize.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:UsersRaman LabAppDataLocalContinuumAnaconda3libsite-packagesseabreezespectrometers.py", line 62, in __init__
    self._open_device(device)
  File "C:UsersRaman LabAppDataLocalContinuumAnaconda3libsite-packagesseabreezespectrometers.py", line 90, in _open_device
    lib.device_open(self._dev)
  File "C:UsersRaman LabAppDataLocalContinuumAnaconda3libsite-packagesseabreezepyseabreezewrapper.py", line 81, in device_open
    return device.interface.open(device)
  File "C:UsersRaman LabAppDataLocalContinuumAnaconda3libsite-packagesseabreezepyseabreezeinterfacescommon.py", line 23, in decorated_func
    raise SeaBreezeError(msg)
SeaBreezeError: An error occured during opening.

感谢您的帮助!

编辑:由于某种原因,我弄清楚了这一点,认为这可能有助于描述这个问题。如果我从cmd.exe输入Interactive Python并粘贴代码,而不是手动键入,我会遇到相同的错误。这使我认为(很可能我错了)它与进口速度相关。我尝试在第3和第5行之间添加睡眠几秒钟,以模拟我在提示中输入时会发生什么,但这无济于事。我希望这足够描述。

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

后添加了2秒钟的延迟

devs = sb.list_devices()

解决了问题。您的代码在Linux机器上正常运行。

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

最新更新