import relative (with a directory)

Chris Rebert clp2 at rebertia.com
Sat Jan 10 21:27:38 EST 2009


On Sat, Jan 10, 2009 at 6:11 PM, rocky <rocky.bernstein at gmail.com> wrote:
> Import relative?
>
> Recently for fun I've been working on a large Python program. It has
> many files/modules spread over several directories/submodules.
>
> Each module has some "demo" code at the end that I can use to run or
> experiment with that module. Of course, modules often refer to others;
> depending on which one, more of the code may get pulled in or not.
>
> In this situation it has been very convenient to have do "import
> relative" which is like import, but I get to specify where to start
> searching from. Of course I can set sys.path, but I find that hokey.
> (When I write systems administration scripts, I also generally prefer
> specifying a full path name to a program, e.g. /bin/mount rather than
> using PATH. Strikes me as the same issue here).
>
> The other thing about the Python import mechanism that sometimes gets
> in the way is that if there is a module imported (from the wrong place
> for this context), it tacitly gets used. Within such a project I want
> to specify when I want the code in this project, and if it can't find
> that, then that's a failure.
>
> I'm curious if other have the same problem and how other they deal
> with this? Reading "Expert Python Programming", I see that one
> solution is to install into a sandbox. But it strikes me as a little
> less agile. Just as I don't enjoy issuing compile and link commands
> (even if run via a Makefile), I don't want to have to issue install-
> into-sandbox commands.
>
> For concreteness here's the import code I've been using is:
>    http://code.google.com/p/pyimport-relative/
>
> It is far from perfect because I just wanted to get this done and I
> don't fully understand the Python modules and imp module, but for now
> it gets what I want done. I will probably add some ability to top-
> level package name to avoid collisions from other packages. However if
> I do this, I still will probably have the program figure out the
> intermediate compound names. So if I am in directory ...my-project/a/b/
> c and I can say import_relative("x", "myproject") and have it import
> that as "myproject.a.b.c.x". That way, if files move around and get
> renamed I don't have to change any code. Also, I can probably store
> somewhere in a variable "myproject".

You should probably check out the relative import syntax introduced in
PEP 328: http://www.python.org/dev/peps/pep-0328/
It should be able to do exactly what you want.

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com



More information about the Python-list mailing list