首页 > 解决方案 > PyInstaller & Pymeasure : NotImplementedError

问题描述

我目前在依赖 Pymeasure 库的代码上使用 PyInstaller 时遇到困难。该程序在提示符下工作正常,但从 PyInstaller 生成的可执行文件启动时却不行。
这是一个在提示下工作但在冻结时不工作的代码的简单示例:

import visa
from pymeasure.instruments.keithley import Keithley2000, Keithley2400

rm = visa.ResourceManager()
list_available = rm.list_resources()
print(list_available)

keithley = Keithley2400("GPIB1::23")

keithley.apply_current()                # Sets up to source current
keithley.source_current_range = 10e-3   # Sets the source current range to 10 mA
keithley.compliance_voltage = 10        # Sets the compliance voltage to 10 V
keithley.source_current = 0             # Sets the source current to 0 mA
keithley.enable_source()                # Enables the source output

keithley.measure_voltage()              # Sets up to measure voltage

keithley.ramp_to_current(5e-3)          # Ramps the current to 5 mA
print(keithley.voltage)                 # Prints the voltage in Volts

keithley.shutdown()                     # Ramps the current to 0 mA and disables output

这是我运行可执行文件时的输出:在此处输入图像描述

请注意,我安装了 PyVISA 1.9.1。

为什么我会收到此错误,我该如何解决?

标签: pythonpyinstallerpyvisa

解决方案


您需要确保在 PyInstaller 项目中包含 PyVisa 的包元数据。PyInstaller 有一个用于该工作的实用程序挂钩;创建一个hook-pyvista.py钩子文件(如果你还没有的话):

from PyInstaller.utils.hooks import copy_metadata

datas = copy_metadata("pyvisa")

并使用--additional-hooks-dir命令行开关告诉 PyInstaller。有关更多详细信息,请参阅有关挂钩如何工作的文档

pymeasurement依赖于该pyvisa.__version__属性来确定您是否安装了该项目的正确版本。但pyvisa.__version__ 默认为"unknown"除非它可以找到它的元数据文件,这将提供pkg_resources所需的元数据来检索它的版本。

__version__您可以通过自己导入并测试属性来验证 PyVisa 是否已正确安装:

import pyvisa
print("PyVisa version", pyvisa.__version__)

推荐阅读