Module import via sys.path and py2exe

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

    #1

    Module import via sys.path and py2exe

    Hi,

    i have a program with is built like this:
    startup.py
    dir1/__init__.py
    dir1/file1.py
    dir2/__init__.py
    dir2/file2.py
    dir3/__init__.py
    dir3/file3.py

    The program works but now i want to add the functionality into an
    existing program 2. This is program 2:

    program2.py
    program2dir1/__init__.py
    program2dir1/file4.py

    When i copy the directories over i get this situation:

    program2.py
    program2dir1/__init__.py
    program2dir1/file4.py
    program1/__init__.py
    program1/dir1/__init__.py
    program1/dir1/file1.py
    program1/dir2/__init__.py
    program1/dir2/file2.py
    program1/dir3/__init__.py
    program1/dir3/file3.py

    However, i need to specify this in program2.py or i can't use the files 1-3:
    import sys
    sys.path.append ('program1')
    import program1.dir1.f ile1 as f1

    Then i can use the functionality of the first program in the second.

    Now for my questions:

    1. Is this the correct way or is there an easier/more logical sollution.
    Now all __init__.py file are empty

    2. As said this works but when i make a standalone of program2 it
    doesn't find any of the program1 files:
    Traceback (most recent call last):
    File "start.py", line 20, in ?
    File "windows\window main.pyo", line 59, in ?
    File "program1\__ini t__.pyo", line 2, in ?
    File "program1\dir1\ __init__.pyo", line 1, in ?
    File "program1\dir1\ file1.pyo", line 28, in ?
    ImportError: No module named dir2.file2

    dir2.file2 is imported in file1.py as import dir2.file2.
    So something in my setup script is wrong. How can i make sure all file
    of program1 are also added?

    Excerpt of setup.py
    ....
    py_modules=['start'],
    packages=['database','dia logs','export', 'graphics','loc alutils','windo ws'],
    ....
    If i specify program1 as module or as package or program1.dir1 and so on
    doesn't seem to help.

    Any ideas?

    Thanks
    Benedict
  • flupke

    #2
    Re: Module import via sys.path and py2exe

    flupke wrote:[color=blue]
    > Hi,
    >
    > i have a program with is built like this:
    > startup.py
    > dir1/__init__.py
    > dir1/file1.py
    > dir2/__init__.py
    > dir2/file2.py
    > dir3/__init__.py
    > dir3/file3.py
    >
    > The program works but now i want to add the functionality into an
    > existing program 2. This is program 2:
    >
    > program2.py
    > program2dir1/__init__.py
    > program2dir1/file4.py
    >
    > When i copy the directories over i get this situation:
    >
    > program2.py
    > program2dir1/__init__.py
    > program2dir1/file4.py
    > program1/__init__.py
    > program1/dir1/__init__.py
    > program1/dir1/file1.py
    > program1/dir2/__init__.py
    > program1/dir2/file2.py
    > program1/dir3/__init__.py
    > program1/dir3/file3.py
    >
    > However, i need to specify this in program2.py or i can't use the files
    > 1-3:
    > import sys
    > sys.path.append ('program1')
    > import program1.dir1.f ile1 as f1
    >
    > Then i can use the functionality of the first program in the second.
    >
    > Now for my questions:
    >
    > 1. Is this the correct way or is there an easier/more logical sollution.
    > Now all __init__.py file are empty
    >
    > 2. As said this works but when i make a standalone of program2 it
    > doesn't find any of the program1 files:
    > Traceback (most recent call last):
    > File "start.py", line 20, in ?
    > File "windows\window main.pyo", line 59, in ?
    > File "program1\__ini t__.pyo", line 2, in ?
    > File "program1\dir1\ __init__.pyo", line 1, in ?
    > File "program1\dir1\ file1.pyo", line 28, in ?
    > ImportError: No module named dir2.file2
    >
    > dir2.file2 is imported in file1.py as import dir2.file2.
    > So something in my setup script is wrong. How can i make sure all file
    > of program1 are also added?
    >
    > Excerpt of setup.py
    > ...
    > py_modules=['start'],
    > packages=['database','dia logs','export', 'graphics','loc alutils','windo ws'],
    > ...
    > If i specify program1 as module or as package or program1.dir1 and so on
    > doesn't seem to help.
    >
    > Any ideas?
    >
    > Thanks
    > Benedict[/color]

    I forgot to add that if i check the shared.lib py2exe creates, the
    program1 dir and subdirs/files are there so they are included in the
    package.

    Benedict

    Comment

    • flupke

      #3
      Re: Module import via sys.path and py2exe

      flupke wrote:
      <snip>
      [color=blue]
      > I forgot to add that if i check the shared.lib py2exe creates, the
      > program1 dir and subdirs/files are there so they are included in the
      > package.
      >
      > Benedict[/color]

      Hi,

      i solved it by adding the same import statement to the py2exe setup.py
      script as in my code:

      import sys
      sys.path.append ('program1')

      I'm still not sure though that the way i'm adding the module in the
      first place is correct but it seems to work okay.

      Regards,
      Benedict

      Comment

      Working...