Import python modules from sub-directories

Ralf Schoenian ralf at schoenian-online.de
Fri Aug 6 08:05:33 CEST 2010


Navkirat Singh wrote:
> Hi guys,
> 
> I am new to python and would like to import certain classes in sub-directories of the working directory. I was wondering how will I be able to achieve this?
> 
> Regards,
> Nav

Hi,

put an empty file with the name __init__.py in your subdirectory. I call 
it sub1 here. Assuming your filename is importme.py and your classname 
is Test you can do the following imports:

from sub1.importme import Test
import sub1.importme

You can find further explainations in the official tutorial 
http://docs.python.org/tutorial/modules.html

Regards,
Ralf



More information about the Python-list mailing list