cx_freeze and importing modules

独自空忆成欢 提交于 2019-12-23 10:16:35

问题


I want to compile a Python 3.3 module with submodules using cx_freeze.

So, my dir strucuture is:

projectname/
     __init__.py
     submodule1/
          __init__.py
          bootstrap.py
          script1.py
          submodule11/
                   script2.py
     submodule2/
          ...

In the __init__.py I import

from submodule1 import bootstrap

and from the bootstrap

import submodule1.submodule11.script2

If I run the init file, anything is good and the script with the submodule imports is executed correctly.

When I compile it, I use this setup.py:

from cx_Freeze import setup,Executable
import sys

includes = []
excludes = ['Tkinter']
packages = ['submodule1', 'submodule2']
base = "Win32GUI"
setup(
    name = 'myapp',version = '0.1',description = 'app',author = 'user',
    options = {'build_exe': {'excludes':excludes,'packages':packages}}, 
    executables = [Executable('__init__.py',base=base)]
)

and run the script in the 'projectname' dir.

But if I start the application I get ImportError: no module named 'submodule1.submodule11' from an error dialog.

And it's true: in the library.zip this submodule doens't exist.

What do I do wrong?


回答1:


So, I found the answer: every module MUST have a __init__.py file in it's folder so python would know that it's a package, not just some folder. Now I compile my project.



来源:https://stackoverflow.com/questions/25938252/cx-freeze-and-importing-modules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!