Why canNOT import from a local directory ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jia Lu

    Why canNOT import from a local directory ?

    Hi all

    I created a folder named *lib* and put a py file *lib.py* in it.
    In the upper folder I created a py file as:

    <CODE>
    import lib.lib

    def main():
    """
    __doc__
    """
    lib.lib.test()


    # ////////////////////////////////////////
    if __name__ == "__main__":
    main()


    But I got an error :
    #.:python main.py
    Traceback (most recent call last):
    File "main.py", line 6, in ?
    import lib.lib
    ImportError: No module named lib.lib

    Why ?

  • Thin Myrna

    #2
    Re: Why canNOT import from a local directory ?

    Jia Lu wrote:
    Hi all
    >
    I created a folder named *lib* and put a py file *lib.py* in it.
    In the upper folder I created a py file as:
    >
    <CODE>
    import lib.lib
    >
    def main():
    """
    __doc__
    """
    lib.lib.test()
    >
    >
    # ////////////////////////////////////////
    if __name__ == "__main__":
    main()
    >
    >
    But I got an error :
    #.:python main.py
    Traceback (most recent call last):
    File "main.py", line 6, in ?
    import lib.lib
    ImportError: No module named lib.lib
    >
    Why ?
    You need to define a file __init__.py in your newly created lib directory.

    HTH
    Thin

    Comment

    • Jia Lu

      #3
      Re: Why canNOT import from a local directory ?

      >
      You need to define a file __init__.py in your newly created lib directory.
      >
      Thank you very much :)


      Comment

      Working...