Python cx冻结不';无法找到所有依赖项

Python cx冻结不';无法找到所有依赖项,python,cx-freeze,Python,Cx Freeze,我有一个python脚本(2.7),顶部有一些“有趣的”导入。我最初希望使用py2exe将其编译成一个exe,以便更容易地分发() 我已经放弃了,正在尝试使用cx freeze。但是,我也有问题。问题似乎是我添加到Python中的库(jinja2和restkit)。我在python目录中看到它们。/Lib/site-packages/Jinja2-2.6-py2.7.egg/Jinja2和这里的。/Lib/site-packages/restkit-4.2.1-py2.7.egg/restkit

我有一个python脚本(2.7),顶部有一些“有趣的”导入。我最初希望使用py2exe将其编译成一个exe,以便更容易地分发()

我已经放弃了,正在尝试使用cx freeze。但是,我也有问题。问题似乎是我添加到Python中的库(jinja2和restkit)。我在python目录中看到它们。/Lib/site-packages/Jinja2-2.6-py2.7.egg/Jinja2和这里的。/Lib/site-packages/restkit-4.2.1-py2.7.egg/restkit

以下是我的脚本中的导入:

import datetime
from jinja2 import Environment, PackageLoader
from optparse import OptionParser
from datetime import date, timedelta
from restkit import Resource, BasicAuth, request
我将setup.py与cx freeze一起使用。以下是setup.py:

from cx_Freeze import setup, Executable


packages  = ["restkit", "jinja2" , "restkit.client" ]
includes = []
includefiles = []
eggsacutibull = Executable(
    script = "myScript.py",
    initScript = None,
    targetName = "myScript.exe",
    compress = True,
    copyDependentFiles = True,
    appendScriptToExe = False,
    appendScriptToLibrary = False,
    icon = None
    )

setup(
        name = "myScript",
        version = "0.1",
        author = 'vickery',
        description = "MyScript description",
        options = {"build_exe": {"includes":includes, "include_files": includefiles, "packages": packages}},
        executables = [eggsacutibull]
        )
packages = [ "restkit", "jinja2" , "restkit.client", "restkit.conn", "socketpool" ]
我像这样运行cxfreeze:

cxfreeze myScript.py --target-dir exe
python setup.py build
我在我的构建中得到了这个:

Missing modules:
? __pypy__ imported from jinja2.debug
? http_parser.http imported from restkit.client
? jinja2._debugsupport imported from jinja2.debug
? jinja2._markupsafe._speedups imported from jinja2._markupsafe
? jinja2.debugrenderer imported from jinja2.debug
? markupsafe imported from jinja2.utils
? pretty imported from jinja2.utils
? socketpool imported from restkit.conn
当我尝试运行exe时,我得到以下结果:

Traceback (most recent call last):
  File "c:\Python27\lib\site-packages\restkit-4.2.1-py2.7.egg\restkit\__init__.py", line 9, in <module>
    from restkit.conn import Connection
  File "c:\Python27\lib\site-packages\restkit-4.2.1-py2.7.egg\restkit\conn.py", line 14, in <module>
    from socketpool import Connector
ImportError: No module named socketpool
Traceback (most recent call last):
  File "c:\Python27\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27, in <module>
    exec code in m.__dict__
  File "myScript.py", line 12, in <module>
ImportError: cannot import name Resource
我还将socketpool添加到我的setup.py中:

from cx_Freeze import setup, Executable


packages  = ["restkit", "jinja2" , "restkit.client" ]
includes = []
includefiles = []
eggsacutibull = Executable(
    script = "myScript.py",
    initScript = None,
    targetName = "myScript.exe",
    compress = True,
    copyDependentFiles = True,
    appendScriptToExe = False,
    appendScriptToLibrary = False,
    icon = None
    )

setup(
        name = "myScript",
        version = "0.1",
        author = 'vickery',
        description = "MyScript description",
        options = {"build_exe": {"includes":includes, "include_files": includefiles, "packages": packages}},
        executables = [eggsacutibull]
        )
packages = [ "restkit", "jinja2" , "restkit.client", "restkit.conn", "socketpool" ]
但是,当我现在尝试构建时,会出现一个构建错误:

$ python setup.py build
running build
running build_exe
Traceback (most recent call last):
  File "setup.py", line 32, in <module>
    executables = [eggsacutibull]
  File "c:\python27\lib\site-packages\cx_Freeze\dist.py", line 365, in setup
    distutils.core.setup(**attrs)
  File "c:\python27\lib\distutils\core.py", line 152, in setup
    dist.run_commands()
  File "c:\python27\lib\distutils\dist.py", line 953, in run_commands
    self.run_command(cmd)
  File "c:\python27\lib\distutils\dist.py", line 972, in run_command
    cmd_obj.run()
  File "c:\python27\lib\distutils\command\build.py", line 127, in run
    self.run_command(cmd_name)
  File "c:\python27\lib\distutils\cmd.py", line 326, in run_command
    self.distribution.run_command(command)
  File "c:\python27\lib\distutils\dist.py", line 972, in run_command
    cmd_obj.run()
  File "c:\python27\lib\site-packages\cx_Freeze\dist.py", line 235, in run
    freezer.Freeze()
  File "c:\python27\lib\site-packages\cx_Freeze\freezer.py", line 570, in Freeze
    self.finder = self._GetModuleFinder()
  File "c:\python27\lib\site-packages\cx_Freeze\freezer.py", line 325, in _GetModuleFinder
    finder.IncludePackage(name)
  File "c:\python27\lib\site-packages\cx_Freeze\finder.py", line 534, in IncludePackage
    module = self._ImportModule(name, deferredImports)
  File "c:\python27\lib\site-packages\cx_Freeze\finder.py", line 274, in _ImportModule
    raise ImportError("No module named %r" % name)
ImportError: No module named 'socketpool'
python如何解析与cxfreeze不同的模块

Edit2: 从python中,我可以做到这一点:

>>> import socketpool
>>> print socketpool.__file__
c:\python27\lib\site-packages\socketpool-0.5.2-py2.7.egg\socketpool\__init__.pyc
那是寻找包裹的非标准场所吗?我可以用PYTHONPATH强迫cxfreez在那里寻找socketpool吗


谢谢

如果有人遇到这种情况,问题似乎是cxfreeze不能很好地处理鸡蛋。一旦我从egg中提取了代码(将其重命名为zip并解压缩),并为代码创建了Python27\Lib\site packages\socketpool,然后运行了构建,一切似乎都很好


看起来像个黑客。淋浴时间到了。

我知道这条线很旧,但我只是花了4天时间才弄明白,这可能对其他人有帮助,所以下面是:

cx\ U冻结中包含\ U文件选项的说明具有误导性。如果您想为build_exe使用include_files选项,下面是一个示例

include_files=[
           (r"C:\Python27\Scripts\mk2ifcoremd.dll",               "mk2ifcoremd.dll"),
           (r"C:\Python27\Scripts\mk2ifportmd.dll",               "mk2ifportmd.dll"),
           (r"C:\Python27\Scripts\mk2imalloc.dll",                "mk2imalloc.dll"),
           (r"C:\Python27\Scripts\mk2iomp5md.dll",                "mk2iomp5md.dll"),
           (r"C:\Python27\Scripts\mk2mmd.dll",                    "mk2mmd.dll"),]
必须有一个元组列表,其中源的路径为绝对路径,目标的路径为文件名。 我遇到了cx_freeze的问题,因为它没有/没有将所有必需的DLL复制到dist目录中。因此,我的exe在我的机器上测试正常,但在另一台没有安装Python的机器上无法工作。为了调试它,我不得不反复地将Python安装移到一边(将C:\Python27重命名为C:\u Python27),并查看是否缺少库

下面是一个适用于我的示例setup.py文件:

# invoke using:
#  python setup.py build

from cx_Freeze import setup, Executable

import sys
import glob
import os
import zlib
import shutil

# Remove the existing folders folder
shutil.rmtree("build", ignore_errors=True)
shutil.rmtree("dist", ignore_errors=True)

########################################
# Here is a list of the Executable options
########################################

#"script":               #the name of the file containing the script which is to be frozen
#"initScript":           #the name of the initialization script that will be executed before the actual script is executed; this script is used to set up the environment for the executable; if a name is given without an absolute path the names of files in the initscripts subdirectory of the cx_Freeze package is searched
#"base":                 #the name of the base executable; if a name is given without an absolute path the names of files in the bases subdirectory of the cx_Freeze package is searched
#"path":                 #list of paths to search for modules
#"targetDir":            #the directory in which to place the target executable and any dependent files
#"targetName":           #the name of the target executable; the default value is the name of the script with the extension exchanged with the extension for the base executable
#"includes":             #list of names of modules to include
#"excludes":             #list of names of modules to exclude
#"packages":             #list of names of packages to include, including all of the package's submodules
#"replacePaths":         #Modify filenames attached to code objects, which appear in tracebacks. Pass a list of 2-tuples containing paths to search for and corresponding replacement values. A search for '*' will match the directory containing the entire package, leaving just the relative path to the module.
#"compress":             #boolean value indicating if the module bytecode should be compressed or not
#"copyDependentFiles":   #boolean value indicating if dependent files should be copied to the target directory or not
#"appendScriptToExe":    #boolean value indicating if the script module should be appended to the executable itself
#"appendScriptToLibrary":#boolean value indicating if the script module should be appended to the shared library zipfile
#"icon":                 #name of icon which should be included in the executable itself on Windows or placed in the target directory for other platforms
#"namespacePackages":    #list of packages to be treated as namespace packages (path is extended using pkgutil)
#"shortcutName":         #the name to give a shortcut for the executable when included in an MSI package
#"shortcutDir":          #the directory in which to place the shortcut when being installed by an MSI package; see the MSI Shortcut table documentation for more information on what values can be placed here.
MY_TARGET_EXE = Executable(
    # what to build
    script = "main.py",
    initScript = None,
    base = 'Win32GUI',
    targetDir = r"dist",
    targetName = "MyProgram.exe",
    compress = True,
    copyDependentFiles = True,
    appendScriptToExe = False,
    appendScriptToLibrary = False,
    icon = None
    )

########################################
#Here is a list of the build_exe options
########################################
#1) append the script module to the executable
append_script_to_exe=False
#2) the name of the base executable to use which, if given as a relative path, will be joined with the bases subdirectory of the cx_Freeze installation; the default value is "Console"
base="Console"
#3) list of names of files to exclude when determining dependencies of binary files that would normally be included; note that version numbers that normally follow the shared object extension are stripped prior to performing the comparison
bin_excludes=[]
#4) list of names of files to include when determining dependencies of binary files that would normally be excluded; note that version numbers that normally follow the shared object extension are stripped prior to performing the comparison
bin_includes=[] 
#5) list of paths from which to exclude files when determining dependencies of binary files
bin_path_excludes=[]
#6) list of paths from which to include files when determining dependencies of binary files
bin_path_includes=[]
#7) directory for built executables and dependent files, defaults to build/
build_exe="dist/"
#8) create a compressed zip file
compressed=False
#9) comma separated list of constant values to include in the constants module called BUILD_CONSTANTS in form <name>=<value>
constants=[]
#10) copy all dependent files
copy_dependent_files=True
#11) create a shared zip file called library.zip which will contain all modules shared by all executables which are built 
create_shared_zip=True
#12) comma separated list of names of modules to exclude
excludes = ['_gtkagg', '_tkagg', 'bsddb', 'curses', 'email', 'pywin.debugger',
            'pywin.debugger.dbgcon', 'pywin.dialogs', 'tcl',
            'Tkconstants', 'Tkinter']
#13) include the icon in the frozen executables on the Windows platform and alongside the frozen executable on other platforms
icon=False
#13) comma separated list of names of modules to include
includes = ['sip', 'matplotlib.backends.backend_wxagg']
#15) list containing files to be copied to the target directory; 
#  it is expected that this list will contain strings or 2-tuples for the source and destination; 
#  the source can be a file or a directory (in which case the tree is copied except for .svn and CVS directories); 
#  the target must not be an absolute path
#
# NOTE: INCLUDE FILES MUST BE OF THIS FORM OTHERWISE freezer.py line 128 WILL TRY AND DELETE dist/. AND FAIL!!!
# Here is a list of ALL the DLLs that are included in Python27\Scripts 
include_files=[
           (r"C:\Python27\Scripts\mk2ifcoremd.dll",               "mk2ifcoremd.dll"),
           (r"C:\Python27\Scripts\mk2ifportmd.dll",               "mk2ifportmd.dll"),
           (r"C:\Python27\Scripts\mk2imalloc.dll",                "mk2imalloc.dll"),
           (r"C:\Python27\Scripts\mk2iomp5md.dll",                "mk2iomp5md.dll"),
           (r"C:\Python27\Scripts\mk2mmd.dll",                    "mk2mmd.dll"),
           (r"C:\Python27\Scripts\mk2_avx.dll",                   "mk2_avx.dll"),
           (r"C:\Python27\Scripts\mk2_blacs_ilp64.dll",           "mk2_blacs_ilp64.dll"),
           (r"C:\Python27\Scripts\mk2_blacs_intelmpi_ilp64.dll",  "mk2_blacs_intelmpi_ilp64.dll"),
           (r"C:\Python27\Scripts\mk2_blacs_intelmpi_lp64.dll",   "mk2_blacs_intelmpi_lp64.dll"),
           (r"C:\Python27\Scripts\mk2_blacs_lp64.dll",            "mk2_blacs_lp64.dll"),
           (r"C:\Python27\Scripts\mk2_blacs_mpich2_ilp64.dll",    "mk2_blacs_mpich2_ilp64.dll"),
           (r"C:\Python27\Scripts\mk2_blacs_mpich2_lp64.dll",     "mk2_blacs_mpich2_lp64.dll"),
           (r"C:\Python27\Scripts\mk2_blacs_msmpi_ilp64.dll",     "mk2_blacs_msmpi_ilp64.dll"),
           (r"C:\Python27\Scripts\mk2_blacs_msmpi_lp64.dll",      "mk2_blacs_msmpi_lp64.dll"),
           (r"C:\Python27\Scripts\mk2_cdft_core.dll",             "mk2_cdft_core.dll"),
           (r"C:\Python27\Scripts\mk2_core.dll",                  "mk2_core.dll"),
           (r"C:\Python27\Scripts\mk2_def.dll",                   "mk2_def.dll"),
           (r"C:\Python27\Scripts\mk2_intel_thread.dll",          "mk2_intel_thread.dll"),
           (r"C:\Python27\Scripts\mk2_mc.dll",                    "mk2_mc.dll"),
           (r"C:\Python27\Scripts\mk2_mc3.dll",                   "mk2_mc3.dll"),
           (r"C:\Python27\Scripts\mk2_p4n.dll",                   "mk2_p4n.dll"),
           (r"C:\Python27\Scripts\mk2_pgi_thread.dll",            "mk2_pgi_thread.dll"),
           (r"C:\Python27\Scripts\mk2_rt.dll",                    "mk2_rt.dll"),
           (r"C:\Python27\Scripts\mk2_scalapack_ilp64.dll",       "mk2_scalapack_ilp64.dll"),
           (r"C:\Python27\Scripts\mk2_scalapack_lp64.dll",        "mk2_scalapack_lp64.dll"),
           (r"C:\Python27\Scripts\mk2_sequential.dll",            "mk2_sequential.dll"),
           (r"C:\Python27\Scripts\mk2_vml_avx.dll",               "mk2_vml_avx.dll"),
           (r"C:\Python27\Scripts\mk2_vml_def.dll",               "mk2_vml_def.dll"),
           (r"C:\Python27\Scripts\mk2_vml_mc.dll",                "mk2_vml_mc.dll"),
           (r"C:\Python27\Scripts\mk2_vml_mc2.dll",               "mk2_vml_mc2.dll"),
           (r"C:\Python27\Scripts\mk2_vml_mc3.dll",               "mk2_vml_mc3.dll"),
           (r"C:\Python27\Scripts\mk2_vml_p4n.dll",               "mk2_vml_p4n.dll"),
# These next DLLs appear to be copied correctly or as needed by cxfreeze...   
#           (r"C:\Python27\Scripts\libgcc_s_sjlj-1.dll",           "libgcc_s_sjlj-1.dll"),
#           (r"C:\Python27\Scripts\libgfortran-3.dll",             "libgfortran-3.dll"),
#           (r"C:\Python27\Scripts\libssp-0.dll",                  "libssp-0.dll"),
#           (r"C:\Python27\Scripts\libstdc++-6.dll",               "libstdc++-6.dll"),
#           (r"C:\Python27\Scripts\pythoncom27.dll",               "pythoncom27.dll"),
#           (r"C:\Python27\Scripts\pywintypes27.dll",              "pywintypes27.dll"),
            ]
             #,("Microsoft.VC90.MFC", mfcfiles), ]
#16) include the script module in the shared zip file
include_in_shared_zip=True
#17) include the Microsoft Visual C runtime DLLs and (if necessary) the manifest file required to run the executable without needing the redistributable package installed
include_msvcr =False
#18) the name of the script to use during initialization which, if given as a relative path, will be joined with the initscripts subdirectory of the cx_Freeze installation; the default value is "Console"
init_script=""
#19) comma separated list of packages to be treated as namespace packages (path is extended using pkgutil)
namespace_packages=[]
#20) optimization level, one of 0 (disabled), 1 or 2
optimize=0
#21) comma separated list of packages to include, which includes all submodules in the package
packages = ['numpy.linalg']
#22) comma separated list of paths to search; the default value is sys.path
path = []
#23) Modify filenames attached to code objects, which appear in tracebacks. Pass a comma separated list of paths in the form <search>=<replace>. The value * in the search portion will match the directory containing the entire package, leaving just the relative path to the module.
replace_paths=[]
#24) suppress all output except warnings  
silent=False
#25) list containing files to be included in the zip file directory; it is expected that this list will contain strings or 2-tuples for the source and destination
zip_includes=[]

setup(
    version = "0.0",
    description = "This is a program that works",
    author = "Your Name Here",
    name = "A text description",

    options = {"build_exe": {
#                            "append_script_to_exe": append_script_to_exe,
#                            "base":                 base,
#                            "bin_excludes":         bin_excludes,
#                            "bin_includes":         bin_includes,
#                            "bin_path_excludes":    bin_path_excludes,
#                            "bin_path_includes":    bin_path_includes,
                            "build_exe":            build_exe,
                            "compressed":           compressed,
#                            "constants":            constants,
                            "copy_dependent_files": copy_dependent_files,
#                            "create_shared_zip":    create_shared_zip,
                            "excludes":             excludes,
#                            "icon":                 icon,
                            "includes":             includes,
                            "include_files":        include_files,
#                            "include_in_shared_zip":include_in_shared_zip,
#                            "include_msvcr":        include_msvcr,
#                            "init_script":          init_script,
#                            "namespace_packages":   namespace_packages,
#                            "optimize":             optimize,
                            "packages":             packages,
                            "path":                 path,
#                            "replace_paths":        replace_paths,
#                            "silent":               silent,    
#                            "zip_includes":         zip_includes, 
                             }
               },

    executables = [MY_TARGET_EXE]
    )
#使用以下方法调用:
#python setup.py构建
从cx\U冻结导入设置,可执行文件
导入系统
导入glob
导入操作系统
进口zlib
进口舒蒂尔
#删除现有文件夹
rmtree(“构建”,忽略错误=True)
rmtree(“dist”,忽略错误=True)
########################################
#下面是可执行选项的列表
########################################
#“脚本”:#包含要冻结的脚本的文件名
#“initScript”:#将在实际脚本执行之前执行的初始化脚本的名称;此脚本用于设置可执行文件的环境;如果给定的名称没有绝对路径,则会搜索cx_Freeze包的initscripts子目录中的文件名
#“base”:#基本可执行文件的名称;如果给定的名称没有绝对路径,则会搜索cx_Freeze包的base子目录中的文件名
#“路径”:#用于搜索模块的路径列表
#“targetDir”:#放置目标可执行文件和任何依赖文件的目录
#“targetName”:#目标可执行文件的名称;默认值是脚本的名称,其扩展名与基本可执行文件的扩展名交换
#“包括”:#要包括的模块名称列表
#“排除”:#要排除的模块名称列表
#“包”:#要包括的包的名称列表,包括包的所有子模块
#“替换路径”:#修改附加到代码对象的文件名,这些文件名显示在回溯中。传递包含要搜索的路径和相应替换值的2元组列表。搜索“*”将匹配包含整个包的目录,只留下模块的相对路径。
#“compress”:#布尔值,指示是否应压缩模块字节码
#“CopyDependentFile”:#布尔值,指示是否应将依赖文件复制到目标目录
#“appendScriptToExe”:#布尔值,指示是否应将脚本模块附加到可执行文件本身
#“appendScriptToLibrary”:#布尔值,指示是否应将脚本模块追加到共享库zipfile
#“图标”:#图标名称,应包含在Windows上的可执行文件中,或放置在其他平台的目标目录中
#“namespacePackages”:#将被视为命名空间包的包列表(使用pkgutil扩展路径)
#“shortcutName”:#包含在MSI包中时为可执行文件提供快捷方式的名称
#“shortcutDir”:#由MSI软件包安装时放置快捷方式的目录;有关可以在此处放置哪些值的更多信息,请参阅MSI快捷方式表文档。
MY_TARGET_EXE=可执行文件(
#建造什么
script=“main.py”,
initScript=None,
base='Win32GUI',
targetDir=r“dist”,
targetName=“MyProgram.exe”,
压缩=真,
copyDependentFiles=True,
appendScriptToExe=False,
appendScriptToLibrary=False,
图标=无
)
########################################
#以下是build_exe选项的列表
########################################
#1) 将脚本模块附加到可执行文件
将\u脚本\u附加到\u exe=False
#2) 要使用的基本可执行文件的名称,如果作为相对路径给出,将与cx_Freeze安装的基本子目录联接;默认值为“