加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

pyinstaller打包python的执行文件如何在32位和64位操作系统下也能运行

(2018-06-12 16:33:24)
标签:

pyinstaller

32位和64位同时能运行

分类: Python
非常有帮助的资讯参见Stackoverflow上的一篇问答。
搜索 create-an-exe-compatible-with-all-versions-of-windows-64-bit-and-32-bit-even-if
/questions/47155474/create-an-exe-compatible-with-all-versions-of-windows-64-bit-and-32-bit-even-if

我们可以通过定义 setup.spec 来解决pyinstaller的参数设置问题。

我在64位的windows 10 操作系统下,用64位Python环境,PyInstaller 打包一个py程序。

如 pyinstaller -F -w my.py

你会看到一些警告信息WARNING,但是结果还是得到一个执行文件。

D:\working\bdt>pyinstaller --clean bdt.spec

==============================

96 INFO: PyInstaller: 3.3.1
96 INFO: Python: 3.6.5
97 INFO: Platform: Windows-10-10.0.16299-SP0
98 INFO: UPX is not available.
99 INFO: Removing temporary files and cleaning cache in C:\Users\xiang\AppData\Roaming\pyinstaller
240 INFO: Extending PYTHONPATH with paths
['D:\\working\\bdt', 'D:\\working\\bdt']
241 INFO: checking Analysis
241 INFO: Building Analysis because out00-Analysis.toc is non existent
242 INFO: Initializing module dependency graph...
259 INFO: Initializing module graph hooks...
268 INFO: Analyzing base_library.zip ...
6388 INFO: running Analysis out00-Analysis.toc
6396 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by c:\python\python36-32\python.exe
6999 WARNING: lib not found: api-ms-win-crt-process-l1-1-0.dll dependency of c:\python\python36-32\python36.dll
7219 WARNING: lib not found: api-ms-win-crt-conio-l1-1-0.dll dependency of c:\python\python36-32\python36.dll
8103 INFO: Caching module hooks...
8113 INFO: Analyzing bdt.py
8396 INFO: Loading module hooks...
8396 INFO: Loading module hook "hook-encodings.py"...
8528 INFO: Loading module hook "hook-pydoc.py"...
8530 INFO: Loading module hook "hook-xml.py"...
8878 INFO: Loading module hook "hook-_tkinter.py"...
9074 INFO: checking Tree
9075 INFO: Building Tree because out00-Tree.toc is non existent
9077 INFO: Building Tree out00-Tree.toc
9171 INFO: checking Tree
9172 INFO: Building Tree because out01-Tree.toc is non existent
9173 INFO: Building Tree out01-Tree.toc
9219 INFO: Looking for ctypes DLLs
9219 INFO: Analyzing run-time hooks ...
9225 INFO: Including run-time hook 'pyi_rth__tkinter.py'
9234 INFO: Looking for dynamic libraries
9474 WARNING: lib not found: api-ms-win-crt-conio-l1-1-0.dll dependency of c:\python\python36-32\DLLs\_ssl.pyd
9731 WARNING: lib not found: api-ms-win-crt-conio-l1-1-0.dll dependency of c:\python\python36-32\DLLs\_hashlib.pyd
10088 INFO: Looking for eggs
10089 INFO: Using Python library c:\python\python36-32\python36.dll
10090 INFO: Found binding redirects:
[]
10109 INFO: Warnings written to D:\working\bdt\build\bdt\warnbdt.txt
10256 INFO: Graph cross-reference written to D:\working\bdt\build\bdt\xref-bdt.html
10406 INFO: Appending 'binaries' from .spec
10410 INFO: Appending 'datas' from .spec
10416 INFO: checking PYZ
10416 INFO: Building PYZ because out00-PYZ.toc is non existent
10417 INFO: Building PYZ (ZlibArchive) D:\working\bdt\build\bdt\out00-PYZ.pyz
13580 INFO: Building PYZ (ZlibArchive) D:\working\bdt\build\bdt\out00-PYZ.pyz completed successfully.
13598 INFO: checking PKG
13599 INFO: Building PKG because out00-PKG.toc is non existent
13599 INFO: Building PKG (CArchive) out00-PKG.pkg
13827 INFO: Updating manifest in C:\Users\xiang\AppData\Roaming\pyinstaller\bincache00_py36_32bit\python36.dll
13828 INFO: Updating resource type 24 name 2 language 1033
15857 INFO: Updating manifest in C:\Users\xiang\AppData\Roaming\pyinstaller\bincache00_py36_32bit\_ssl.pyd
15860 INFO: Updating resource type 24 name 2 language 1033
15996 INFO: Updating manifest in C:\Users\xiang\AppData\Roaming\pyinstaller\bincache00_py36_32bit\pyexpat.pyd
15999 INFO: Updating resource type 24 name 2 language 1033
16145 INFO: Updating manifest in C:\Users\xiang\AppData\Roaming\pyinstaller\bincache00_py36_32bit\_hashlib.pyd
16147 INFO: Updating resource type 24 name 2 language 1033
16263 INFO: Updating manifest in C:\Users\xiang\AppData\Roaming\pyinstaller\bincache00_py36_32bit\select.pyd
16266 INFO: Updating resource type 24 name 2 language 1033
16395 INFO: Updating manifest in C:\Users\xiang\AppData\Roaming\pyinstaller\bincache00_py36_32bit\_lzma.pyd
16397 INFO: Updating resource type 24 name 2 language 1033
16505 INFO: Updating manifest in C:\Users\xiang\AppData\Roaming\pyinstaller\bincache00_py36_32bit\_socket.pyd
16508 INFO: Updating resource type 24 name 2 language 1033
16616 INFO: Updating manifest in C:\Users\xiang\AppData\Roaming\pyinstaller\bincache00_py36_32bit\_bz2.pyd
16620 INFO: Updating resource type 24 name 2 language 1033
16746 INFO: Updating manifest in C:\Users\xiang\AppData\Roaming\pyinstaller\bincache00_py36_32bit\unicodedata.pyd
16748 INFO: Updating resource type 24 name 2 language 1033
16819 INFO: Updating manifest in C:\Users\xiang\AppData\Roaming\pyinstaller\bincache00_py36_32bit\_tkinter.pyd
16819 INFO: Updating resource type 24 name 2 language 1033
17046 INFO: Updating manifest in C:\Users\xiang\AppData\Roaming\pyinstaller\bincache00_py36_32bit\tk86t.dll
17048 INFO: Updating resource type 24 name 1 language 1033
22208 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully.
22261 INFO: Bootloader c:\python\python36-32\lib\site-packages\PyInstaller\bootloader\Windows-32bit\runw.exe
22262 INFO: checking EXE
22263 INFO: Building EXE because out00-EXE.toc is non existent
22265 INFO: Building EXE from out00-EXE.toc
22361 INFO: SRCPATH [('myicon.ico', None)]
22362 INFO: Updating icons from ['myicon.ico'] to C:\Users\xiang\AppData\Local\Temp\tmpmqa8wn0w
22364 INFO: Writing RT_GROUP_ICON 0 resource with 20 bytes
22365 INFO: Writing RT_ICON 1 resource with 13760 bytes
22472 INFO: Appending archive to EXE D:\working\bdt\dist\bdt.exe
22966 INFO: Building EXE from out00-EXE.toc completed successfully.

================================================

该执行文件在64位win10下运行可以,但是在32位老旧一点的win7/8...下就不能运行。

报错信息是 找不到相应的DLL。

bdt.spec文件内容增加dll搜索目录 如下:

===================================================

# -*- mode: python -*-

block_cipher = None


a = Analysis(['bdt.py'],
             pathex=['D:\\working\\bdt','D:\\working\\bdt\\dll'],
             binaries=[('D:\\working\\bdt\\dll\*.dll','.')],
             datas=[('README.txt','.')],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name='bdt',
          debug=False,
          strip=False,
          upx=False,
          runtime_tmpdir=None,
          console=False , icon='myicon.ico')

=============================

再次运行得到:

D:\working\bdt>pyinstaller --clean bdt.spec

=============================

110 INFO: PyInstaller: 3.3.1
110 INFO: Python: 3.6.5
110 INFO: Platform: Windows-10-10.0.16299-SP0
112 INFO: UPX is not available.
113 INFO: Removing temporary files and cleaning cache in C:\Users\xiang\AppData\Roaming\pyinstaller
234 INFO: Extending PYTHONPATH with paths
['D:\\working\\bdt', 'D:\\working\\bdt', 'D:\\working\\bdt\\dll']
240 INFO: checking Analysis
243 INFO: Building Analysis because out00-Analysis.toc is non existent
244 INFO: Initializing module dependency graph...
266 INFO: Initializing module graph hooks...
284 INFO: Analyzing base_library.zip ...
5749 INFO: running Analysis out00-Analysis.toc
5755 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by c:\python\python36-32\python.exe
7113 INFO: Caching module hooks...
7126 INFO: Analyzing bdt.py
7463 INFO: Loading module hooks...
7463 INFO: Loading module hook "hook-encodings.py"...
7628 INFO: Loading module hook "hook-pydoc.py"...
7630 INFO: Loading module hook "hook-xml.py"...
8022 INFO: Loading module hook "hook-_tkinter.py"...
8254 INFO: checking Tree
8255 INFO: Building Tree because out00-Tree.toc is non existent
8257 INFO: Building Tree out00-Tree.toc
8363 INFO: checking Tree
8363 INFO: Building Tree because out01-Tree.toc is non existent
8364 INFO: Building Tree out01-Tree.toc
8419 INFO: Looking for ctypes DLLs
8419 INFO: Analyzing run-time hooks ...
8425 INFO: Including run-time hook 'pyi_rth__tkinter.py'
8437 INFO: Looking for dynamic libraries
8821 INFO: Looking for eggs
8821 INFO: Using Python library c:\python\python36-32\python36.dll
8822 INFO: Found binding redirects:
[]
8833 INFO: Warnings written to D:\working\bdt\build\bdt\warnbdt.txt
8914 INFO: Graph cross-reference written to D:\working\bdt\build\bdt\xref-bdt.html
9047 INFO: Appending 'binaries' from .spec
9050 INFO: Appending 'datas' from .spec
9062 INFO: checking PYZ
9063 INFO: Building PYZ because out00-PYZ.toc is non existent
9064 INFO: Building PYZ (ZlibArchive) D:\working\bdt\build\bdt\out00-PYZ.pyz
11207 INFO: Building PYZ (ZlibArchive) D:\working\bdt\build\bdt\out00-PYZ.pyz completed successfully.
11229 INFO: checking PKG
11229 INFO: Building PKG because out00-PKG.toc is non existent
11229 INFO: Building PKG (CArchive) out00-PKG.pkg
11361 INFO: Updating manifest in C:\Users\xiang\AppData\Roaming\pyinstaller\bincache00_py36_32bit\python36.dll
11362 INFO: Updating resource type 24 name 2 language 1033
12312 INFO: Updating manifest in C:\Users\xiang\AppData\Roaming\pyinstaller\bincache00_py36_32bit\_ssl.pyd
12312 INFO: Updating resource type 24 name 2 language 1033
12381 INFO: Updating manifest in C:\Users\xiang\AppData\Roaming\pyinstaller\bincache00_py36_32bit\unicodedata.pyd
12382 INFO: Updating resource type 24 name 2 language 1033
12459 INFO: Updating manifest in C:\Users\xiang\AppData\Roaming\pyinstaller\bincache00_py36_32bit\pyexpat.pyd
12460 INFO: Updating resource type 24 name 2 language 1033
12527 INFO: Updating manifest in C:\Users\xiang\AppData\Roaming\pyinstaller\bincache00_py36_32bit\_hashlib.pyd
12528 INFO: Updating resource type 24 name 2 language 1033
12597 INFO: Updating manifest in C:\Users\xiang\AppData\Roaming\pyinstaller\bincache00_py36_32bit\_bz2.pyd
12598 INFO: Updating resource type 24 name 2 language 1033
12656 INFO: Updating manifest in C:\Users\xiang\AppData\Roaming\pyinstaller\bincache00_py36_32bit\_lzma.pyd
12657 INFO: Updating resource type 24 name 2 language 1033
12714 INFO: Updating manifest in C:\Users\xiang\AppData\Roaming\pyinstaller\bincache00_py36_32bit\_socket.pyd
12715 INFO: Updating resource type 24 name 2 language 1033
12771 INFO: Updating manifest in C:\Users\xiang\AppData\Roaming\pyinstaller\bincache00_py36_32bit\select.pyd
12772 INFO: Updating resource type 24 name 2 language 1033
12829 INFO: Updating manifest in C:\Users\xiang\AppData\Roaming\pyinstaller\bincache00_py36_32bit\_tkinter.pyd
12830 INFO: Updating resource type 24 name 2 language 1033
12924 INFO: Updating manifest in C:\Users\xiang\AppData\Roaming\pyinstaller\bincache00_py36_32bit\tk86t.dll
12926 INFO: Updating resource type 24 name 1 language 1033
16568 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully.
16627 INFO: Bootloader c:\python\python36-32\lib\site-packages\PyInstaller\bootloader\Windows-32bit\runw.exe
16628 INFO: checking EXE
16629 INFO: Building EXE because out00-EXE.toc is non existent
16631 INFO: Building EXE from out00-EXE.toc
16746 INFO: SRCPATH [('myicon.ico', None)]
16746 INFO: Updating icons from ['myicon.ico'] to C:\Users\xiang\AppData\Local\Temp\tmpoflne3pw
16749 INFO: Writing RT_GROUP_ICON 0 resource with 20 bytes
16749 INFO: Writing RT_ICON 1 resource with 13760 bytes
16862 INFO: Appending archive to EXE D:\working\bdt\dist\bdt.exe
17373 INFO: Building EXE from out00-EXE.toc completed successfully.

==============================================

你会发现 WARNING 没有了。


 This has to do with all those warnings about missing dlls and dependencies. The following link offers a lot more information and solutions:

https://github.com/pyinstaller/pyinstaller/issues/1566

Below was my solution to the problem (taken from the above link):

1) Download and install this Windows SDK: https://developer.microsoft.com/en-us/windows/downloads/windows-10-sdk

2) These dlls can then be found here (or Program Files x86):

 C:\Program Files\Windows Kits\10\Redist\ucrt\DLLs

3) Update your pathex variable in your spec file to tell pyinstaller to look there:

pathex=['C:\\Users\\grey_hat\\Desktop\\csm\\test', 
        'C:\\Program Files (x86)\\Windows Kits\\10\\Redist\\ucrt\\DLLs\\x86',
        'C:\\Program Files (x86)\\Windows Kits\\10\\Redist\\ucrt\\DLLs\\x64']

4) run pyinstaller:

pyinstaller yourspecfile.spec

This may not be the only solution, but it is how I got it working for my setup.

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有