Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

building string for __import__()

8 views
Skip to first unread message

Agent Drek

unread,
Dec 13, 1999, 3:00:00 AM12/13/99
to pytho...@python.org
I have searched deja, python.org/* and cannot find an answer
to my problem. I'm sure I'm just missing something very simple...

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

Adrian Eyre

unread,
Dec 14, 1999, 3:00:00 AM12/14/99
to Agent Drek, pytho...@python.org
> ImportError: No module named /mnt/somewhere0/foo1/flub2/TARGET

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
--------------------------------------------

Agent Drek

unread,
Dec 14, 1999, 3:00:00 AM12/14/99
to pytho...@python.org
On Tue, 14 Dec 1999, Adrian Eyre wrote:

|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,

Adrian Eyre

unread,
Dec 17, 1999, 3:00:00 AM12/17/99
to Agent Drek, pytho...@python.org
> my problem was that the variable that I built up with:
>
> [snip]

>
> would give me the import error but just typing it manually into the
> interpreter would work fine. I'll try the . notation.

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.

Jacek Artymiak

unread,
Dec 20, 1999, 3:00:00 AM12/20/99
to
Hi Everybody!

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
------------------------------------------------------------------


0 new messages