[issue25372] load_module() does not link submodule to parent package

2015-10-14 Thread R. David Murray

R. David Murray added the comment:

Well, once you've loaded the module and added it to the namespace of the parent 
package, you've imported it.  Since import_module is available in 2.7, it 
sounds like that satisfies your use case.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue25372] load_module() does not link submodule to parent package

2015-10-13 Thread Martin Panter

Martin Panter added the comment:

My use case is for Readline auto-completion, to list submodules of a package 
given by the user, without importing anything unnecessary. The original code is 
compatible with Python 2, but I am also writing a patch for 3.6 that wouldn't 
need that. The original implementation is like this pseudocode:

def list_submodules(package):
# Ensure it is a package before loading or importing it
for name in parent_packages:
loader = pkgutil.find_loader(name)
assert loader.is_package()
# Could call importlib.import_module(), but this seemed easier because I 
already have the loader:
package = loader.load_module(name)
# The only reason I want to load the module:
search_path = package.__path__
return pkgutil.iter_modules(search_path)

Thanks for your feedback Brett. I have changed over to 
importlib.import_module(), and will accept that this is just a quirk of the low 
level import stuff.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue25372] load_module() does not link submodule to parent package

2015-10-11 Thread Brett Cannon

Brett Cannon added the comment:

1) I would use 
https://docs.python.org/3/library/importlib.html#importlib.find_loader instead 
of pkgutil

2) there are a bunch of ways to get a hold of a loader and its hard to document 
how to use a loader properly since it's low level like __path__ while being 
crucial for stuff like reloading and loaders predate __spec__

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue25372] load_module() does not link submodule to parent package

2015-10-11 Thread R. David Murray

R. David Murray added the comment:

Brett: Martin is calling pkgutil.find_loader, which is not marked as 
deprecated, and then calling load_module on the returned loader, which 
apparently does *almost* what he wants but not quite.

Martin: maybe you should explain your use case in more detail.  Probably there 
is a better way to accomplish it using importlib directly.  Unless one of your 
issues is needing to write 2/3 compatible code...

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue25372] load_module() does not link submodule to parent package

2015-10-11 Thread Brett Cannon

Brett Cannon added the comment:

This is entirely on purpose as you're not meant to call load_module() if you 
want to import code programmatically; that's what importlib.import_module() is 
for. The load_module() method -- which is kind of deprecated thanks to 
exec_module() -- is there purely to initialize the module being imported, with 
everything else the responsibility of other parts of import. You should only be 
calling load_module/execx_module tonload a specific set of bytes and to bypass 
the import machinery entirely, in which case you have to manage any "extras" 
you want, like setting a submodule on a parent package.

--
resolution:  -> not a bug
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue25372] load_module() does not link submodule to parent package

2015-10-10 Thread Arfrever Frehtes Taifersar Arahesis

Changes by Arfrever Frehtes Taifersar Arahesis :


--
nosy: +Arfrever

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue25372] load_module() does not link submodule to parent package

2015-10-10 Thread Martin Panter

Martin Panter added the comment:

I don’t think it is a regression. But it is annoying, because my code that 
works fine for top-level modules and packages happens to screw up the import 
system for deeper-level module names.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue25372] load_module() does not link submodule to parent package

2015-10-10 Thread Serhiy Storchaka

Changes by Serhiy Storchaka :


--
nosy: +brett.cannon, eric.snow, ncoghlan

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue25372] load_module() does not link submodule to parent package

2015-10-10 Thread R. David Murray

R. David Murray added the comment:

I'm no expert on pep 302 either, but I'm guessing this is an intentional part 
of the design.  You'll note that updating the namespace is *not* mentioned as 
one of the things load_module has to do.  The update of the namespace is 
something the import system as a whole implements, and I'm not sure where it 
happens (probably __import__/import_module).

Unless you are saying that this is a regression, it doesn't really matter if it 
was intentional or not, since if it is not a regression then it's been that way 
for a long time.

--
nosy: +r.david.murray

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue25372] load_module() does not link submodule to parent package

2015-10-10 Thread Pathangi Jatinshravan

Pathangi Jatinshravan added the comment:

Hi, if this is not too critical of a bug, can I be assigned to this issue?

--
nosy: +Pathangi Jatinshravan

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue25372] load_module() does not link submodule to parent package

2015-10-10 Thread Martin Panter

New submission from Martin Panter:

The load_module() API adds an entry to sys.modules when loading a submodule, 
but does not add the submodule as an attribute in the parent package. I am no 
expert on PEP 302 or the import system, but this feels like a bug to me:

>>> import sys, pkgutil
>>> loader = pkgutil.find_loader("xml.etree")
>>> loader.load_module("xml.etree")

>>> sys.modules["xml.etree"]  # Entry added, per documentation and PEP 302

>>> hasattr(sys.modules["xml"], "etree")  # But not linked to parent!
False
>>> import xml.etree
>>> xml.etree  # Broken :(
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'module' object has no attribute 'etree'

--
messages: 252753
nosy: martin.panter
priority: normal
severity: normal
status: open
title: load_module() does not link submodule to parent package
type: behavior
versions: Python 2.7, Python 3.4, Python 3.5, Python 3.6

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com