Skip to content

nitypes: Fix PackageNotFoundError when importing nitypes from a PyInstaller EXE - #310

Merged
bkeryan merged 5 commits into
mainfrom
users/bkeryan/fix-version-in-pyinstaller
Nov 11, 2026
Merged

bkeryan merged 5 commits into
mainfrom
users/bkeryan/fix-version-in-pyinstaller

Conversation

@bkeryan

@bkeryan bkeryan commented Nov 7, 2026 •

Copy link
Copy Markdown
Collaborator

What does this Pull Request accomplish?

Make the nitypes.__version__ attribute compatible with PyInstaller:

Revert workaround for broken pip in run_unit_tests_oldest_deps.yml, which I missed in a previous PR.

Corresponding nidaqmx PR: ni/nidaqmx-python#1050

Why should this Pull Request be merged?

This is part of the fix for ni/nidaqmx-python#1042

Fixes #311

What testing has been done?

Built and tested a PyInstaller EXE of https://github.com/ni/measurement-plugin-python/tree/main/examples/nidaqmx_analog_input

  • Add print(nitypes.__version__) and print(nidaqmx.__version__) to main()
  • poetry install
  • poetry run pip install pyinstaller
  • poetry run poetry run pyinstaller --onefile --console measurement.py

Before:

PS C:\dev\measurement-plugin-python\examples\nidaqmx_analog_input> .\dist\measurement.exe
Traceback (most recent call last):
  File "importlib\metadata\__init__.py", line 407, in from_name
StopIteration

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "measurement.py", line 13, in <module>
    from nidaqmx.constants import TaskMode
  File "pyimod02_importers.py", line 457, in exec_module
  File "nidaqmx\__init__.py", line 13, in <module>
  File "pyimod02_importers.py", line 457, in exec_module
  File "nidaqmx\scale.py", line 5, in <module>
  File "pyimod02_importers.py", line 457, in exec_module
  File "nidaqmx\utils.py", line 8, in <module>
  File "pyimod02_importers.py", line 457, in exec_module
  File "nidaqmx\_base_interpreter.py", line 6, in <module>
  File "pyimod02_importers.py", line 457, in exec_module
  File "nitypes\__init__.py", line 6, in <module>
  File "importlib\metadata\__init__.py", line 987, in version
  File "importlib\metadata\__init__.py", line 960, in distribution
  File "importlib\metadata\__init__.py", line 409, in from_name
importlib.metadata.PackageNotFoundError: No package metadata was found for nitypes
[PYI-20192:ERROR] Failed to execute script 'measurement' due to unhandled exception!

After: (I do not have the discovery service installed on this machine, so this failure is expected)

PS C:\dev\measurement-plugin-python\examples\nidaqmx_analog_input> .\measurement.exe
1.2.0.dev1
1.8.0.dev0
2026-11-11 15:10:28,652 ERROR: Unable to register with discovery service. Possible reason: discovery service not running.
Traceback (most recent call last):
  File "measurement.py", line 98, in <module>
    main()
    ~~~~^^
  File "click\core.py", line 1653, in __call__
  File "click\core.py", line 1560, in main
  File "click\core.py", line 1414, in invoke
  File "click\core.py", line 911, in invoke
  File "measurement.py", line 93, in main
    with measurement_service.host_service():
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "ni_measurement_plugin_sdk_service\measurement\service.py", line 530, in host_service
  File "ni_measurement_plugin_sdk_service\_internal\service_manager.py", line 151, in start
  File "ni\measurementlink\discovery\v1\client\_client.py", line 104, in register_service
  File "ni\measurementlink\discovery\v1\client\_client.py", line 65, in _get_stub
  File "ni\measurementlink\discovery\v1\client\_support.py", line 43, in _get_discovery_service_address
  File "ni\measurementlink\discovery\v1\client\_support.py", line 137, in _get_key_file_path
  File "ni\measurementlink\discovery\v1\client\_support.py", line 144, in _get_key_file_directory
  File "ni\measurementlink\discovery\v1\client\_support.py", line 189, in _get_nipath
FileNotFoundError: [WinError 2] The system cannot find the file specified
[PYI-29700:ERROR] Failed to execute script 'measurement' due to unhandled exception!

@bkeryan
bkeryan requested a review from csjall as a code owner November 7, 2026 18:30
@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Test Results

    44 files  ±0      44 suites  ±0   11m 21s ⏱️ -30s
 2 312 tests ±0   2 312 ✅ ±0  0 💤 ±0  0 ❌ ±0 
52 707 runs  ±0  52 707 ✅ ±0  0 💤 ±0  0 ❌ ±0 

Results for commit 9f3d358. ± Comparison against base commit 2f519ca.

This pull request removes 1 and adds 1 tests. Note that renamed tests count towards both.
tests.unit.test_placeholder ‑ test___placeholder
tests.unit.test_version ‑ test___version_attribute___matches_importlib_metadata

♻️ This comment has been updated with latest results.

@bkeryan
bkeryan requested a review from mshafer-NI November 7, 2026 18:37
@bkeryan

bkeryan commented Nov 7, 2026 •

Copy link
Copy Markdown
Collaborator Author

@mshafer-NI Please let me know if you have any concerns with this approach or recommend doing it another way.

I also considered:

  • Write version to a VERSION or _version.txt file
  • Leave version set to "unknown" in PyInstaller EXEs
  • Move __version__ to a module-level __getattr__, and possibly deprecate it (as click has done)

Comment thread src/nitypes/__init__.py Resolved
Signed-off-by: Brad Keryan <brad.keryan@ni.com>
@bkeryan
bkeryan force-pushed the users/bkeryan/fix-version-in-pyinstaller branch from d6810e7 to b535502 Compare November 11, 2026 21:13
Signed-off-by: Brad Keryan <brad.keryan@ni.com>
Signed-off-by: Brad Keryan <brad.keryan@ni.com>
Signed-off-by: Brad Keryan <brad.keryan@ni.com>
Signed-off-by: Brad Keryan <brad.keryan@ni.com>
@bkeryan
bkeryan merged commit 82061e3 into main Nov 11, 2026
39 checks passed
@bkeryan
bkeryan deleted the users/bkeryan/fix-version-in-pyinstaller branch November 11, 2026 21:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Importing nitypes fails in applications built with PyInstaller

2 participants