使用pyinstaller将python程序转为exe程序

本来是想用py2exe来转换的。无奈遇到了问题,没有找到答案,但自己觉得是和python版本或者py2exe版本有点关系,所以暂且提了个问题https://bbs.csdn.net/topics/392688323 看看有没有哪位神僧有遇到过。

然后就换用pyinstaller

写个大概的流程:

1:安装pyinstaller

在cmd.exe控制台中执行(前提是python一定要加入环境变量啊!!!!)

E:\Python>pip3 install pyinstaller

但是刚开始发现有报错:

E:\Python>pip3 install pyinstaller
Collecting pyinstaller
  Downloading https://files.pythonhosted.org/packages/03/32/0e0de593f129bf1d1e77eed562496d154ef4460fd5cecfd78612ef39a0cc/PyInstaller-3.4.tar.gz (3.5MB)
    100% |████████████████████████████████| 3.5MB 2.3MB/s
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
    Preparing wheel metadata ... done
Collecting altgraph (from pyinstaller)
  Downloading https://files.pythonhosted.org/packages/0a/cc/646187eac4b797069e2e6b736f14cdef85dbe405c9bfc7803ef36e4f62ef/altgraph-0.16.1-py2.py3-none-any.whl
Collecting pywin32-ctypes (from pyinstaller)
  Downloading https://files.pythonhosted.org/packages/9e/4b/3ab2720f1fa4b4bc924ef1932b842edf10007e4547ea8157b0b9fc78599a/pywin32_ctypes-0.2.0-py2.py3-none-any.whl
Requirement already satisfied: setuptools in c:\program files\lib\site-packages (from pyinstaller) (40.8.0)
Collecting pefile>=2017.8.1 (from pyinstaller)
  Downloading https://files.pythonhosted.org/packages/36/58/acf7f35859d541985f0a6ea3c34baaefbfaee23642cf11e85fe36453ae77/pefile-2019.4.18.tar.gz (62kB)
    100% |████████████████████████████████| 71kB 8.0MB/s
Collecting macholib>=1.8 (from pyinstaller)
  Downloading https://files.pythonhosted.org/packages/41/f1/6d23e1c79d68e41eb592338d90a33af813f98f2b04458aaf0b86908da2d8/macholib-1.11-py2.py3-none-any.whl
Collecting future (from pefile>=2017.8.1->pyinstaller)
  Downloading https://files.pythonhosted.org/packages/90/52/e20466b85000a181e1e144fd8305caf2cf475e2f9674e797b222f8105f5f/future-0.17.1.tar.gz (829kB)
    100% |████████████████████████████████| 829kB 1.2MB/s
Building wheels for collected packages: pyinstaller
  Building wheel for pyinstaller (PEP 517) ... done
  Stored in directory: C:\Users\nasri.yang\AppData\Local\pip\Cache\wheels\09\ee\0a\94ef5d39074625f49e2e9cf97ac30cb7a87e1a7458ed195b8d
Successfully built pyinstaller
Installing collected packages: altgraph, pywin32-ctypes, future, pefile, macholib, pyinstaller
Could not install packages due to an EnvironmentError: [WinError 5] 拒绝访问。: 'c:\\program files\\Lib\\site-packages\\altgraph'
Consider using the `--user` option or check the permissions.

You are using pip version 19.0.3, however version 19.1.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

看到提示之后,使用下面的命令后就可以了。

E:\Python>pip3 install --user pyinstaller
Collecting pyinstaller
Requirement already satisfied: setuptools in c:\program files\lib\site-packages (from pyinstaller) (40.8.0)
Collecting pefile>=2017.8.1 (from pyinstaller)
  Using cached https://files.pythonhosted.org/packages/36/58/acf7f35859d541985f0a6ea3c34baaefbfaee23642cf11e85fe36453ae77/pefile-2019.4.18.tar.gz
Collecting pywin32-ctypes (from pyinstaller)
  Using cached https://files.pythonhosted.org/packages/9e/4b/3ab2720f1fa4b4bc924ef1932b842edf10007e4547ea8157b0b9fc78599a/pywin32_ctypes-0.2.0-py2.py3-none-any.whl
Collecting altgraph (from pyinstaller)
  Using cached https://files.pythonhosted.org/packages/0a/cc/646187eac4b797069e2e6b736f14cdef85dbe405c9bfc7803ef36e4f62ef/altgraph-0.16.1-py2.py3-none-any.whl
Collecting macholib>=1.8 (from pyinstaller)
  Using cached https://files.pythonhosted.org/packages/41/f1/6d23e1c79d68e41eb592338d90a33af813f98f2b04458aaf0b86908da2d8/macholib-1.11-py2.py3-none-any.whl
Collecting future (from pefile>=2017.8.1->pyinstaller)
  Using cached https://files.pythonhosted.org/packages/90/52/e20466b85000a181e1e144fd8305caf2cf475e2f9674e797b222f8105f5f/future-0.17.1.tar.gz
Installing collected packages: future, pefile, pywin32-ctypes, altgraph, macholib, pyinstaller
  Running setup.py install for future ... done
  Running setup.py install for pefile ... done
  The scripts macho_dump.exe, macho_find.exe and macho_standalone.exe are installed in 'C:\Users\nasri.yang\AppData\Roaming\Python\Python37\Scripts' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  The scripts pyi-archive_viewer.exe, pyi-bindepend.exe, pyi-grab_version.exe, pyi-makespec.exe, pyi-set_version.exe and pyinstaller.exe are installed in 'C:\Users\nasri.yang\AppData\Roaming\Python\Python37\Scripts' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed altgraph-0.16.1 future-0.17.1 macholib-1.11 pefile-2019.4.18 pyinstaller-3.4 pywin32-ctypes-0.2.0
You are using pip version 19.0.3, however version 19.1.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

这个提示很友好,说我没有设置环境变量。所以我就设置了下环境变量

我Win10的系统,环境变量设置起来很简单,把上面提示的路径加上到path中就可以,可能要重启下电脑哦。

我没有重启,所以下面我就用绝对路径转换了。

2: 开始转换

我的Python程序比较简单,就是把正在运行的一个进程个杀掉。

估计有人会说我,你是不是讨厌sogou的这个cloud程序?  我呢,我哪里是讨厌这个cloud,我可是相当讨厌这个cloud.....

import os
os.system("taskkill /IM SogouCloud.exe /F")

然后就用下面的命令转换

在cmd下输入 pyinstaller.exe --onefile killexe.py

我下面写的是绝对路径

E:\Python>C:\Users\nasri.yang\AppData\Roaming\Python\Python37\Scripts\pyinstaller.exe --onefile killexe.py
59 INFO: PyInstaller: 3.4
59 INFO: Python: 3.7.3
60 INFO: Platform: Windows-10-10.0.17134-SP0
69 INFO: wrote E:\Python\killexe.spec
71 INFO: UPX is not available.
73 INFO: Extending PYTHONPATH with paths
['E:\\Python', 'E:\\Python']
73 INFO: checking Analysis
73 INFO: Building Analysis because Analysis-00.toc is non existent
73 INFO: Initializing module dependency graph...
75 INFO: Initializing module graph hooks...
77 INFO: Analyzing base_library.zip ...
3363 INFO: running Analysis Analysis-00.toc
3367 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by c:\program files\python.exe
3394 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of c:\program files\python.exe
3415 WARNING: lib not found: api-ms-win-crt-locale-l1-1-0.dll dependency of c:\program files\python.exe
3435 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of c:\program files\python.exe
3455 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of c:\program files\python.exe
3479 WARNING: lib not found: api-ms-win-crt-math-l1-1-0.dll dependency of c:\program files\python.exe
3527 WARNING: lib not found: api-ms-win-crt-math-l1-1-0.dll dependency of c:\program files\python37.dll
3548 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of c:\program files\python37.dll
3567 WARNING: lib not found: api-ms-win-crt-process-l1-1-0.dll dependency of c:\program files\python37.dll
3589 WARNING: lib not found: api-ms-win-crt-locale-l1-1-0.dll dependency of c:\program files\python37.dll
3611 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of c:\program files\python37.dll
3632 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of c:\program files\python37.dll
3654 WARNING: lib not found: api-ms-win-crt-convert-l1-1-0.dll dependency of c:\program files\python37.dll
3674 WARNING: lib not found: api-ms-win-crt-conio-l1-1-0.dll dependency of c:\program files\python37.dll
3694 WARNING: lib not found: api-ms-win-crt-filesystem-l1-1-0.dll dependency of c:\program files\python37.dll
3722 WARNING: lib not found: api-ms-win-crt-environment-l1-1-0.dll dependency of c:\program files\python37.dll
3742 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of c:\program files\python37.dll
3762 WARNING: lib not found: api-ms-win-crt-time-l1-1-0.dll dependency of c:\program files\python37.dll
3787 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of c:\program files\VCRUNTIME140.dll
3807 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of c:\program files\VCRUNTIME140.dll
3828 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of c:\program files\VCRUNTIME140.dll
3849 WARNING: lib not found: api-ms-win-crt-convert-l1-1-0.dll dependency of c:\program files\VCRUNTIME140.dll
3872 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of c:\program files\VCRUNTIME140.dll
3875 INFO: Caching module hooks...
3882 INFO: Analyzing E:\Python\killexe.py
3886 INFO: Loading module hooks...
3887 INFO: Loading module hook "hook-encodings.py"...
3974 INFO: Loading module hook "hook-pydoc.py"...
3976 INFO: Loading module hook "hook-xml.py"...
4223 INFO: Looking for ctypes DLLs
4223 INFO: Analyzing run-time hooks ...
4230 INFO: Looking for dynamic libraries
4260 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of c:\program files\DLLs\_ssl.pyd
4281 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of c:\program files\DLLs\_ssl.pyd
4307 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of c:\program files\DLLs\_ssl.pyd
4334 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of c:\program files\DLLs\pyexpat.pyd
4354 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of c:\program files\DLLs\pyexpat.pyd
4373 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of c:\program files\DLLs\pyexpat.pyd
4395 WARNING: lib not found: api-ms-win-crt-environment-l1-1-0.dll dependency of c:\program files\DLLs\pyexpat.pyd
4416 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of c:\program files\DLLs\pyexpat.pyd
4447 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of c:\program files\DLLs\unicodedata.pyd
4467 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of c:\program files\DLLs\unicodedata.pyd
4490 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of c:\program files\DLLs\unicodedata.pyd
4518 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of c:\program files\DLLs\_hashlib.pyd
4544 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of c:\program files\DLLs\_bz2.pyd
4564 WARNING: lib not found: api-ms-win-crt-math-l1-1-0.dll dependency of c:\program files\DLLs\_bz2.pyd
4584 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of c:\program files\DLLs\_bz2.pyd
4604 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of c:\program files\DLLs\_bz2.pyd
4626 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of c:\program files\DLLs\_bz2.pyd
4653 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of c:\program files\DLLs\_lzma.pyd
4673 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of c:\program files\DLLs\_lzma.pyd
4700 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of c:\program files\DLLs\_socket.pyd
4725 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of c:\program files\DLLs\_socket.pyd
4749 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of c:\program files\DLLs\select.pyd
4783 WARNING: lib not found: api-ms-win-crt-utility-l1-1-0.dll dependency of c:\program files\DLLs\libssl-1_1-x64.dll
4802 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of c:\program files\DLLs\libssl-1_1-x64.dll
4825 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of c:\program files\DLLs\libssl-1_1-x64.dll
4844 WARNING: lib not found: api-ms-win-crt-time-l1-1-0.dll dependency of c:\program files\DLLs\libssl-1_1-x64.dll
4904 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of c:\program files\DLLs\libcrypto-1_1-x64.dll
4926 WARNING: lib not found: api-ms-win-crt-filesystem-l1-1-0.dll dependency of c:\program files\DLLs\libcrypto-1_1-x64.dll
4945 WARNING: lib not found: api-ms-win-crt-utility-l1-1-0.dll dependency of c:\program files\DLLs\libcrypto-1_1-x64.dll
4966 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of c:\program files\DLLs\libcrypto-1_1-x64.dll
4986 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of c:\program files\DLLs\libcrypto-1_1-x64.dll
5008 WARNING: lib not found: api-ms-win-crt-convert-l1-1-0.dll dependency of c:\program files\DLLs\libcrypto-1_1-x64.dll
5031 WARNING: lib not found: api-ms-win-crt-environment-l1-1-0.dll dependency of c:\program files\DLLs\libcrypto-1_1-x64.dll
5051 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of c:\program files\DLLs\libcrypto-1_1-x64.dll
5071 WARNING: lib not found: api-ms-win-crt-time-l1-1-0.dll dependency of c:\program files\DLLs\libcrypto-1_1-x64.dll
5071 INFO: Looking for eggs
5072 INFO: Using Python library c:\program files\python37.dll
5073 INFO: Found binding redirects:
[]
5077 INFO: Warnings written to E:\Python\build\killexe\warn-killexe.txt
5120 INFO: Graph cross-reference written to E:\Python\build\killexe\xref-killexe.html
5137 INFO: checking PYZ
5137 INFO: Building PYZ because PYZ-00.toc is non existent
5138 INFO: Building PYZ (ZlibArchive) E:\Python\build\killexe\PYZ-00.pyz
5606 INFO: Building PYZ (ZlibArchive) E:\Python\build\killexe\PYZ-00.pyz completed successfully.
5611 INFO: checking PKG
5611 INFO: Building PKG because PKG-00.toc is non existent
5611 INFO: Building PKG (CArchive) PKG-00.pkg
7305 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
7308 INFO: Bootloader C:\Users\nasri.yang\AppData\Roaming\Python\Python37\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
7308 INFO: checking EXE
7309 INFO: Building EXE because EXE-00.toc is non existent
7310 INFO: Building EXE from EXE-00.toc
7311 INFO: Appending archive to EXE E:\Python\dist\killexe.exe
7321 INFO: Building EXE from EXE-00.toc completed successfully.

 

生产过程不用看,在对应的目录中可以看到有个dist目录

在里面就有killexe.exe

双击这个exe,就可以正常的杀掉这个进程了。

然后下面我的目标就是开机启动这个exe,然后加入类似的代码,杀掉更多的“流氓”进程。Sorry....

 

你可能感兴趣的:(Python,Python,pyinstaller,py2exe)