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. Advertising

  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. Advertising

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:
    493
    Joel Finkel
    Sep 12, 2003
  2. Ben
    Replies:
    2
    Views:
    905
  3. ImpalerCore
    Replies:
    0
    Views:
    843
    ImpalerCore
    Mar 10, 2011
  4. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

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

Share This Page