New submission from Andrey <[email protected]>:
```
import os, sys
def import_module(dir_path, module_name, ref_module_name = None):
module_file_path = os.path.join(dir_path, module_name).replace('\\', '/')
if sys.version_info[0] > 3 or sys.version_info[0] == 3 and
sys.version_info[1] >= 4:
import importlib
import_spec =
importlib.util.spec_from_file_location(os.path.splitext(module_name)[0] if
ref_module_name is None else ref_module_name, os.path.join(dir_path,
module_name).replace('\\', '/'))
import_module = importlib.util.module_from_spec(import_spec)
import_spec.loader.exec_module(import_module)
#globals()[module_name if ref_module_name is None else ref_module_name] =
import_module # still does not accept modules with periods without this
else:
# back compatability
import imp
module_file, module_file_name, module_desc =
imp.find_module(os.path.splitext(module_name)[0],
[os.path.dirname(module_file_path)])
globals()[module_name if ref_module_name is None else ref_module_name] =
imp.load_module(module_file_path, module_file, module_file_name, module_desc)
```
I am trying to import my modules targeted by file path:
```
import_module(MYTOOLS_ROOT, 'cmdoplib.std.py', 'cmdoplib')
import_module(MYTOOLS_ROOT, 'cmdoplib.yaml.py', 'cmdoplib_yaml')
```
And got error:
```
Traceback (most recent call last):
File "c:\python\x64\37\lib\site-packages\xonsh\proc.py", line 1411, in run
r = self.f(self.args, sp_stdin, sp_stdout, sp_stderr, spec, spec.stack)
File "c:\python\x64\37\lib\site-packages\xonsh\proc.py", line 1204, in
proxy_two
return f(args, stdin)
File "c:\python\x64\37\lib\site-packages\xonsh\aliases.py", line 575, in
source_alias
builtins.execx(src, "exec", ctx, filename=fpath)
File "c:\python\x64\37\lib\site-packages\xonsh\built_ins.py", line 1497, in
__call__
return self.obj.__call__(*args, **kwargs)
File "c:\python\x64\37\lib\site-packages\xonsh\execer.py", line 190, in exec
return exec(code, glbs, locs)
File
"w:/Work/MyProjects/__scm_solutions/all-in-one/_common/tools/cmdoplib.yaml.xsh",
line 11, in <module>
g_yaml_env = cmdoplib_yaml.YamlEnv()
NameError: name 'cmdoplib_yaml' is not defined
```
If try to uncomment this:
```
globals()[module_name if ref_module_name is None else ref_module_name] =
import_module
```
All works fine.
Seems the latest version of the python still can not handle modules with the
periods in a file name without constructions from the old and well known `imp`
module.
It seems for me as a bug or at least as an incomplete (re)implementation.
----------
components: Interpreter Core
messages: 350950
nosy: andry
priority: normal
severity: normal
status: open
title: importlib can not handle module file names with periods
type: enhancement
versions: Python 3.7
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue38000>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com