You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First of all, thank you for your job and for making this repo available for everyone!
I was trying to understand how your codec generator works. While using the repository as a submodule of another repo, I noticed that the function get_config_module from tools_config.py allways attempts to dynamically import a module based on the file name in CONFIG_ARGS['config_file']. However, it exclusively uses importlib.import_module, which relies on the module being discoverable in Python's sys.path. This introduces a minor limitation: if the specified file is not located in a directory already included in sys.path, the function will fail with a ModuleNotFoundError.
Here are two specific cases I encountered:
If I generate a configuration file called config.py in a directory different from the current working directory (as shown in the attached image), cbexigen imports the config.py file located in the current working directory instead of the one in config_dir directory
If I generate a configuration file named config_default located in a directory other than the current working directory, I get a ModuleNotFoundError:
I think that it is a path dependency issue: The code does not use the full path of the configuration file; it extracts only the file name. This assumes the file resides in a directory that is already accessible via sys.path, which may not always be the case.
I tried implementing a solution using importlib.util, which provides a more robust approach by loading the module directly from its specified file path. This method does not rely on the file being located in sys.path or having a unique name.
Here’s how the proposed solution addresses the issues:
Direct Path Import: By using importlib.util.spec_from_file_location and module_from_spec, the module is loaded explicitly from the provided path. This bypasses the sys.path mechanism, ensuring that the correct file is always imported.
Name Handling: The module name is derived programmatically from the file's base name using stem property (e.g., config.py becomes config), without manually checking if the string .py is contained in the name.
Here’s the updated implementation, which also ensures backward compatibility:
My project is formed with the following structure:
src: source code, where I have configuration files and some utilities to craft V2G messages via Python
cbexigen: repo as submodule
README
etc
My working environment was formed by:
Pycharm IDE
Python virtual environment based on Python3.11
I have also created a pyproject.toml in order to make your library installable and allow Pycharm to automatically resolve all dependencies without the need of manually adding the cbexigen/src to sys.path. It also allow autocompletion when the instruction import cbexigen is used
The pyproject.toml I am using is quite similar to:
Describe the problem
First of all, thank you for your job and for making this repo available for everyone!
I was trying to understand how your codec generator works. While using the repository as a submodule of another repo, I noticed that the function
get_config_module
fromtools_config.py
allways attempts to dynamically import a module based on the file name inCONFIG_ARGS['config_file']
. However, it exclusively usesimportlib.import_module
, which relies on the module being discoverable in Python'ssys.path
. This introduces a minor limitation: if the specified file is not located in a directory already included in sys.path, the function will fail with a ModuleNotFoundError.Here are two specific cases I encountered:
config.py
in a directory different from the current working directory (as shown in the attached image), cbexigen imports theconfig.py
file located in the current working directory instead of the one inconfig_dir
directoryconfig_default
located in a directory other than the current working directory, I get a ModuleNotFoundError:I think that it is a path dependency issue: The code does not use the full path of the configuration file; it extracts only the file name. This assumes the file resides in a directory that is already accessible via sys.path, which may not always be the case.
The function we are talking about is:
Describe your solution
I tried implementing a solution using
importlib.util
, which provides a more robust approach by loading the module directly from its specified file path. This method does not rely on the file being located insys.path
or having a unique name.Here’s how the proposed solution addresses the issues:
importlib.util.spec_from_file_location
andmodule_from_spec
, the module is loaded explicitly from the provided path. This bypasses the sys.path mechanism, ensuring that the correct file is always imported.stem
property (e.g., config.py becomes config), without manually checking if the string.py
is contained in the name.Here’s the updated implementation, which also ensures backward compatibility:
Additional context
My project is formed with the following structure:
My working environment was formed by:
pyproject.toml
in order to make your library installable and allow Pycharm to automatically resolve all dependencies without the need of manually adding the cbexigen/src to sys.path. It also allow autocompletion when the instructionimport cbexigen
is usedThe pyproject.toml I am using is quite similar to:
I hope something of this information could be useful, thank you in advance!
The text was updated successfully, but these errors were encountered: