path to modules per import statement

Fuzzyman fuzzyman at gmail.com
Fri Mar 24 10:42:15 EST 2006


AndyL wrote:
> danmcleran at yahoo.com wrote:
> >>For instance: "import my.path.module" would load module from
> >>./my/path/module.py?
> >
> >
> > Yeah, just do that. I don't understand the question, it works just like
> > this today.
> >
>
> I work on rather big set of Python applications: something like 100 .py
> files divided into libs and separate sub-applications.
>
> For now I keep almost everything in one directory but I wish following
> structure to be in place:
>
> 	app1/  app2/  lib1/  lib2/  lib3/
>

There are several approaches.

One is to add each of these directories to your sys.path :

    sys.path.extend(map(os.path.abspath, ['app1/', 'app2/', 'lib1/',
'lib2/', 'lib3/']))

This is a perfectly normal thing to do - so I wouldn't be shy of it.

Another is to add an empty file called ``__init__.py`` to each of these
directories. This makes each directory a 'package'. You can then do :

import app1.module

My module `pathutils
<http://www.voidspace.org.uk/python/pathutils.html>`_ contains a third
approach that *does* allow you to specify the location of your module.
It's a less suitable approach in your case however.

HTH

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml

>
> and be able to import from each app[12] all the libs. I do not want to
> touch existing code to prefix all the import places with lib[123] nether
> I want to play with sys.path.append too much.
> 
> 
> A.




More information about the Python-list mailing list