importing module as filename

D

david farning

I am having a problem with name space contention.
I am combining two existing programs both with their own config
modules.

I have been expermenting with
import config #grab first config
sys.path.insert(0,"/usr/share/yum/")
import config as yconfig #grab second config


The first config always seems to squash the second config.
There should be a yconfig.yumconf but there is not.

any suggestions?

Dave Farning
 
P

Peter Otten

david said:
I am having a problem with name space contention.
I am combining two existing programs both with their own config
modules.

I have been expermenting with
import config #grab first config
sys.path.insert(0,"/usr/share/yum/")
import config as yconfig #grab second config


The first config always seems to squash the second config.
There should be a yconfig.yumconf but there is not.

any suggestions?

How about a symlink to disambiguate the module names,
e. g. ln -s config.py yumconfig.py, and then

import config, yumconfig

Somewhat more ambitious: convert the program directory into a package by
adding an __init__.py script and then

import config, yum.config

Peter
 
D

david farning

Peter said:
How about a symlink to disambiguate the module names,
e. g. ln -s config.py yumconfig.py, and then

import config, yumconfig

Thanks, great suggestions. I will talk to the upstream maintainers about
1. changing config to something a little less ambigous ;)
2. having them add the symlink
3 me adding the symlink (I wouldn't want to muck around their sandbox too
much)

Somewhat more ambitious: convert the program directory into a package by
adding an __init__.py script and then

import config, yum.config

Peter
Dave Farning
 
A

Andrew Dalke

david farning:
import config #grab first config
sys.path.insert(0,"/usr/share/yum/")
import config as yconfig #grab second config
The first config always seems to squash the second config.
There should be a yconfig.yumconf but there is not.

The first 'config' is stored in sys.modules['config']. The
second import finds 'config' in the modules and uses that,
rather than reloading it.

Try this

import sys
import config #grab first config
del sys.modules["config"]
sys.path.insert(0,"/usr/share/yum/")
import config as yconfig #grab second config

In addition, you can try

config = __import__("config")
sys.path.insert(0,"/usr/share/yum/")
import config as yconfig #grab second config

Andrew
(e-mail address removed)
 
P

Peter Hansen

david said:
I am having a problem with name space contention.
I am combining two existing programs both with their own config
modules.

I have been expermenting with
import config #grab first config
sys.path.insert(0,"/usr/share/yum/")
import config as yconfig #grab second config

The first config always seems to squash the second config.
There should be a yconfig.yumconf but there is not.

Working from the names alone, you might find it easier
to simply execfile() the scripts. If they do nothing more
than define a bunch of constants or something like that, this
should be sufficient to avoid the problem you've seen.

Otherwise the "del sys.modules[config]" approach would seem
the most expedient, if ugly.

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top