Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NI instruments do not work with Python 3.12 due to nicaiu dependency #1008

Open
Sionwage opened this issue Dec 20, 2023 · 7 comments
Open

NI instruments do not work with Python 3.12 due to nicaiu dependency #1008

Sionwage opened this issue Dec 20, 2023 · 7 comments
Labels
dependency Issue in an external dependency help wanted instrument

Comments

@Sionwage
Copy link
Contributor

I would like to report success in using PyMeasure with Python 3.12 when I comment out line 59 in pymeasure\instruments\__init__.py

More specifically, the submodule 'NI' within instruments wants to load DAQmx which ends up looking for ctypes.windll.nicaiu which if not installed raises an AttributeError.

Running the following:

from pymeasure.instruments import Instrument

Full error message

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
File ~\.pyenv\pyenv-win\versions\3.12.0\Lib\ctypes\__init__.py:450, in LibraryLoader.__getattr__(self, name)
    449 try:
--> 450     dll = self._dlltype(name)
    451 except OSError:

File ~\.pyenv\pyenv-win\versions\3.12.0\Lib\ctypes\__init__.py:379, in CDLL.__init__(self, name, mode, handle, use_errno, use_last_error, winmode)
    378 if handle is None:
--> 379     self._handle = _dlopen(self._name, mode)
    380 else:

FileNotFoundError: Could not find module 'nicaiu' (or one of its dependencies). Try using the full path with constructor syntax.

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
Cell In[1], line 6
      3 from io import BytesIO
      5 import numpy as np
----> 6 from pymeasure.instruments import Instrument, RangeException, discreteTruncate
      7 from pyvisa import VisaIOError
      9 from pymeasure.adapters import PrologixAdapter

File ~\AppData\Local\pypoetry\Cache\virtualenvs\py3-12-pymeasure-env-DpKIcySs-py3.12\Lib\site-packages\pymeasure\instruments\__init__.py:59
     57 from . import mksinst
     58 from . import newport
---> 59 from . import ni
     60 from . import novanta
     61 from . import oxfordinstruments

File ~\AppData\Local\pypoetry\Cache\virtualenvs\py3-12-pymeasure-env-DpKIcySs-py3.12\Lib\site-packages\pymeasure\instruments\ni\__init__.py:26
      1 #
      2 # This file is part of the PyMeasure package.
      3 #
   (...)
     22 # THE SOFTWARE.
     23 #
     25 try:
---> 26     from .daqmx import DAQmx
     27 except OSError:
     28     # Error Logging is handled within package
     29     pass

File ~\AppData\Local\pypoetry\Cache\virtualenvs\py3-12-pymeasure-env-DpKIcySs-py3.12\Lib\site-packages\pymeasure\instruments\ni\daqmx.py:38
     36 try:
     37     if platform == "win32":
---> 38         nidaq = ctypes.windll.nicaiu
     39 except OSError as err:
     40     log.info('Failed loading the NI-DAQmx library. '
     41              + 'Check the NI-DAQmx documentation on how to '
     42              + 'install this external dependency. '
     43              + f'OSError: {err}')

File ~\.pyenv\pyenv-win\versions\3.12.0\Lib\ctypes\__init__.py:452, in LibraryLoader.__getattr__(self, name)
    450     dll = self._dlltype(name)
    451 except OSError:
--> 452     raise AttributeError(name)
    453 setattr(self, name, dll)
    454 return dll

AttributeError: nicaiu

Would this be a good instance of utilizing dynamic imports with the importlib module to prevent searching for the instrumental package from https://github.com/mabuchilab/Instrumental unless one is specifically importing NIQAQ from pymeasure.instruments.ni?

@BenediktBurger
Copy link
Member

Some relations to #662 , where there is a discussion about the NI drivers.

@Sionwage
Copy link
Contributor Author

#919 Solves this issue since daqmx.py doesn't get imported on initializing the Instrument class.

@Sionwage
Copy link
Contributor Author

Solved by 919

@BenediktBurger
Copy link
Member

Now pymeasure works for the other instruments, but the NI instruments won't work with python 3.12, therefore I reopen this issue.

@BenediktBurger BenediktBurger added instrument dependency Issue in an external dependency labels Feb 20, 2024
@BenediktBurger BenediktBurger changed the title Python 3.12 seems to work if NI instruments gets commented out NI instruments do not work with Python 3.12 due to nicaiu dependency Feb 20, 2024
@Sionwage
Copy link
Contributor Author

I just want to clarify that I am not asserting that the NI instruments don't work, but the code in daqmx.py:

try:
    if platform == "win32":
        nidaq = ctypes.windll.nicaiu
except OSError as err:
    log.info('Failed loading the NI-DAQmx library. '
             + 'Check the NI-DAQmx documentation on how to '
             + 'install this external dependency. '
             + f'OSError: {err}')
    raise

was what was previously preventing 3.12 being able to run pymeasure before.

Does import ctypes; nidaq = ctypes.windll.nicaiu throw an error for someone with the NIDAQ installed?

@Sionwage
Copy link
Contributor Author

Also, probably need to add an exception in test_all_instruments.py for find_devices_in_module(module) to except AttributeError for nicaiu to get testing to pass.

@BenediktBurger
Copy link
Member

#1051 adds that error case to test_all_instruments.py.
Thanks for finding them, @Sionwage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependency Issue in an external dependency help wanted instrument
Projects
None yet
Development

No branches or pull requests

2 participants