US_EIA Fix import path#2072
Conversation
Code fix unenergy
There was a problem hiding this comment.
Code Review
This pull request adds the repository root directory to sys.path in scripts/us_eia/opendata/process/common.py. The reviewer notes that adding both the root directory and the util/ subdirectory to sys.path is a Python anti-pattern that can cause duplicate module loading, and recommends transitioning to absolute imports.
| _MODULE_DIR = os.path.dirname(os.path.abspath(__file__)) | ||
| _SCRIPT_PATH = os.path.dirname(os.path.abspath(__file__)) | ||
| sys.path.append(os.path.join(_SCRIPT_PATH, '../../../../util/')) | ||
| sys.path.append(os.path.join(_SCRIPT_PATH, '../../../../')) |
There was a problem hiding this comment.
I did not realize that it could break the import scripts. I wonder if it can impact other imports as well. Instead of fixing various imports, can we revert the changes to file_util.py in my PR and instead handle it in differ_utils.py
-from util.file_util import FileIO
-from util.file_util import file_get_matching
+import sys
+_SCRIPT_DIR = os.path.dirname(os.path.abspath(file))
+_DATA_DIR = os.path.dirname(os.path.dirname(_SCRIPT_DIR))
+sys.path.append(os.path.join(_DATA_DIR, 'util'))
+
+from file_util import FileIO
+from file_util import file_get_matching
There was a problem hiding this comment.
yes sure this will be fine.
This PR fixes a ModuleNotFoundError: No module named 'util' error that blocked the execution of us_eia scripts (both locally and when running in cloud environments).
The issue arose because of a recent change in the shared util/ package, which broke the legacy directory-appending strategy used by the EIA ingestion scripts.