473,414 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

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 1641
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Markus Doering | last post by:
Hello, I just switched from 2.2 to Python 2.3. I am developing an XML/CGI interface to databases as a python package called "unitWrapper" containing several modules which ran fine under v2.2. ...
16
by: Manlio Perillo | last post by:
Hi. I'm a new user of Python but I have noted a little problem. Python is a very good language but it is evolving, in particular its library is evolving. This can be a problem when, ad example,...
4
by: Steve Holden | last post by:
I'm trying to load module code from a database, which stores for each module its full name, code, load date and a Boolean indicating whether it's a package or not. The following simple program:...
5
by: Steve Holden | last post by:
This is even stranger: it makes it if I import the module a second time: import dbimp as dbimp import sys if __name__ == "__main__": dbimp.install() #k = sys.modules.keys() #k.sort() #for...
0
by: Anders J. Munch | last post by:
Now 2.5 is out, and we have a syntax for explicit relative imports (PEP 328, http://www.python.org/dev/peps/pep-0328/, in case anyone wasn't paying attention). The long-term plan is that the...
7
by: Ron Adam | last post by:
from __future__ import absolute_import Is there a way to check if this is working? I get the same results with or without it. Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) on win 32 ...
49
by: Martin Unsal | last post by:
I'm using Python for what is becoming a sizeable project and I'm already running into problems organizing code and importing packages. I feel like the Python package system, in particular the...
12
by: Alan Isaac | last post by:
Are relative imports broken in 2.5? Directory ``temp`` contains:: __init__.py test1.py test2.py File contents: __init__.py and test2.py are empty test1.py contains a single line::
13
by: Rafe | last post by:
Hi, I am in a situation where I feel I am being forced to abandon a clean module structure in favor of a large single module. If anyone can save my sanity here I would be forever grateful. My...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.