This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: importlib can not handle module file names with periods
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: andry, brett.cannon
Priority: normal Keywords:

Created on 2019-09-01 12:27 by andry, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg350950 - (view) Author: Andrey (andry) Date: 2019-09-01 12:27
```
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.
msg351515 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2019-09-09 16:10
This works as expected as nothing is inherently injected into your global namespace in any of the other code that you are executing. Otherwise the fact that the imported module is not being put into sys.modules also doesn't help if you're trying to get access via `import cmdoplib_yaml`. 

But once the module object is created then the file name does not matter.
History
Date User Action Args
2022-04-11 14:59:19adminsetgithub: 82181
2019-09-09 16:10:20brett.cannonsetstatus: open -> closed
resolution: not a bug
messages: + msg351515

stage: resolved
2019-09-01 12:52:17xtreaksetnosy: + brett.cannon
2019-09-01 12:27:54andrycreate