469,649 Members | 1,516 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,649 developers. It's quick & easy.

import module from package - confusing syntax

I'm setting up a large hierarchy of module packages and using a
variable to select which of many alternative packages to import. For
example, the variable 'config.modsel' points to a particular "model
selector" package. 'config' is a module containing many such
variables.

My first attempt resulted in a confusing error message:
from config.modsel.modelselector import ModelSelector
Traceback (most recent call last):
File "<pyshell#25>", line 1, in -toplevel-
from config.modsel.modelselector import ModelSelector
ImportError: No module named modsel.modelselector

It works if I enter the literal path rather than a variable:
config.modsel <module 'libs.modsel01' ... > from libs.modsel01.modelselector import ModelSelector
ModelSelector <class libs.modsel01.modelselector.ModelSelector at 0x009F68D0>

I need to use a variable, however. The best I can come up with is:
exec('from '+ config.modsel.__name__ +'.modelselector import ModelSelector')


Is this the best we can do with current Python syntax?

Should we think about suggesting a better syntax? I think the
fundamental problem is the confusion that arises from using '.' as
both a path separator and an attribute qualifier.

-- Dave

Jul 18 '05 #1
2 1537
On Sat, 2004-07-03 at 08:21, David MacQuigg wrote:
I'm setting up a large hierarchy of module packages and using a
variable to select which of many alternative packages to import.


This means you need __import__.

// m
Jul 18 '05 #2
On Sat, 03 Jul 2004 08:54:50 -0500, Mark McEahern
<ma*******@mceahern.com> wrote:
On Sat, 2004-07-03 at 08:21, David MacQuigg wrote:
I'm setting up a large hierarchy of module packages and using a
variable to select which of many alternative packages to import.


This means you need __import__.


__import__ looks even messier than exec() We have to interpolate the
string 'config.modsel' twice.

import libs, config
config.modsel = 'modsel01'

## # What we want:
## from (config.modsel).modelselector import ModelSelector

## # Works, but messy:
## __import__('libs.%s.modelselector' % config.modsel)
## modsel = getattr(libs, config.modsel)
## ModelSelector = modsel.modelselector.ModelSelector

# Still seems like the lesser evil:
exec('from libs.%s.modelselector import ModelSelector' %
config.modsel)

The __import__ function looks like a real kludge. There is a
suggestion in section 2.1 of the Library Reference to use the 'imp'
module, and write my own import function. I guess I'll look at that
next.

-- Dave

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Markus Doering | last post: by
16 posts views Thread by Manlio Perillo | last post: by
4 posts views Thread by Steve Holden | last post: by
5 posts views Thread by Steve Holden | last post: by
reply views Thread by Anders J. Munch | last post: by
7 posts views Thread by Ron Adam | last post: by
49 posts views Thread by Martin Unsal | last post: by
12 posts views Thread by Alan Isaac | last post: by
13 posts views Thread by Rafe | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.