accesing modules in sibling packages

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • noama
    New Member
    • Aug 2007
    • 9

    accesing modules in sibling packages

    Hi,
    I've create a file hierarchy:
    test/
    __init__.py
    one/
    __init__.py
    one.py
    two/
    __init__.py
    two.py


    all the __init__.py files are empty.
    the code in one.py is
    Code:
    import test.two
    print two.v
    the code in two.py is
    Code:
    v=0

    but when i run one.py i get an error:

    Traceback (most recent call last):
    File "C:\Python25\My Scripts\Test\1\ one.py", line 1, in <module>
    import Test.two
    ImportError: No module named Test.two
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by noama
    Hi,
    I've create a file hierarchy:
    test/
    __init__.py
    one/
    __init__.py
    one.py
    two/
    __init__.py
    two.py


    all the __init__.py files are empty.
    the code in one.py is
    Code:
    import test.two
    print two.v
    the code in two.py is
    Code:
    v=0

    but when i run one.py i get an error:

    Traceback (most recent call last):
    File "C:\Python25\My Scripts\Test\1\ one.py", line 1, in <module>
    import Test.two
    ImportError: No module named Test.two
    Try this:[code=Python]import test.two.two[/code]

    Comment

    • noama
      New Member
      • Aug 2007
      • 9

      #3
      Originally posted by bvdet
      Try this:[code=Python]import test.two.two[/code]
      Nope. still the same error.

      Comment

      • elcron
        New Member
        • Sep 2007
        • 43

        #4
        try this:
        [CODE=Python]
        import sys; sys.path.insert (0, "..")
        import two.two
        print two.two.v
        [/CODE]
        Though in the test/two/__init__.py I would add:
        [CODE=Python]
        from two import *
        [/CODE]
        and change the code in one.py to:
        [CODE=Python]
        import sys; sys.path.insert (0, "..")
        import two
        print two.v
        [/CODE]

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by elcron
          try this:
          [CODE=Python]
          import sys; sys.path.insert (0, "..")
          import two.two
          print two.two.v
          [/CODE]
          Though in the test/two/__init__.py I would add:
          [CODE=Python]
          from two import *
          [/CODE]
          and change the code in one.py to:
          [CODE=Python]
          import sys; sys.path.insert (0, "..")
          import two
          print two.v
          [/CODE]
          Nice trick, my friend!

          Comment

          Working...