[Tutor] functions in other files, executing

Magnus Lycka magnus@thinkware.se
Fri, 18 Oct 2002 12:41:55 +0200


At 23:24 2002-10-17 +1300, Thomi Richards wrote:
>yes, i am aware of the import function. the problem i am trying to get
>around however, is that i have about 20-50 to import, an each one is in
>a different directory. i know where the directories are, but i do not
>want to fill up sys.path with a whole heap of directories...

Aha, that's where the problem lies. Now I see...

I'm curious about the rationale behind using so
many dictionaries and files, but it's probably no
problem. At least if they are reasonably closely
collected. Can you make a package of all these
modules? (Or most of them.)

Have a look at how __init__.py files are made.
There are a few in Python, if you look in the
installation dir.

The tutorial chapter 6.4 describes packages,
but you can make much more advanced things
than you see in that short description.

Does anyone know of a more elaborate description
of how to do magic with __init__.py files? I've
seen some tricks in some files that could be
useful here, but I don't really know where to
find documentation about it.

BTW, it you worry about a big sys.path, I guess
you can remove directories once you have imported
the relevant pieces, but I haven't tested that.

On the other hand, I'm still surpriced that you
have so many modules in so many directories.
Maybe import isn't the right thing here after
all. Can you tell us what it is you are doing?
What's in this multitude of files? Why do they
have to be spread out like that?

Another option would be to open the files as
text, and use restricted exection or exec to
run them instead.


--=20
Magnus Lyck=E5, Thinkware AB
=C4lvans v=E4g 99, SE-907 50 UME=C5
tel: 070-582 80 65, fax: 070-612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se