2017-08-07 85 views
2

我想將我的python腳本轉換爲.exe文件。爲python腳本錯誤創建.exe

最初我嘗試用py2exe庫來爲我的python腳本做這個轉換。

的Python腳本只有一個行:

print ("hello world") 

和另一個腳本,將轉換我的腳本.EXE

from distutils.core import setup 
import py2exe 

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

,當我使用指令該腳本:Python安裝。 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

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

+0

cx_Freeze就像一個魅力。謝謝! –