How to simulate packages?

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Thu Aug 21 16:22:58 EDT 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
>     -mod1_file.py
>   -module2
>     -mod2_file.py
> 
> Everytime I run mod2_file.py I need to import mod1_file.py.  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