Import problem

Discussion in 'Python' started by Johny, Mar 6, 2010.

  1. Johny

    Johny Guest

    I have this directory structure

    C:
    \A
    __init__.py
    amodule.py

    \B
    __init__.py
    bmodule.py

    \D
    __init__.py
    dmodule.py

    and I want to import bmodule.py
    C:\>cd \

    C:\>python
    Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
    (Intel)] on win
    32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from A.B import bmodule

    I am bmodule
    >>>

    C:\>

    so far so good. Now I would like to import bmodule but if the current
    directory is \D subdirectory.

    C:> cd \A\B\D
    C:\A\B\D>
    C:\A\B\D>python
    Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
    (Intel)] on win
    32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sys
    >>> sys.path.append('C:\\A')
    >>> from A.B import bmodule

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ImportError: No module named A.B

    C:\>

    so I can not import a module from the parent directory? Or where did I
    make an error?
    Thanks for help

    L.
     
    Johny, Mar 6, 2010
    #1
    1. Advertising

  2. On Sat, 06 Mar 2010 03:53:53 -0800, Johny wrote:

    >>>> import sys
    >>>> sys.path.append('C:\\A')
    >>>> from A.B import bmodule

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    > ImportError: No module named A.B


    The current directory is irrelevant, except that it is automatically
    added to the PYTHONPATH. That's why you can import A.B when the current
    directory is C.

    You are trying to import module B from package A *inside* directory C:\A,
    but there is no such package A inside C:\A. You need to add C to the
    path, and then it should work.




    --
    Steven
     
    Steven D'Aprano, Mar 6, 2010
    #2
    1. Advertising

  3. Johny wrote:
    > I have this directory structure
    >
    > C:
    > \A
    > __init__.py
    > amodule.py
    >
    > \B
    > __init__.py
    > bmodule.py
    >
    > \D
    > __init__.py
    > dmodule.py
    >
    > and I want to import bmodule.py
    > C:\>cd \
    >
    > C:\>python
    > Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
    > (Intel)] on win
    > 32
    > Type "help", "copyright", "credits" or "license" for more information.
    >
    >>>> from A.B import bmodule
    >>>>

    > I am bmodule
    >
    > C:\>
    >
    > so far so good. Now I would like to import bmodule but if the current
    > directory is \D subdirectory.
    >
    > C:> cd \A\B\D
    > C:\A\B\D>
    > C:\A\B\D>python
    > Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
    > (Intel)] on win
    > 32
    > Type "help", "copyright", "credits" or "license" for more information.
    >
    >>>> import sys
    >>>> sys.path.append('C:\\A')
    >>>> from A.B import bmodule
    >>>>

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    > ImportError: No module named A.B
    >
    > C:\>
    >
    > so I can not import a module from the parent directory? Or where did I
    > make an error?
    > Thanks for help
    >
    > L.
    >

    try

    import sys
    sys.path.append('C:\\')
    from A.B import bmodule


    JM
     
    Jean-Michel Pichavant, Mar 8, 2010
    #3
  4. Johny

    News123 Guest

    Hi Steven,

    Steven D'Aprano wrote:
    > On Sat, 06 Mar 2010 03:53:53 -0800, Johny wrote:
    >
    >>>>> import sys
    >>>>> sys.path.append('C:\\A')
    >>>>> from A.B import bmodule

    >> Traceback (most recent call last):
    >> File "<stdin>", line 1, in <module>
    >> ImportError: No module named A.B

    >
    > The current directory is irrelevant, except that it is automatically
    > added to the PYTHONPATH. That's why you can import A.B when the current
    > directory is C.


    Minor currection:

    It doesn't seem to be the current directory, but the directory, where
    the script is located in, which is auto-appended to the pythonpath

    Please see following example:

    $ python -V
    Python 2.6.4

    $ mkdir A

    $ touch A/__init__

    $ # create A/blla.py an A/blo.py

    $ cat A/bla.py
    print "I am bla"
    import A.blo
    print "and I found blo",dir(A.blo)
    $ cat A/blo.py
    avar = 3
    print "I am blo"

    $ python A/bla.py
    I am bla
    Traceback (most recent call last):
    File "A/bla.py", line 2, in <module>
    import A.blo
    ImportError: No module named A.blo



    However:
    $ cat alternative_bla.py
    import sys
    sys.path.append(".")
    print "I am bla"
    import A.blo
    print "and I found blo",dir(A.blo)

    $ python A/alternativ_bla.py
    I am bla
    I am blo
    and I found blo ['__builtins__', '__doc__', '__file__', '__name__',
    '__package__', 'avar']


    bye N
     
    News123, Mar 8, 2010
    #4
  5. Johny

    News123 Guest

    Jean-Michel Pichavant wrote:
    > Johny wrote:
    >> I have this directory structure
    >>
    >> C:
    >> \A
    >> __init__.py
    >> amodule.py
    >>
    >> \B
    >> __init__.py
    >> bmodule.py
    >>
    >> \D
    >> __init__.py
    >> dmodule.py
    >>
    >> and I want to import bmodule.py
    >> C:\>cd \
    >>
    >> C:\>python
    >> Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
    >> (Intel)] on win
    >> 32
    >> Type "help", "copyright", "credits" or "license" for more information.
    >>
    >>>>> from A.B import bmodule
    >>>>>

    >> I am bmodule
    >> C:\>
    >>
    >> so far so good. Now I would like to import bmodule but if the current
    >> directory is \D subdirectory.
    >>
    >> C:> cd \A\B\D
    >> C:\A\B\D>
    >> C:\A\B\D>python
    >> Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
    >> (Intel)] on win
    >> 32
    >> Type "help", "copyright", "credits" or "license" for more information.
    >>
    >>>>> import sys
    >>>>> sys.path.append('C:\\A')
    >>>>> from A.B import bmodule
    >>>>>

    >> Traceback (most recent call last):
    >> File "<stdin>", line 1, in <module>
    >> ImportError: No module named A.B
    >>
    >> C:\>
    >>
    >> so I can not import a module from the parent directory? Or where did I
    >> make an error?
    >> Thanks for help
    >>
    >> L.
    >>

    > try
    >
    > import sys
    > sys.path.append('C:\\')
    > from A.B import bmodule
    >

    is there any 'automatic' way of finding the top level
    directory?basically the 'top level directory is the first directory
    going upwards, that doesn't contain a __init__.py file.

    of course you could do this 'manually' by
    doing:

    # assume, that this module is A.amodule
    import sys
    import os

    # I'd love to have a similiar automatic construct
    if __name__ == "__main__":
    level = 1 # or function locating how far to go up before
    # finding a dir, whcih does not contain a __init__.py
    mydir = os.path.split(__file__)[0]
    topdir = os.path.join( mydir,*(("..",)*level))
    abstop = os.path.abspath(topdir)
    sys.path.append(abstop)

    ## now you can import with the normal module paths

    import A.blo
    print "and I found blo",dir(A.blo)


    bye N
     
    News123, Mar 8, 2010
    #5
  6. Johny

    News123 Guest

    Jean-Michel Pichavant wrote:
    > Johny wrote:
    >> I have this directory structure
    >>
    >> C:
    >> \A
    >> __init__.py
    >> amodule.py
    >>
    >> \B
    >> __init__.py
    >> bmodule.py
    >>
    >> \D
    >> __init__.py
    >> dmodule.py
    >>
    >> and I want to import bmodule.py
    >> C:\>cd \
    >>
    >> C:\>python
    >> Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
    >> (Intel)] on win
    >> 32
    >> Type "help", "copyright", "credits" or "license" for more information.
    >>
    >>>>> from A.B import bmodule
    >>>>>

    >> I am bmodule
    >> C:\>
    >>
    >> so far so good. Now I would like to import bmodule but if the current
    >> directory is \D subdirectory.
    >>
    >> C:> cd \A\B\D
    >> C:\A\B\D>
    >> C:\A\B\D>python
    >> Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
    >> (Intel)] on win
    >> 32
    >> Type "help", "copyright", "credits" or "license" for more information.
    >>
    >>>>> import sys
    >>>>> sys.path.append('C:\\A')
    >>>>> from A.B import bmodule
    >>>>>

    >> Traceback (most recent call last):
    >> File "<stdin>", line 1, in <module>
    >> ImportError: No module named A.B
    >>
    >> C:\>
    >>
    >> so I can not import a module from the parent directory? Or where did I
    >> make an error?
    >> Thanks for help
    >>
    >> L.
    >>

    > try
    >
    > import sys
    > sys.path.append('C:\\')
    > from A.B import bmodule
    >

    is there any 'automatic' way of finding the top level
    directory?basically the 'top level directory is the first directory
    going upwards, that doesn't contain a __init__.py file.

    of course you could do this 'manually' by
    doing:

    # assume, that this module is A.amodule
    import sys
    import os

    # I'd love to have a similiar automatic construct
    if __name__ == "__main__":
    level = 1 # or function locating how far to go up before
    # finding a dir, whcih does not contain a __init__.py
    mydir = os.path.split(__file__)[0]
    topdir = os.path.join( mydir,*(("..",)*level))
    abstop = os.path.abspath(topdir)
    sys.path.append(abstop)

    ## now you can import with the normal module paths

    import A.blo
    print "and I found blo",dir(A.blo)


    bye N
     
    News123, Mar 8, 2010
    #6
  7. News123 wrote:
    > Jean-Michel Pichavant wrote:
    >
    >> Johny wrote:
    >>
    >>> I have this directory structure
    >>>
    >>> C:
    >>> \A
    >>> __init__.py
    >>> amodule.py
    >>>
    >>> \B
    >>> __init__.py
    >>> bmodule.py
    >>>
    >>> \D
    >>> __init__.py
    >>> dmodule.py
    >>>
    >>> and I want to import bmodule.py
    >>> C:\>cd \
    >>>
    >>> C:\>python
    >>> Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
    >>> (Intel)] on win
    >>> 32
    >>> Type "help", "copyright", "credits" or "license" for more information.
    >>>
    >>>
    >>>>>> from A.B import bmodule
    >>>>>>
    >>>>>>
    >>> I am bmodule
    >>> C:\>
    >>>
    >>> so far so good. Now I would like to import bmodule but if the current
    >>> directory is \D subdirectory.
    >>>
    >>> C:> cd \A\B\D
    >>> C:\A\B\D>
    >>> C:\A\B\D>python
    >>> Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
    >>> (Intel)] on win
    >>> 32
    >>> Type "help", "copyright", "credits" or "license" for more information.
    >>>
    >>>
    >>>>>> import sys
    >>>>>> sys.path.append('C:\\A')
    >>>>>> from A.B import bmodule
    >>>>>>
    >>>>>>
    >>> Traceback (most recent call last):
    >>> File "<stdin>", line 1, in <module>
    >>> ImportError: No module named A.B
    >>>
    >>> C:\>
    >>>
    >>> so I can not import a module from the parent directory? Or where did I
    >>> make an error?
    >>> Thanks for help
    >>>
    >>> L.
    >>>
    >>>

    >> try
    >>
    >> import sys
    >> sys.path.append('C:\\')
    >> from A.B import bmodule
    >>
    >>

    > is there any 'automatic' way of finding the top level
    > directory?basically the 'top level directory is the first directory
    > going upwards, that doesn't contain a __init__.py file.
    >

    what if some user has an __init__.py file the top level directory of
    your package ?
    > of course you could do this 'manually' by
    > doing:
    >
    > # assume, that this module is A.amodule
    > import sys
    > import os
    >
    > # I'd love to have a similiar automatic construct
    > if __name__ == "__main__":
    > level = 1 # or function locating how far to go up before
    > # finding a dir, whcih does not contain a __init__.py
    > mydir = os.path.split(__file__)[0]
    > topdir = os.path.join( mydir,*(("..",)*level))
    > abstop = os.path.abspath(topdir)
    > sys.path.append(abstop)
    >
    > ## now you can import with the normal module paths
    >
    > import A.blo
    > print "and I found blo",dir(A.blo)
    >
    >
    > bye N
    >
    >
    >
    >

    You don't want to do that and you don't need it neither. That's what the
    env variable PYTHONPATH is for. set it correctly, install your package
    inside and everything works just fine (+standard). With a linux OS it
    easy to create smb links to point to any working directory. It should be
    possible on windows as well.

    If your package is meant to be destributed, you may use setup.py

    JM
     
    Jean-Michel Pichavant, Mar 10, 2010
    #7
  8. Johny

    News123 Guest

    Hi JM,

    Jean-Michel Pichavant wrote:
    > News123 wrote:
    >> Jean-Michel Pichavant wrote:
    >>
    >>> Johny wrote:
    >>>
    >>>> I have this directory structure
    >>>>
    >>>> C:
    >>>> \A
    >>>> __init__.py
    >>>> amodule.py
    >>>>
    >>>> \B
    >>>> __init__.py
    >>>> bmodule.py
    >>>>
    >>>> \D
    >>>> __init__.py
    >>>> dmodule.py
    >>>>
    >>>> and I want to import bmodule.py
    >>>> C:\>cd \
    >>>>
    >>>> C:\>python
    >>>> Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
    >>>> (Intel)] on win
    >>>> 32
    >>>> Type "help", "copyright", "credits" or "license" for more information.
    >>>>
    >>>>
    >>>>>>> from A.B import bmodule
    >>>>>>>
    >>>> I am bmodule
    >>>> C:\>
    >>>>
    >>>> so far so good. Now I would like to import bmodule but if the current
    >>>> directory is \D subdirectory.
    >>>>
    >>>> C:> cd \A\B\D
    >>>> C:\A\B\D>
    >>>> C:\A\B\D>python
    >>>> Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
    >>>> (Intel)] on win
    >>>> 32
    >>>> Type "help", "copyright", "credits" or "license" for more information.
    >>>>
    >>>>
    >>>>>>> import sys
    >>>>>>> sys.path.append('C:\\A')
    >>>>>>> from A.B import bmodule
    >>>>>>>
    >>>> Traceback (most recent call last):
    >>>> File "<stdin>", line 1, in <module>
    >>>> ImportError: No module named A.B
    >>>>
    >>>> C:\>
    >>>>
    >>>> so I can not import a module from the parent directory? Or where did I
    >>>> make an error?
    >>>> Thanks for help
    >>>>
    >>>> L.
    >>>>
    >>> try
    >>>
    >>> import sys
    >>> sys.path.append('C:\\')
    >>> from A.B import bmodule
    >>>
    >>>

    >> is there any 'automatic' way of finding the top level
    >> directory?basically the 'top level directory is the first directory
    >> going upwards, that doesn't contain a __init__.py file.
    >>

    > what if some user has an __init__.py file the top level directory of
    > your package ?


    Is there any other usage of __init.py__ than indicating a module directory?
    I wasn't aware of it, but you're right I did not investigte in depth and
    users can of course do whatever they like.



    >> of course you could do this 'manually' by
    >> doing:
    >>
    >> # assume, that this module is A.amodule
    >> import sys
    >> import os
    >>
    >> # I'd love to have a similiar automatic construct
    >> if __name__ == "__main__":
    >> level = 1 # or function locating how far to go up before
    >> # finding a dir, whcih does not contain a __init__.py
    >> mydir = os.path.split(__file__)[0]
    >> topdir = os.path.join( mydir,*(("..",)*level))
    >> abstop = os.path.abspath(topdir)
    >> sys.path.append(abstop)
    >>
    >> ## now you can import with the normal module paths
    >>
    >> import A.blo
    >> print "and I found blo",dir(A.blo)
    >>
    >>

    > You don't want to do that and you don't need it neither. That's what the
    > env variable PYTHONPATH is for. set it correctly, install your package
    > inside and everything works just fine (+standard). With a linux OS it
    > easy to create smb links to point to any working directory. It should be
    > possible on windows as well.

    I like your idea with the symlinks.
    However not sure how to do it with windows.
    I assume default shortcuts won't do.
    >
    > If your package is meant to be destributed, you may use setup.py
    >
    >

    Well,

    It's nice if a user just unpacks a zip file and can click on any script
    with the .py suffix in the tree.

    (Its nice for example for tutorials / demos )

    It's also nice if he can later on just delete the unpacked directory and
    there will be no trace left in the registry or in the python base dir.

    This is why I'm interested in solutions without setup.py or changing
    environment variables.

    bye


    N
     
    News123, Mar 10, 2010
    #8
  9. Johny

    News123 Guest

    Hi JM,

    Jean-Michel Pichavant wrote:
    > News123 wrote:
    >> Jean-Michel Pichavant wrote:
    >>
    >>> Johny wrote:
    >>>
    >>>> I have this directory structure
    >>>>
    >>>> C:
    >>>> \A
    >>>> __init__.py
    >>>> amodule.py
    >>>>
    >>>> \B
    >>>> __init__.py
    >>>> bmodule.py
    >>>>
    >>>> \D
    >>>> __init__.py
    >>>> dmodule.py
    >>>>
    >>>> and I want to import bmodule.py
    >>>> C:\>cd \
    >>>>
    >>>> C:\>python
    >>>> Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
    >>>> (Intel)] on win
    >>>> 32
    >>>> Type "help", "copyright", "credits" or "license" for more information.
    >>>>
    >>>>
    >>>>>>> from A.B import bmodule
    >>>>>>>
    >>>> I am bmodule
    >>>> C:\>
    >>>>
    >>>> so far so good. Now I would like to import bmodule but if the current
    >>>> directory is \D subdirectory.
    >>>>
    >>>> C:> cd \A\B\D
    >>>> C:\A\B\D>
    >>>> C:\A\B\D>python
    >>>> Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
    >>>> (Intel)] on win
    >>>> 32
    >>>> Type "help", "copyright", "credits" or "license" for more information.
    >>>>
    >>>>
    >>>>>>> import sys
    >>>>>>> sys.path.append('C:\\A')
    >>>>>>> from A.B import bmodule
    >>>>>>>
    >>>> Traceback (most recent call last):
    >>>> File "<stdin>", line 1, in <module>
    >>>> ImportError: No module named A.B
    >>>>
    >>>> C:\>
    >>>>
    >>>> so I can not import a module from the parent directory? Or where did I
    >>>> make an error?
    >>>> Thanks for help
    >>>>
    >>>> L.
    >>>>
    >>> try
    >>>
    >>> import sys
    >>> sys.path.append('C:\\')
    >>> from A.B import bmodule
    >>>
    >>>

    >> is there any 'automatic' way of finding the top level
    >> directory?basically the 'top level directory is the first directory
    >> going upwards, that doesn't contain a __init__.py file.
    >>

    > what if some user has an __init__.py file the top level directory of
    > your package ?


    Is there any other usage of __init.py__ than indicating a module directory?
    I wasn't aware of it, but you're right I did not investigte in depth and
    users can of course do whatever they like.



    >> of course you could do this 'manually' by
    >> doing:
    >>
    >> # assume, that this module is A.amodule
    >> import sys
    >> import os
    >>
    >> # I'd love to have a similiar automatic construct
    >> if __name__ == "__main__":
    >> level = 1 # or function locating how far to go up before
    >> # finding a dir, whcih does not contain a __init__.py
    >> mydir = os.path.split(__file__)[0]
    >> topdir = os.path.join( mydir,*(("..",)*level))
    >> abstop = os.path.abspath(topdir)
    >> sys.path.append(abstop)
    >>
    >> ## now you can import with the normal module paths
    >>
    >> import A.blo
    >> print "and I found blo",dir(A.blo)
    >>
    >>

    > You don't want to do that and you don't need it neither. That's what the
    > env variable PYTHONPATH is for. set it correctly, install your package
    > inside and everything works just fine (+standard). With a linux OS it
    > easy to create smb links to point to any working directory. It should be
    > possible on windows as well.

    I like your idea with the symlinks.
    However not sure how to do it with windows.
    I assume default shortcuts won't do.
    >
    > If your package is meant to be destributed, you may use setup.py
    >
    >

    Well,

    It's nice if a user just unpacks a zip file and can click on any script
    with the .py suffix in the tree.

    (Its nice for example for tutorials / demos )

    It's also nice if he can later on just delete the unpacked directory and
    there will be no trace left in the registry or in the python base dir.

    This is why I'm interested in solutions without setup.py or changing
    environment variables.

    bye


    N
     
    News123, Mar 10, 2010
    #9
    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. Paul Opal
    Replies:
    12
    Views:
    1,009
    Paul Opal
    Oct 11, 2004
  2. JPractitioner
    Replies:
    13
    Views:
    20,240
    Roedy Green
    Feb 24, 2006
  3. Vitali Gontsharuk
    Replies:
    2
    Views:
    605
    Vitali Gontsharuk
    Aug 25, 2005
  4. Stefan Seefeld
    Replies:
    3
    Views:
    1,001
  5. per9000
    Replies:
    7
    Views:
    428
    Magnus Lycka
    Feb 27, 2006
Loading...

Share This Page