Skip to content

Latest commit

 

History

History
191 lines (160 loc) · 4.85 KB

028.Py2EXE.md

File metadata and controls

191 lines (160 loc) · 4.85 KB

Py2EXE

But It is in Windows.

Now, I want to make a python code to exe in Windows.

e.g.

# 028.Py2EXE/run.py
import time
n = 0
while n < 10:
	time.sleep(0.5)
	print(n)
	n += 1

First, You need:

pip install pyinstaller
# pip install pywin32 PyQt5

And run this code:

# 028.Py2EXE/setup.py
import os
path = r"C:\Users\IvanXu\Anaconda3\Scripts"

# 00.Make trun.spec
os.system(path + r"\pyi-makespec.exe -F run.py --name trun")

# 01.Change trun.spec to run.spec
rfile = open("run.spec", "w")
rfile.write("import sys\n")
rfile.write("sys.setrecursionlimit(9999)\n")
with open("trun.spec", "r") as f:
    for i in f:
        rfile.write(i)
rfile.close()

# 02.pyinstaller run.spec to exe
os.system(path + r"\pyinstaller.exe -F run.spec")

# 03.Test Run
os.system(r"dist\trun.exe")

run.spec

! cat run.spec

import sys
sys.setrecursionlimit(9999)
# -*- mode: python -*-

block_cipher = None


a = Analysis(['run.py'],
             pathex=['C:\\Users\\IvanXu\\Desktop\\code\\pytoexe'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='trun',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=True )

BTW, dll insert in datas, like cx_Oracle

...
a = Analysis(['run.py'],
...
    datas=[
        ('...\\instantclient-basic-nt-12.2.0.1.0\\instantclient_12_2\\oci.dll','.'),
    ]
    hiddenimports=[],

Finally, If You see:

C:\Users\IvanXu\Desktop\code\pytoexe>C:\Users\IvanXu\Anaconda3\python.exe setup.py
wrote C:\Users\IvanXu\Desktop\code\pytoexe\trun.spec
now run pyinstaller.py to build the executable
83 INFO: PyInstaller: 3.4
83 INFO: Python: 3.6.5
84 INFO: Platform: Windows-7-6.1.7601-SP1
86 INFO: UPX is not available.
88 INFO: Extending PYTHONPATH with paths
['C:\\Users\\IvanXu\\Desktop\\code\\pytoexe',
 'C:\\Users\\IvanXu\\Desktop\\code\\pytoexe']
89 INFO: checking Analysis
89 INFO: Building Analysis because Analysis-00.toc is non existent
90 INFO: Initializing module dependency graph...
92 INFO: Initializing module graph hooks...
93 INFO: Analyzing base_library.zip ...
3134 INFO: running Analysis Analysis-00.toc
3150 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by c:\users\ivanxu\anaconda3\python.exe
3756 INFO: Caching module hooks...
3760 INFO: Analyzing run.py
3763 INFO: Loading module hooks...
3764 INFO: Loading module hook "hook-encodings.py"...
3859 INFO: Loading module hook "hook-pydoc.py"...
3860 INFO: Loading module hook "hook-xml.py"...
4102 INFO: Looking for ctypes DLLs
4102 INFO: Analyzing run-time hooks ...
4107 INFO: Looking for dynamic libraries
4187 INFO: Looking for eggs
4188 INFO: Using Python library c:\users\ivanxu\anaconda3\python36.dll
4188 INFO: Found binding redirects:
[]
4193 INFO: Warnings written to C:\Users\IvanXu\Desktop\code\pytoexe\build\run\warn-run.txt
4233 INFO: Graph cross-reference written to C:\Users\IvanXu\Desktop\code\pytoexe\build\run\xref-run.html
4241 INFO: checking PYZ
4242 INFO: Building PYZ because PYZ-00.toc is non existent
4242 INFO: Building PYZ (ZlibArchive) C:\Users\IvanXu\Desktop\code\pytoexe\build\run\PYZ-00.pyz
4643 INFO: Building PYZ (ZlibArchive) C:\Users\IvanXu\Desktop\code\pytoexe\build\run\PYZ-00.pyz completed successfully.
4646 INFO: checking PKG
4647 INFO: Building PKG because PKG-00.toc is non existent
4647 INFO: Building PKG (CArchive) PKG-00.pkg
5951 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
5953 INFO: Bootloader c:\users\ivanxu\anaconda3\lib\site-packages\PyInstaller\bootloader\Windows-32bit\run.exe
5953 INFO: checking EXE
5954 INFO: Building EXE because EXE-00.toc is non existent
5954 INFO: Building EXE from EXE-00.toc
5955 INFO: Appending archive to EXE C:\Users\IvanXu\Desktop\code\pytoexe\dist\trun.exe
5972 INFO: Building EXE from EXE-00.toc completed successfully.
0
1
2
3
4
5
6
7
8
9

Nice!

! dir

2019/10/21  19:14    <DIR>          .
2019/10/21  19:14    <DIR>          ..
2019/10/21  19:14    <DIR>          build
2019/10/21  19:14    <DIR>          dist
2019/10/21  19:09                71 run.py
2019/10/21  19:14               894 run.spec
2019/10/21  19:14               506 setup.py
2019/10/21  19:14               853 trun.spec
2019/10/21  19:14    <DIR>          __pycache__
               4 个文件          2,324 字节
               5 个目录 55,285,837,824 可用字节

dist\trun.exe

You got it!