howto add a sub-directory to the searchpath / namespace ?

Larry Bates larry.bates at websafe.com
Wed Oct 10 17:05:50 EDT 2007


stef mientki wrote:
> hello,
> 
> my program has become a bit large,
> and now I want to split the files over several subdirectories.
> So in the example shown below, I just moved the files f1.py and f2.py to 
> a deeper subdirectory.
> 
> basedirectory\
>    mainfile.py
>    file1.py
>    file2.py
>    subdir1\
>       __init__.py
>       f1.py
>       f2.py
> 
> Now I don't want (even can't) change my program,
> to change imports from
>    from f1 import something
> into
>    from subdir1.f1 import something
> simply because f1.py and f2.py are python files dropped by users
> and I do not know on forehand what will be dropped.
> 
> I looked into the description of __init__.py,
> in the hope I could make f1.py and f2.py available as if they were in 
> the basedirectory,
> but i couldn't find a way.
> 
> Is there a way to make f1.py and f2.py available as if they were located 
> in the base directory,
> without knowing their names (so in general all py-files in the subdir1)  ??
> 
> thanks,
> Stef Mientki
> 
> 
Put basedirectory\subdir in the PYTHONPATH environment variable or

os.path.append(r'basedirectory\subdir1')

in the body of your program.

-Larry



More information about the Python-list mailing list