Import python modules from sub-directories

Discussion in 'Python' started by Navkirat Singh, Aug 6, 2010.

  1. 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
     
    Navkirat Singh, Aug 6, 2010
    #1
    1. Advertisements

  2. 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
     
    Ralf Schoenian, Aug 6, 2010
    #2
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joel Finkel
    Replies:
    0
    Views:
    634
    Joel Finkel
    Sep 12, 2003
  2. Jeffry van de Vuurst
    Replies:
    2
    Views:
    731
    Jeffry van de Vuurst
    Jul 30, 2003
  3. =?Utf-8?B?TGFzc2UgTmlsc3Nvbg==?=

    Multiple bin-directories with virtual directories?

    =?Utf-8?B?TGFzc2UgTmlsc3Nvbg==?=, Nov 9, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    1,003
    =?Utf-8?B?TGFzc2UgTmlsc3Nvbg==?=
    Nov 9, 2004
  4. Remy Cool
    Replies:
    1
    Views:
    553
    Remy Cool
    Aug 27, 2003
  5. Ben
    Replies:
    2
    Views:
    1,257
  6. Muddy Coder
    Replies:
    0
    Views:
    386
    Muddy Coder
    Mar 6, 2009
  7. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    3,111
    Lawrence D'Oliveiro
    May 20, 2011
  8. Ron Smith
    Replies:
    5
    Views:
    361
    Michele Dondi
    Nov 2, 2004
Loading...