为 python 脚本错误创建 .exe

Creating .exe for python script error

我正在尝试将我的 python 脚本转换为 .exe 文件。

最初我尝试使用 py2exe 库为我的 python 脚本执行此转换。

python脚本只有一行:

print ("hello world")

和另一个将我的脚本转换为 .exe 的脚本

from distutils.core import setup
import py2exe

setup (console=['py2exetut.py'])

当我 运行 这个脚本使用命令时:python setup.py py2exe

我收到这个错误:

    running py2exe
    Traceback (most recent call last):
  File "setup.py", line 4, in <module>
    setup (console=['py2exetut.py'])
  File "C:\Users\draj3\AppData\Local\Programs\Python\Python36-32\lib\distutils\core.py", line 148, in setup
    dist.run_commands()
  File "C:\Users\draj3\AppData\Local\Programs\Python\Python36-32\lib\distutils\dist.py", line 955, in run_commands
    self.run_command(cmd)
  File "C:\Users\draj3\AppData\Local\Programs\Python\Python36-32\lib\distutils\dist.py", line 974, in run_command
    cmd_obj.run()
  File "C:\Users\draj3\AppData\Local\Programs\Python\Python36-32\lib\site-packages\py2exe\distutils_buildexe.py", line 188, in run
    self._run()
  File "C:\Users\draj3\AppData\Local\Programs\Python\Python36-32\lib\site-packages\py2exe\distutils_buildexe.py", line 267, in _run
    builder.analyze()
  File "C:\Users\draj3\AppData\Local\Programs\Python\Python36-32\lib\site-packages\py2exe\runtime.py", line 159, in analyze
    self.mf.import_hook(modname)
  File "C:\Users\draj3\AppData\Local\Programs\Python\Python36-32\lib\site-packages\py2exe\mf3.py", line 117, in import_hook
    module = self._gcd_import(name)
  File "C:\Users\draj3\AppData\Local\Programs\Python\Python36-32\lib\site-packages\py2exe\mf3.py", line 267, in _gcd_import
    return self._find_and_load(name)
  File "C:\Users\draj3\AppData\Local\Programs\Python\Python36-32\lib\site-packages\py2exe\mf3.py", line 320, in _find_and_load
    self._scan_code(module.__code__, module)
  File "C:\Users\draj3\AppData\Local\Programs\Python\Python36-32\lib\site-packages\py2exe\mf3.py", line 352, in _scan_code
    for what, args in self._scan_opcodes(code):
  File "C:\Users\draj3\AppData\Local\Programs\Python\Python36-32\lib\site-packages\py2exe\mf3.py", line 381, in _scan_opcodes
    yield "store", (names[oparg],)
IndexError: tuple index out of range

然后我尝试使用 Pyinstaller 另一个包,使用类似的程序,我遇到了类似这样的错误:

  pyinstaller file-creator.py
126 INFO: PyInstaller: 3.2.1
126 INFO: Python: 3.6.1
127 INFO: Platform: Windows-7-6.1.7601-SP1
128 INFO: wrote C:\Deepan\exe python\file-creator.spec
136 INFO: UPX is not available.
143 INFO: Extending PYTHONPATH with paths
['C:\Deepan\exe python', 'C:\Deepan\exe python']
144 INFO: checking Analysis
145 INFO: Building Analysis because out00-Analysis.toc is non existent
145 INFO: Initializing module dependency graph...
149 INFO: Initializing module graph hooks...
153 INFO: Analyzing base_library.zip ...
Traceback (most recent call last):
  File "C:\Users\draj3\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\draj3\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyinstaller-3.2.1-py3.6.egg\PyInstaller\__main__.py", line 90, in run
    run_build(pyi_config, spec_file, **vars(args))
  File "C:\Users\draj3\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyinstaller-3.2.1-py3.6.egg\PyInstaller\__main__.py", line 46, in run_build
    PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
  File "C:\Users\draj3\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyinstaller-3.2.1-py3.6.egg\PyInstaller\building\build_main.py", line 788, in main
    build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
  File "C:\Users\draj3\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyinstaller-3.2.1-py3.6.egg\PyInstaller\building\build_main.py", line 734, in build
    exec(text, spec_namespace)
  File "<string>", line 16, in <module>
  File "C:\Users\draj3\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyinstaller-3.2.1-py3.6.egg\PyInstaller\building\build_main.py", line 212, in __init__
    self.__postinit__()
  File "C:\Users\draj3\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyinstaller-3.2.1-py3.6.egg\PyInstaller\building\datastruct.py", line 161, in __postinit__
    self.assemble()
  File "C:\Users\draj3\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyinstaller-3.2.1-py3.6.egg\PyInstaller\building\build_main.py", line 317, in assemble
    excludes=self.excludes, user_hook_dirs=self.hookspath)
  File "C:\Users\draj3\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyinstaller-3.2.1-py3.6.egg\PyInstaller\depend\analysis.py", line 560, in initialize_modgraph
    graph.import_hook(m)
  File "C:\Users\draj3\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyinstaller-3.2.1-py3.6.egg\PyInstaller\lib\modulegraph\modulegraph.py", line 1509, in import_hook
    source_package, target_module_partname, level)
  File "C:\Users\draj3\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyinstaller-3.2.1-py3.6.egg\PyInstaller\lib\modulegraph\modulegraph.py", line 1661, in _find_head_package
    target_module_headname, target_package_name, source_package)
  File "C:\Users\draj3\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyinstaller-3.2.1-py3.6.egg\PyInstaller\depend\analysis.py", line 209, in _safe_import_module
    module_basename, module_name, parent_package)
  File "C:\Users\draj3\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyinstaller-3.2.1-py3.6.egg\PyInstaller\lib\modulegraph\modulegraph.py", line 2077, in _safe_import_module
    module_name, file_handle, pathname, metadata)
  File "C:\Users\draj3\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyinstaller-3.2.1-py3.6.egg\PyInstaller\lib\modulegraph\modulegraph.py", line 2167, in _load_module
    self._scan_code(m, co, co_ast)
  File "C:\Users\draj3\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyinstaller-3.2.1-py3.6.egg\PyInstaller\lib\modulegraph\modulegraph.py", line 2585, in _scan_code
    module, module_code_object, is_scanning_imports=False)
  File "C:\Users\draj3\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyinstaller-3.2.1-py3.6.egg\PyInstaller\lib\modulegraph\modulegraph.py", line 2831, in _scan_bytecode
    global_attr_name = get_operation_arg_name()
  File "C:\Users\draj3\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyinstaller-3.2.1-py3.6.egg\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

注意:我使用的是 windows 7 OS 和 python 3.6.

所以我想问题不在于包裹。有什么方法可以继续吗?

py2exe 不推荐用于 3.6(通常人们会为此使用 Python 2.x。)。为此使用 cx_Freeze 库。

您可以从 here 下载它。

您也可以在 cmd 提示符下输入 pip install cx_Freeze

通常的setup.py脚本:

import cx_Freeze as cx_f #for now, do the wildcard import, though the bigger the script gets, I would recommend an as ... structure

executables = [cx_f.Executable("Oilfield.py")]

cx_f.setup(name="whatever you want", executables=executables)

随着您的脚本变得越来越复杂,您可能需要在 setup() 函数中包含更多项目,但 executables 变量是最重要的部分。

我建议使用 "pyinstaller"。它的使用很简单并且与 python 3.6

兼容

如果对于 py2exe,这可能会有所帮助:https://github.com/albertosottile/py2exe