How to simulate packages?

Bruno Desthuilliers bdesth.quelquechose at
Thu Aug 21 22:22:58 CEST 2008

Daniel a écrit :
> Hello,
> I have a project that I've decided to split into packages in order to
> organize my code better.  So what I have looks something like this
> src
>   -module1
>   -module2
> Everytime I run I need to import  Right now
> I'm using an ugly little thing to do my import (see below).  Isn't
> there some python mechanism to handle packages?

cf Gabriel's answer wrt/ Python's packages.

> import os, sys
> # add packages to path then finish importing
> for dir in ['module1','module2']:
>     # how about this ugly thing to find adjacent directories for
> imports
>     sys.path.append('\\'.join(os.path.dirname(__file__).split('\\')
> [:len(os.path.dirname(__file__).split('\\'))-1]) + "\\" + dir)
> Any and all suggestions appreciated.  Thanks in advance.

A suggestion: os.path is about os independance when it comes to 
filesystem. Please read the whole doc for this module, and learn how to 
avoid writing system dependant code.

More information about the Python-list mailing list