importing a package

Discussion in 'Python' started by flupke, Jun 22, 2005.

  1. flupke

    flupke Guest

    Hi,

    I developed a package with a structure like this
    src/
    tesfile.py
    dir1/
    __init__.py
    file1.py
    dir2/
    __init__.py
    file2.py

    The testfile is meant to try the code out that is specified in file1.py
    and file2.py

    Now i have another project where i want to use the code from that package.
    The structure of that project:

    src/
    file3.py
    dir3/
    __init__.py
    file4.py

    To use it, i copied the package in the root of the project:
    src/
    file3.py
    dir3/
    __init__.py
    file4.py
    package/
    __init__.py
    dir1/
    __init__.py
    file1.py
    dir2/
    __init__.py
    file2.py

    In my code (file3.py) i then do an "import package.dir1.file1 as myobj"
    and access a class like this:
    myobj.LMyClass()

    (where myobj and LMyClass are decent names. Used these as example)

    That works but i get an error in the package files.
    I then get an error in package/dir1/file1.py on an import statement
    specified in that file1.py that says import dir2.file2

    How come this works as standalone project and not when i try to use it
    as in situation 2 it doesn't seem to find module file2.py ?
    How can i solve that?

    Thanks
    Benedict
    flupke, Jun 22, 2005
    #1
    1. Advertising

  2. flupke

    Damjan Guest

    > I developed a package with a structure like this
    > src/
    > tesfile.py
    > dir1/
    > __init__.py
    > file1.py
    > dir2/
    > __init__.py
    > file2.py


    Importing dir2/file2 from dir1/file1.py works here, because when yuo started
    the testfile script the src/ directory was added to the sys.path list.

    If you relocate dir1/ and dir2/ in a "package" directory here it will not
    work.


    --
    damjan
    Damjan, Jun 22, 2005
    #2
    1. Advertising

  3. flupke

    flupke Guest

    Damjan wrote:
    >>I developed a package with a structure like this
    >>src/
    >> tesfile.py
    >> dir1/
    >> __init__.py
    >> file1.py
    >> dir2/
    >> __init__.py
    >> file2.py

    >
    >
    > Importing dir2/file2 from dir1/file1.py works here, because when yuo started
    > the testfile script the src/ directory was added to the sys.path list.
    >
    > If you relocate dir1/ and dir2/ in a "package" directory here it will not
    > work.
    >
    >


    Indeed, when i do this, then it works
    import sys
    sys.path.append('package')

    However, why is it that package isn't added automatically to the pad?

    Regards,
    Benedict
    flupke, Jun 22, 2005
    #3
  4. flupke

    Damjan Guest

    > Indeed, when i do this, then it works
    > import sys
    > sys.path.append('package')
    >
    > However, why is it that package isn't added automatically to the pad?


    When you execute a python program the directory where the program is is
    automatically added to sys.path. No other directory is added to the default
    builtin sys.path.

    In you case (the second case), you can import package.dir2.file2.


    --
    damjan
    Damjan, Jun 22, 2005
    #4
  5. Damjan wrote:
    >>Indeed, when i do this, then it works
    >>import sys
    >>sys.path.append('package')
    >>
    >>However, why is it that package isn't added automatically to the pad?

    >
    >
    > When you execute a python program the directory where the program is is
    > automatically added to sys.path. No other directory is added to the default
    > builtin sys.path.
    >
    > In you case (the second case), you can import package.dir2.file2.


    OK, thanks for the info

    Regards,
    Benedict

    --
    Benedict Verheyen Debian User
    http://www.heimdallit.be Public Key 0x712CBB8D
    Benedict Verheyen, Jun 23, 2005
    #5
    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. mhk
    Replies:
    1
    Views:
    605
    Chris Smith
    Nov 28, 2003
  2. Alex
    Replies:
    0
    Views:
    497
  3. Javabean

    importing for package

    Javabean, Mar 1, 2006, in forum: Java
    Replies:
    4
    Views:
    512
  4. Dave
    Replies:
    2
    Views:
    457
  5. plb
    Replies:
    2
    Views:
    339
Loading...

Share This Page