Skip to content

Commit 5ebe075

Browse files
fix: replace deprecated pkg_resources with importlib.resources (#792)
* fix: replace deprecated pkg_resources with importlib.resources Updated BrainFlow EEG files: - emulator/: cyton_windows.py, freeeeg32_windows.py, knightboard_windows.py - python_package/brainflow/: board_shim.py, data_filter.py, ml_model.py ✅ Eliminates pkg_resources deprecation warnings * docs: detailed fallback comments for NixOS/Python<3.9 compatibility
1 parent 9ef5fe3 commit 5ebe075

6 files changed

Lines changed: 61 additions & 13 deletions

File tree

emulator/brainflow_emulator/cyton_windows.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import sys
55
import time
66

7-
import pkg_resources
7+
from importlib.resources import files
88
from brainflow_emulator.emulate_common import TestFailureError, Listener, log_multilines
99
from serial import Serial
1010

@@ -18,7 +18,15 @@ def read(port, num_bytes):
1818

1919

2020
def get_isntaller():
21-
return pkg_resources.resource_filename(__name__, os.path.join('com0com', 'setup_com0com_W7_x64_signed.exe'))
21+
try:
22+
resource = files(__name__).joinpath('com0com').joinpath('setup_com0com_W7_x64_signed.exe')
23+
return str(resource)
24+
except (TypeError, AttributeError):
25+
# Fallback for:
26+
# 1. Python < 3.9 (importlib.resources.files not available)
27+
# 2. NixOS/packaging edge cases where importlib.resources may not work
28+
import pkg_resources
29+
return pkg_resources.resource_filename(__name__, os.path.join('com0com', 'setup_com0com_W7_x64_signed.exe'))
2230

2331

2432
def install_com0com():

emulator/brainflow_emulator/freeeeg32_windows.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import sys
55
import time
66

7-
import pkg_resources
7+
from importlib.resources import files
88
from brainflow_emulator.emulate_common import TestFailureError, log_multilines
99
from brainflow_emulator.freeeeg32_emulator import Listener
1010
from serial import Serial
@@ -19,7 +19,15 @@ def read(port, num_bytes):
1919

2020

2121
def get_isntaller():
22-
return pkg_resources.resource_filename(__name__, os.path.join('com0com', 'setup_com0com_W7_x64_signed.exe'))
22+
try:
23+
resource = files(__name__).joinpath('com0com').joinpath('setup_com0com_W7_x64_signed.exe')
24+
return str(resource)
25+
except (TypeError, AttributeError):
26+
# Fallback for:
27+
# 1. Python < 3.9 (importlib.resources.files not available)
28+
# 2. NixOS/packaging edge cases where importlib.resources may not work
29+
import pkg_resources
30+
return pkg_resources.resource_filename(__name__, os.path.join('com0com', 'setup_com0com_W7_x64_signed.exe'))
2331

2432

2533
def install_com0com():

emulator/brainflow_emulator/knightboard_windows.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import sys
55
import time
66

7-
import pkg_resources
7+
from importlib.resources import files
88
from brainflow_emulator.emulate_common import TestFailureError, log_multilines
99
from brainflow_emulator.knightboard_emulator import Listener
1010
from serial import Serial
@@ -19,7 +19,15 @@ def read(port, num_bytes):
1919

2020

2121
def get_isntaller():
22-
return pkg_resources.resource_filename(__name__, os.path.join('com0com', 'setup_com0com_W7_x64_signed.exe'))
22+
try:
23+
resource = files(__name__).joinpath('com0com').joinpath('setup_com0com_W7_x64_signed.exe')
24+
return str(resource)
25+
except (TypeError, AttributeError):
26+
# Fallback for:
27+
# 1. Python < 3.9 (importlib.resources.files not available)
28+
# 2. NixOS/packaging edge cases where importlib.resources may not work
29+
import pkg_resources
30+
return pkg_resources.resource_filename(__name__, os.path.join('com0com', 'setup_com0com_W7_x64_signed.exe'))
2331

2432

2533
def install_com0com():

python_package/brainflow/board_shim.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from typing import List
88

99
import numpy
10-
import pkg_resources
10+
from importlib.resources import files
1111
from brainflow.exit_codes import BrainFlowExitCodes, BrainFlowError
1212
from brainflow.utils import LogLevels
1313
from numpy.ctypeslib import ndpointer
@@ -21,7 +21,7 @@ class BoardIds(enum.IntEnum):
2121
STREAMING_BOARD = -2 #:
2222
SYNTHETIC_BOARD = -1 #:
2323
CYTON_BOARD = 0 #:
24-
GANGLION_BOARD = 1 #:
24+
GANGLION_BOARD = 1 #:
2525
CYTON_DAISY_BOARD = 2 #:
2626
GALEA_BOARD = 3 #:
2727
GANGLION_WIFI_BOARD = 4 #:
@@ -173,7 +173,15 @@ def __init__(self):
173173
dll_path = 'lib/libBoardController.dylib'
174174
else:
175175
dll_path = 'lib/libBoardController.so'
176-
full_path = pkg_resources.resource_filename(__name__, dll_path)
176+
try:
177+
resource = files(__name__).joinpath(dll_path)
178+
full_path = str(resource)
179+
except (TypeError, AttributeError):
180+
# Fallback for:
181+
# 1. Python < 3.9 (importlib.resources.files not available)
182+
# 2. NixOS/packaging edge cases where importlib.resources may not work
183+
import pkg_resources
184+
full_path = pkg_resources.resource_filename(__name__, dll_path)
177185
if os.path.isfile(full_path):
178186
dir_path = os.path.abspath(os.path.dirname(full_path))
179187
# for python we load dll by direct path but this dll may depend on other dlls and they will not be found!

python_package/brainflow/data_filter.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from typing import List, Tuple
77

88
import numpy
9-
import pkg_resources
9+
from importlib.resources import files
1010
from brainflow.exit_codes import BrainFlowExitCodes, BrainFlowError
1111
from brainflow.utils import check_memory_layout_row_major, LogLevels
1212
from numpy.ctypeslib import ndpointer
@@ -153,7 +153,15 @@ def __init__(self):
153153
dll_path = 'lib/libDataHandler.dylib'
154154
else:
155155
dll_path = 'lib/libDataHandler.so'
156-
full_path = pkg_resources.resource_filename(__name__, dll_path)
156+
try:
157+
resource = files(__name__).joinpath(dll_path)
158+
full_path = str(resource)
159+
except (TypeError, AttributeError):
160+
# Fallback for:
161+
# 1. Python < 3.9 (importlib.resources.files not available)
162+
# 2. NixOS/packaging edge cases where importlib.resources may not work
163+
import pkg_resources
164+
full_path = pkg_resources.resource_filename(__name__, dll_path)
157165
if os.path.isfile(full_path):
158166
dir_path = os.path.abspath(os.path.dirname(full_path))
159167
# for python 3.8 PATH env var doesnt work anymore

python_package/brainflow/ml_model.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from typing import List
88

99
import numpy
10-
import pkg_resources
10+
from importlib.resources import files
1111
from brainflow.board_shim import BrainFlowError, LogLevels
1212
from brainflow.exit_codes import BrainFlowExitCodes
1313
from numpy.ctypeslib import ndpointer
@@ -78,7 +78,15 @@ def __init__(self):
7878
dll_path = 'lib/libMLModule.dylib'
7979
else:
8080
dll_path = 'lib/libMLModule.so'
81-
full_path = pkg_resources.resource_filename(__name__, dll_path)
81+
try:
82+
resource = files(__name__).joinpath(dll_path)
83+
full_path = str(resource)
84+
except (TypeError, AttributeError):
85+
# Fallback for:
86+
# 1. Python < 3.9 (importlib.resources.files not available)
87+
# 2. NixOS/packaging edge cases where importlib.resources may not work
88+
import pkg_resources
89+
full_path = pkg_resources.resource_filename(__name__, dll_path)
8290
if os.path.isfile(full_path):
8391
# for python we load dll by direct path but this dll may depend on other dlls and they will not be found!
8492
# to solve it we can load all of them before loading the main one or change PATH\LD_LIBRARY_PATH env var.

0 commit comments

Comments
 (0)