diff --git a/emulator/brainflow_emulator/cyton_windows.py b/emulator/brainflow_emulator/cyton_windows.py index 328097875..b5360e15b 100644 --- a/emulator/brainflow_emulator/cyton_windows.py +++ b/emulator/brainflow_emulator/cyton_windows.py @@ -4,7 +4,7 @@ import sys import time -import pkg_resources +from importlib.resources import files from brainflow_emulator.emulate_common import TestFailureError, Listener, log_multilines from serial import Serial @@ -18,7 +18,15 @@ def read(port, num_bytes): def get_isntaller(): - return pkg_resources.resource_filename(__name__, os.path.join('com0com', 'setup_com0com_W7_x64_signed.exe')) + try: + resource = files(__name__).joinpath('com0com').joinpath('setup_com0com_W7_x64_signed.exe') + return str(resource) + except (TypeError, AttributeError): + # Fallback for: + # 1. Python < 3.9 (importlib.resources.files not available) + # 2. NixOS/packaging edge cases where importlib.resources may not work + import pkg_resources + return pkg_resources.resource_filename(__name__, os.path.join('com0com', 'setup_com0com_W7_x64_signed.exe')) def install_com0com(): diff --git a/emulator/brainflow_emulator/freeeeg32_windows.py b/emulator/brainflow_emulator/freeeeg32_windows.py index 5b419beda..223f56ac0 100644 --- a/emulator/brainflow_emulator/freeeeg32_windows.py +++ b/emulator/brainflow_emulator/freeeeg32_windows.py @@ -4,7 +4,7 @@ import sys import time -import pkg_resources +from importlib.resources import files from brainflow_emulator.emulate_common import TestFailureError, log_multilines from brainflow_emulator.freeeeg32_emulator import Listener from serial import Serial @@ -19,7 +19,15 @@ def read(port, num_bytes): def get_isntaller(): - return pkg_resources.resource_filename(__name__, os.path.join('com0com', 'setup_com0com_W7_x64_signed.exe')) + try: + resource = files(__name__).joinpath('com0com').joinpath('setup_com0com_W7_x64_signed.exe') + return str(resource) + except (TypeError, AttributeError): + # Fallback for: + # 1. Python < 3.9 (importlib.resources.files not available) + # 2. NixOS/packaging edge cases where importlib.resources may not work + import pkg_resources + return pkg_resources.resource_filename(__name__, os.path.join('com0com', 'setup_com0com_W7_x64_signed.exe')) def install_com0com(): diff --git a/emulator/brainflow_emulator/knightboard_windows.py b/emulator/brainflow_emulator/knightboard_windows.py index c63cdceb4..4b08850d5 100644 --- a/emulator/brainflow_emulator/knightboard_windows.py +++ b/emulator/brainflow_emulator/knightboard_windows.py @@ -4,7 +4,7 @@ import sys import time -import pkg_resources +from importlib.resources import files from brainflow_emulator.emulate_common import TestFailureError, log_multilines from brainflow_emulator.knightboard_emulator import Listener from serial import Serial @@ -19,7 +19,15 @@ def read(port, num_bytes): def get_isntaller(): - return pkg_resources.resource_filename(__name__, os.path.join('com0com', 'setup_com0com_W7_x64_signed.exe')) + try: + resource = files(__name__).joinpath('com0com').joinpath('setup_com0com_W7_x64_signed.exe') + return str(resource) + except (TypeError, AttributeError): + # Fallback for: + # 1. Python < 3.9 (importlib.resources.files not available) + # 2. NixOS/packaging edge cases where importlib.resources may not work + import pkg_resources + return pkg_resources.resource_filename(__name__, os.path.join('com0com', 'setup_com0com_W7_x64_signed.exe')) def install_com0com(): diff --git a/python_package/brainflow/board_shim.py b/python_package/brainflow/board_shim.py index a81a58375..c3c61888e 100644 --- a/python_package/brainflow/board_shim.py +++ b/python_package/brainflow/board_shim.py @@ -7,7 +7,7 @@ from typing import List import numpy -import pkg_resources +from importlib.resources import files from brainflow.exit_codes import BrainFlowExitCodes, BrainFlowError from brainflow.utils import LogLevels from numpy.ctypeslib import ndpointer @@ -21,7 +21,7 @@ class BoardIds(enum.IntEnum): STREAMING_BOARD = -2 #: SYNTHETIC_BOARD = -1 #: CYTON_BOARD = 0 #: - GANGLION_BOARD = 1 #: + GANGLION_BOARD = 1 #: CYTON_DAISY_BOARD = 2 #: GALEA_BOARD = 3 #: GANGLION_WIFI_BOARD = 4 #: @@ -173,7 +173,15 @@ def __init__(self): dll_path = 'lib/libBoardController.dylib' else: dll_path = 'lib/libBoardController.so' - full_path = pkg_resources.resource_filename(__name__, dll_path) + try: + resource = files(__name__).joinpath(dll_path) + full_path = str(resource) + except (TypeError, AttributeError): + # Fallback for: + # 1. Python < 3.9 (importlib.resources.files not available) + # 2. NixOS/packaging edge cases where importlib.resources may not work + import pkg_resources + full_path = pkg_resources.resource_filename(__name__, dll_path) if os.path.isfile(full_path): dir_path = os.path.abspath(os.path.dirname(full_path)) # for python we load dll by direct path but this dll may depend on other dlls and they will not be found! diff --git a/python_package/brainflow/data_filter.py b/python_package/brainflow/data_filter.py index aeb243df8..c01be79e3 100644 --- a/python_package/brainflow/data_filter.py +++ b/python_package/brainflow/data_filter.py @@ -6,7 +6,7 @@ from typing import List, Tuple import numpy -import pkg_resources +from importlib.resources import files from brainflow.exit_codes import BrainFlowExitCodes, BrainFlowError from brainflow.utils import check_memory_layout_row_major, LogLevels from numpy.ctypeslib import ndpointer @@ -153,7 +153,15 @@ def __init__(self): dll_path = 'lib/libDataHandler.dylib' else: dll_path = 'lib/libDataHandler.so' - full_path = pkg_resources.resource_filename(__name__, dll_path) + try: + resource = files(__name__).joinpath(dll_path) + full_path = str(resource) + except (TypeError, AttributeError): + # Fallback for: + # 1. Python < 3.9 (importlib.resources.files not available) + # 2. NixOS/packaging edge cases where importlib.resources may not work + import pkg_resources + full_path = pkg_resources.resource_filename(__name__, dll_path) if os.path.isfile(full_path): dir_path = os.path.abspath(os.path.dirname(full_path)) # for python 3.8 PATH env var doesnt work anymore diff --git a/python_package/brainflow/ml_model.py b/python_package/brainflow/ml_model.py index 3e5fd71d2..3dd7a8317 100644 --- a/python_package/brainflow/ml_model.py +++ b/python_package/brainflow/ml_model.py @@ -7,7 +7,7 @@ from typing import List import numpy -import pkg_resources +from importlib.resources import files from brainflow.board_shim import BrainFlowError, LogLevels from brainflow.exit_codes import BrainFlowExitCodes from numpy.ctypeslib import ndpointer @@ -78,7 +78,15 @@ def __init__(self): dll_path = 'lib/libMLModule.dylib' else: dll_path = 'lib/libMLModule.so' - full_path = pkg_resources.resource_filename(__name__, dll_path) + try: + resource = files(__name__).joinpath(dll_path) + full_path = str(resource) + except (TypeError, AttributeError): + # Fallback for: + # 1. Python < 3.9 (importlib.resources.files not available) + # 2. NixOS/packaging edge cases where importlib.resources may not work + import pkg_resources + full_path = pkg_resources.resource_filename(__name__, dll_path) if os.path.isfile(full_path): # for python we load dll by direct path but this dll may depend on other dlls and they will not be found! # to solve it we can load all of them before loading the main one or change PATH\LD_LIBRARY_PATH env var.