Import python modules from sub-directories

N

Navkirat Singh

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
 
R

Ralf Schoenian

Navkirat said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top