为python脚本错误创建.exe

时间:2017-08-07 13:34:33

标签: python py2exe

我正在尝试将我的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操作系统和python 3.6。

所以我猜问题不在于包裹。还有什么办法吗?

2 个答案:

答案 0 :(得分:2)

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变量是最重要的部分。

答案 1 :(得分:1)

我建议使用“ pyinstaller”。它的使用非常简单,并且与python 3.6兼容

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