I have three directory attributes:
dirdata = {
'somewhere': '0',
'foo': '1',
'flub': '2'
}
FILE = "/mnt/somewhere" + dirdata['somewhere'] + \
"/foo" + dirdata['foo'] \
"/flub" + dirdata['flub'] \
"/TARGET"
RMDATA = __import__(FILE)
will get me the error:
ImportError: No module named /mnt/somewhere0/foo1/flub2/TARGET
however when I do this from the cmdline:
>>> RMDATA = __import__("/mnt/somewhere0/foo1/flub2/TARGET")
everything works.
The file is called TARGET.py
I think that I don't understand how to build up a proper string to pass on to
__import__()
thanks for any help.
cheers,
=derek
Monster By Mistake Inc > 'digital plumber'
http://www.interlog.com/~drek
Assuming PYTHONPATH contains /mnt
import and __import__ take the format: somewhere0.foo1.flub2.TARGET
Although if you want to execute a script, you can use:
execfile("/mnt/somewhere0/foo1/flub2/TARGET")
--------------------------------------------
Adrian Eyre <mailto:a.e...@optichrome.com>
Optichrome Computer Solutions Ltd
Maybury Road, Woking, Surrey, GU21 5HX, UK
Tel: +44 1483 740 233 Fax: +44 1483 760 644
http://www.optichrome.com
--------------------------------------------
|Date: Tue, 14 Dec 1999 10:18:22 -0000
|From: Adrian Eyre <a.e...@optichrome.com>
|To: Agent Drek <dr...@MonsterByMistake.Com>, pytho...@python.org
|Subject: RE: building string for __import__()
Hmmm...
my problem was that the variable that I built up with:
dirdata = {
'somewhere': '0',
'foo': '1',
'flub': '2'
}
FILE = "/mnt/somewhere" + dirdata['somewhere'] + \
"/foo" + dirdata['foo'] \
"/flub" + dirdata['flub'] \
"/TARGET"
would give me the import error but just typing it manually into the
interpreter would work fine. I'll try the . notation.
>>> c = import__("/typing/in/the/path/by/hand/did/it/TARGET")
execfile(FILE) works well enough however. __import__ just didn't like
my Object FILE.
I'm happy enough with execfile and was just curious about what I was
doing wrong with __import__
thanks,
If it works by hand, try somthing like:
dirdata = {
'somewhere': '0',
'foo': '1',
'flub': '2'
}
filename = "/mnt/somewhere" + dirdata['somewhere'] + \
"/foo" + dirdata['foo'] \
"/flub" + dirdata['flub'] \
"/TARGET"
expected_filename = "/typing/in/the/path/by/hand/did/it/TARGET"
if filename != expected_filename:
raise "Pah, I've screwed up somewhere"
c = __import__(filename)
Using the import notation will also have different behaviour depending
on the current working directory when you run the script. Python adds
. to the PYTHONPATH IIRC.
> I'm happy enough with execfile and was just curious about what I was
> doing wrong with __import__
Be aware that execfile is slightly different in that is runs the
script in the __main__ namespace, rather than importing it.
First of all I want to thank all those (currently over 50) Python users
who registered on my Python Users Counter. Today I added a proper
counter page (its displayed after you register at
http://www.wszechnica.safenet.pl/cgi-bin/pythonuserscounter.py ) and a
check-without-incrementing link and page (located at
http://www.wszechnica.safenet.pl/cgi-bin/checkpythonuserscounter.py ).
Enjoy!
Jacek Artymiak
------------------------------------------------------------------
Register at the Python Users Counter!
http://www.wszechnica.safenet.pl/cgi-bin/pythonuserscounter.py
------------------------------------------------------------------
Autor/Dziennikarz/Konsultant - Author/Journalist/Consultant
arty...@safenet.pl, http://www.wszechnica.safenet.pl
co-author: StarOffice for Linux Bible (IDG Books Worldwide, Inc.)
http://www.amazon.com/exec.obidos/ASIN/0764533630/polskawszechnica
------------------------------------------------------------------