Python 3.x 如何防止Python构建失败

Python 3.x 如何防止Python构建失败,python-3.x,tkinter,build,pyinstaller,cx-freeze,Python 3.x,Tkinter,Build,Pyinstaller,Cx Freeze,我正在尝试构建一个我开发的基本Python tkinter应用程序。当我尝试通过命令提示符使用cx_freeze构建应用程序时,会得到以下错误输出: C:\Users\RedCode\PycharmProjects\DatabaseExample>python setup.py build running build running build_exe Traceback (most recent call last): File "setup.py", line 26, in <

我正在尝试构建一个我开发的基本Python tkinter应用程序。当我尝试通过命令提示符使用cx_freeze构建应用程序时,会得到以下错误输出:

C:\Users\RedCode\PycharmProjects\DatabaseExample>python setup.py build
running build
running build_exe
Traceback (most recent call last):
  File "setup.py", line 26, in <module>
    executables = [Executable("cardb.py", base = base)])
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cx_Freeze\dist.py", line 349, in setup
    distutils.core.setup(**attrs)
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\distutils\core.py", line 148, in setup
    dist.run_commands()
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\distutils\dist.py", line 955, in run_commands
    self.run_command(cmd)
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\distutils\dist.py", line 974, in run_command
    cmd_obj.run()
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\distutils\command\build.py", line 135, in run
    self.run_command(cmd_name)
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\distutils\cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\distutils\dist.py", line 974, in run_command
    cmd_obj.run()
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cx_Freeze\dist.py", line 219, in run
    freezer.Freeze()
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cx_Freeze\freezer.py", line 623, in Freeze
    self._FreezeExecutable(executable)
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cx_Freeze\freezer.py", line 199, in _FreezeExecutable
    finder.IncludeFile(exe.script, exe.moduleName)
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cx_Freeze\finder.py", line 633, in IncludeFile
    deferredImports)
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cx_Freeze\finder.py", line 474, in _LoadModule
    self._ScanCode(module.code, module, deferredImports)
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cx_Freeze\finder.py", line 564, in _ScanCode
    module, relativeImportIndex)
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cx_Freeze\finder.py", line 310, in _ImportModule
    deferredImports, namespace = namespace)
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cx_Freeze\finder.py", line 403, in _InternalImportModule
    parentModule, namespace)
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cx_Freeze\finder.py", line 416, in _LoadModule
    namespace)
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cx_Freeze\finder.py", line 485, in _LoadPackage
    self._LoadModule(name, fp, path, info, deferredImports, parent)
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cx_Freeze\finder.py", line 463, in _LoadModule
    self._RunHook("load", module.name, module)
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cx_Freeze\finder.py", line 536, in _RunHook
    method(self, *args)
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cx_Freeze\hooks.py", line 613, in load_tkinter
    tclSourceDir = os.environ["TCL_LIBRARY"]
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\os.py", line 669, in __getitem__
    raise KeyError(key) from None
KeyError: 'TCL_LIBRARY'
尽管它说它成功了,但我在我的电脑上找不到它的任何内容,即使在我的整个机器上使用了搜索功能

然后我尝试了
pyinstaller setup.py
,得到了以下输出错误:

C:\Users\RedCode\PycharmProjects\DatabaseExample>pyinstaller setup.py
495 INFO: PyInstaller: 3.2.1
495 INFO: Python: 3.6.1
496 INFO: Platform: Windows-10-10.0.10240-SP0
501 INFO: wrote C:\Users\RedCode\PycharmProjects\DatabaseExample\setup.spec
503 INFO: UPX is not available.
509 INFO: Extending PYTHONPATH with paths
['C:\\Users\\RedCode\\PycharmProjects\\DatabaseExample',
 'C:\\Users\\RedCode\\PycharmProjects\\DatabaseExample']
509 INFO: checking Analysis
509 INFO: Building Analysis because out00-Analysis.toc is non existent
510 INFO: Initializing module dependency graph...
512 INFO: Initializing module graph hooks...
518 INFO: Analyzing base_library.zip ...
Traceback (most recent call last):
  File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\Scripts\pyinstaller-script.py", line 11, in <module>
    load_entry_point('PyInstaller==3.2.1', 'console_scripts', 'pyinstaller')()
  File "c:\users\RedCode\appdata\local\programs\python\python36-32\lib\site-packages\PyInstaller\__main__.py", line 90, in run
    run_build(pyi_config, spec_file, **vars(args))
  File "c:\users\RedCode\appdata\local\programs\python\python36-32\lib\site-packages\PyInstaller\__main__.py", line 46, in run_build
    PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
  File "c:\users\RedCode\appdata\local\programs\python\python36-32\lib\site-packages\PyInstaller\building\build_main.py", line 788, in main
    build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
  File "c:\users\RedCode\appdata\local\programs\python\python36-32\lib\site-packages\PyInstaller\building\build_main.py", line 734, in build
    exec(text, spec_namespace)
  File "<string>", line 16, in <module>
  File "c:\users\RedCode\appdata\local\programs\python\python36-32\lib\site-packages\PyInstaller\building\build_main.py", line 212, in __init__
    self.__postinit__()
  File "c:\users\RedCode\appdata\local\programs\python\python36-32\lib\site-packages\PyInstaller\building\datastruct.py", line 161, in __postinit__
    self.assemble()
  File "c:\users\RedCode\appdata\local\programs\python\python36-32\lib\site-packages\PyInstaller\building\build_main.py", line 317, in assemble
    excludes=self.excludes, user_hook_dirs=self.hookspath)
  File "c:\users\RedCode\appdata\local\programs\python\python36-32\lib\site-packages\PyInstaller\depend\analysis.py", line 560, in initialize_modgraph
    graph.import_hook(m)
  File "c:\users\RedCode\appdata\local\programs\python\python36-32\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1509, in import_hook
    source_package, target_module_partname, level)
  File "c:\users\RedCode\appdata\local\programs\python\python36-32\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1661, in _find_head_package
    target_module_headname, target_package_name, source_package)
  File "c:\users\RedCode\appdata\local\programs\python\python36-32\lib\site-packages\PyInstaller\depend\analysis.py", line 209, in _safe_import_module
    module_basename, module_name, parent_package)
  File "c:\users\RedCode\appdata\local\programs\python\python36-32\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2077, in _safe_import_module
    module_name, file_handle, pathname, metadata)
  File "c:\users\RedCode\appdata\local\programs\python\python36-32\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2167, in _load_module
    self._scan_code(m, co, co_ast)
  File "c:\users\RedCode\appdata\local\programs\python\python36-32\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2585, in _scan_code
    module, module_code_object, is_scanning_imports=False)
  File "c:\users\RedCode\appdata\local\programs\python\python36-32\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2831, in _scan_bytecode
    global_attr_name = get_operation_arg_name()
  File "c:\users\RedCode\appdata\local\programs\python\python36-32\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2731, in get_operation_arg_name
    return module_code_object.co_names[co_names_index]
IndexError: tuple index out of range
我正在使用cx_Freeze构建应用程序,如果有帮助的话,我正在通过PyCharm Community 2017.1.1运行Python 3.6.1。我到处都搜索过,似乎找不到任何有效的东西(而且看起来对Python 3.6.x的支持非常差)

如何成功构建我的应用程序?或者至少,我如何纠正在构建过程中发生的这些错误

import sys
import os
from cx_Freeze import setup, Executable
import cx_Freeze
import tkinter
import os.path


base = None

if sys.platform == 'win32':
    base = "Win32GUI"


PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')

#This was another style I tried using. If you don't have your python installed #to your path you could try this method. Obviously need to update the URL to #those files 
#os.environ['TCL_LIBRARY'] = r'C:\Users\gardener\Downloads\WinPython-64bit-3.5.3.0Qt5\python-3.5.3.amd64\tcl\tcl8.6'
#os.environ['TK_LIBRARY'] = r'C:\Users\gardener\Downloads\WinPython-64bit-3.5.3.0Qt5\python-3.5.3.amd64\tcl\tk8.6'

executables = [cx_Freeze.Executable("cardb.py", base=base)]


options = {
    'build_exe': {

        'include_files':[
            os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'),
            os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'),

         ]
    },

}

cx_Freeze.setup(
    name = "cardb",
    options = options,
    version = "0.1",
    description = 'Simple tkinter application',
    executables = executables
)

试一试

如果可以的话,你能解释一下为什么这样做吗?这将对其他读者有所帮助。通常,当cx\U Freeze无法正确创建Tkinter应用程序时,这是因为我所指的DLL文件没有包含在冻结中。太棒了,它成功了!但现在每当我运行应用程序时,cmd提示符也会打开。运行应用程序时,是否有任何方法防止cmd打开?请注意,由于某些原因,它不再工作:(命令提示符打开与cx\U冻结无关。您的主程序代码中可能有东西打开它
application_title = "Car Database"
main_python_file = "cardb.py"

import sys

from cx_Freeze import setup, Executable

base = None
if sys.platform == "win32":
    base = "Win32GUI"

setup(
        name = "cardb.py",
        version = "0.1",
        description = "Simple tkinter application",
        executables = [Executable("cardb.py", base = base)])
import sys
import os
from cx_Freeze import setup, Executable
import cx_Freeze
import tkinter
import os.path


base = None

if sys.platform == 'win32':
    base = "Win32GUI"


PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')

#This was another style I tried using. If you don't have your python installed #to your path you could try this method. Obviously need to update the URL to #those files 
#os.environ['TCL_LIBRARY'] = r'C:\Users\gardener\Downloads\WinPython-64bit-3.5.3.0Qt5\python-3.5.3.amd64\tcl\tcl8.6'
#os.environ['TK_LIBRARY'] = r'C:\Users\gardener\Downloads\WinPython-64bit-3.5.3.0Qt5\python-3.5.3.amd64\tcl\tk8.6'

executables = [cx_Freeze.Executable("cardb.py", base=base)]


options = {
    'build_exe': {

        'include_files':[
            os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'),
            os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'),

         ]
    },

}

cx_Freeze.setup(
    name = "cardb",
    options = options,
    version = "0.1",
    description = 'Simple tkinter application',
    executables = executables
)