define module in non-standard location?

Shane gshanemiller at verizon.net
Mon Oct 17 00:38:11 EDT 2011


On Oct 16, 11:55 pm, Steven D'Aprano <steve
+comp.lang.pyt... at pearwood.info> wrote:
> On Sun, 16 Oct 2011 19:43:20 -0700, Shane wrote:
> > Normally if one has a code set under a directory "top_level" like this:
>
> > top_level:
> >    __main__.py
> >    a
> >       __init__.py
> >       b
> >          __init__.py
>
> > then this directory structure is naturally satisfies this line in
> > __main__.py:
>
> >>import a.b
>
> > But support, for some stupid reason --- say a.b is user defined code ---
> > that I want
> > to locate modules a and a.b somewhere else under another directory
> > "other_top_level".
>
> You mean like this?
>
> top_level/
>     __main__.py
> other_top_level/
>     a/
>         __init__.py
>         b/
>             __init__.py
>
> > What would the line "import a.b" in __main__,py be replaced by?
>
> Make sure other_top_level is in your PYTHONPATH, and then just use
> "import a.b" as before.
>
> Either use your shell to do something like this:
>
> export PYTHONPATH=other_top_level:$PYTHONPATH
>
> (that's using bash syntax, other shells may need something different), or
> in __main__.py do this:
>
> import sys
> sys.path.append(other_top_level)
>
> --
> Steven

Cheers. Thanks



More information about the Python-list mailing list