Correctness: sys.path.append("..")

Paul Boddie paul at boddie.net
Thu Jan 10 06:26:50 EST 2002


Jeremy Bowers <clp at jerf.org> wrote in message news:<GX9%7.12340$fG.80917 at rwcrnsc51.ops.asp.att.net>...
> The upshot of this thread seems to be that if try to create a 
> directory structure like
> 
> MainProject/
>     UI/
>     DataStructures/
>     FileLoaders/
> 
> such that files in the UI directory can import files in the DataStructures
> directory, the only "correct" way to do this is to make sure that the parent
> of MainProject is on the PYTHONPATH.

That's largely the conclusion to be made, yes.

> Mucking with sys.path still seems somehow wrong, but is a superior solution
> IMHO to enforcing the idea that my program must be installed in a rather
> limited set of directories, or the user has to muck with an environment
> variable (when many users have no idea such a thing exists).

Well, tools like distutils provide support for installation in the
standard 'site-packages' directory (and probably other standard
locations, too), and such locations are always on 'sys.path'. You
could probably wrap up any distutils installer invocations with nicer
programs if necessary, but see below for a final piece of advice.

> I'd complain, but the language is up to 2.2 and it doesn't seem to bother
> anyone else.  :-)
> 
> Despite the wrongness, I'm going with the os.absolute_path("..") solution.
> Both projects are pure Python and my goal is to get installation directions
> down to
> 
> * Install Python if necessary
> * Uncompress my program's archive file
> * Double-click/invoke in an OS-appropriate manner MainProg.py
> 
> Thanks for the help, everyone.

Before you go and implement this widely, there's another thing which I
mentioned: your package root can reside in the same directory as your
main program - as a result, the package should always be found when
the main program tries to import things from it, at least as far as I
can tell.

Paul



More information about the Python-list mailing list