我正嘗試使用海風(seabreeze)和pyseabreeze(pyseabreeze)與海洋光學光譜儀進行交互。Python代碼以交互式提示運行,但不以腳本運行
如果我在Windows命令提示符(CMD)進入蟒蛇並輸入線下面的代碼行,它的工作原理。但是,如果我將它放在腳本(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行之間添加睡眠幾秒鐘,以模擬在輸入提示時發生的情況,但這並沒有幫助。我希望這是足夠描述性的。
這看起來像路徑的問題。檢查以確保您可能需要的任何dll都包含在路徑中。 – MiltoxBeyond
我不認爲該代碼產生了該錯誤消息。回溯的第一行提到了spec_sample_plot.py,第10行。您提供的代碼只有8行。請複製粘貼生成錯誤的* exact *程序。 –
@Robᵩ我在代碼中有一些評論,我沒有在這裏發佈該行號不匹配的帳戶。我已編輯帖子,僅包含此代碼的錯誤讀數。 –