简体   繁体   中英

Creating .exe for python script error

I am trying to convert my python script into .exe file.

Initially I tried with py2exe library to do this conversion for my python script.

The python script has only one line :

print ("hello world")

and another script that will convert my script to .exe

from distutils.core import setup
import py2exe

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

and when i run this script using the command : python setup.py py2exe

I am getting this error :

    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

And then I tried with Pyinstaller another package, with similar kinda procedure, I am getting some similar kind of error like this :

  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

Note: I am using windows 7 OS and python 3.6.

So I guess the problem is not with the package. any way to proceed ?

py2exe isn't recommended for 3.6 (usually one would use Python 2.x for that.). Use the cx_Freeze library for that.

You can download it from here .

You could also type in cmd prompt pip install cx_Freeze .

The usual setup.py script:

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)

As your scripts get more complicated, you may have to include more items on the setup() function, but the executables variable is the most important part.

I recommend using "pyinstaller". its use is easy and compatible with python 3.6

if for py2exe maybe this might help: https://github.com/albertosottile/py2exe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM