Pyinstaller 在 windows 上出现 pyscard 失败

Pyinstaller fails with pyscard on windows

我正在尝试在使用 pyscard 的简单脚本上使用 pyinstaller。

from smartcard.Exceptions import NoCardException
from smartcard.System import readers
from smartcard.util import toHexString

for reader in readers():
    try:
        connection = reader.createConnection()
        connection.connect()
        print(reader, toHexString(connection.getATR()))
    except NoCardException:
        print(reader, 'no card inserted')

这在 Ubuntu 上没问题,但在 Windows7 和 Windows10 机器上失败。 windows 的两个版本上的失败是相同的,这里是 Windows7 上的屏幕输出:

C:\Users\seb\ownCloud\rex>pyinstaller --clean --win-private-assemblies --hidden-import=smartcard.scard._scard --onefile testpyscard.py
55 INFO: PyInstaller: 3.3.1
55 INFO: Python: 3.6.5
56 INFO: Platform: Windows-7-6.1.7601-SP1
57 INFO: wrote C:\Users\seb\ownCloud\rex\testpyscard.spec
60 INFO: UPX is not available.
60 INFO: Removing temporary files and cleaning cache in C:\Users\seb\AppData\Roaming\pyinstaller
74 INFO: Extending PYTHONPATH with paths
['C:\Users\seb\ownCloud\rex', 'C:\Users\seb\ownCloud\rex']
75 INFO: checking Analysis
76 INFO: Building Analysis because out00-Analysis.toc is non existent
76 INFO: Initializing module dependency graph...
79 INFO: Initializing module graph hooks...
81 INFO: Analyzing base_library.zip ...
2472 INFO: Analyzing hidden import 'smartcard.scard._scard'
2564 INFO: running Analysis out00-Analysis.toc
2566 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by C:\Python36\python.exe
3106 INFO: Caching module hooks...
3111 INFO: Analyzing C:\Users\seb\ownCloud\rex\testpyscard.py
3115 INFO: Loading module hooks...
3115 INFO: Loading module hook "hook-encodings.py"...
3195 INFO: Loading module hook "hook-pydoc.py"...
3196 INFO: Loading module hook "hook-xml.py"...
3382 INFO: Looking for ctypes DLLs
3383 INFO: Analyzing run-time hooks ...
3388 INFO: Looking for dynamic libraries
3461 INFO: Looking for eggs
3461 INFO: Using Python library C:\Python36\python36.dll
3462 INFO: Found binding redirects:
[]
3465 INFO: Warnings written to C:\Users\seb\ownCloud\rex\build\testpyscard\warntestpyscard.txt
3501 INFO: Graph cross-reference written to C:\Users\seb\ownCloud\rex\build\testpyscard\xref-testpyscard.html
3506 INFO: checking PYZ
3507 INFO: Building PYZ because out00-PYZ.toc is non existent
3507 INFO: Building PYZ (ZlibArchive) C:\Users\seb\ownCloud\rex\build\testpyscard\out00-PYZ.pyz
3910 INFO: Building PYZ (ZlibArchive) C:\Users\seb\ownCloud\rex\build\testpyscard\out00-PYZ.pyz completed successfully.
3914 INFO: checking PKG
3914 INFO: Building PKG because out00-PKG.toc is non existent
3915 INFO: Building PKG (CArchive) out00-PKG.pkg
3929 INFO: Updating manifest in C:\Users\seb\AppData\Roaming\pyinstaller\bincache00_py36_64bit\python36.dll
3930 INFO: Updating resource type 24 name 2 language 1033
4036 INFO: Updating manifest in C:\Users\seb\AppData\Roaming\pyinstaller\bincache00_py36_64bit\_ssl.pyd
4036 INFO: Updating resource type 24 name 2 language 1033
4047 INFO: Updating manifest in C:\Users\seb\AppData\Roaming\pyinstaller\bincache00_py36_64bit\unicodedata.pyd
4048 INFO: Updating resource type 24 name 2 language 1033
4055 INFO: Updating manifest in C:\Users\seb\AppData\Roaming\pyinstaller\bincache00_py36_64bit\pyexpat.pyd
4056 INFO: Updating resource type 24 name 2 language 1033
4066 INFO: Updating manifest in C:\Users\seb\AppData\Roaming\pyinstaller\bincache00_py36_64bit\_hashlib.pyd
4066 INFO: Updating resource type 24 name 2 language 1033
4075 INFO: Updating manifest in C:\Users\seb\AppData\Roaming\pyinstaller\bincache00_py36_64bit\select.pyd
4076 INFO: Updating resource type 24 name 2 language 1033
4083 INFO: Updating manifest in C:\Users\seb\AppData\Roaming\pyinstaller\bincache00_py36_64bit\_bz2.pyd
4083 INFO: Updating resource type 24 name 2 language 1033
4089 INFO: Updating manifest in C:\Users\seb\AppData\Roaming\pyinstaller\bincache00_py36_64bit\_lzma.pyd
4089 INFO: Updating resource type 24 name 2 language 1033
4095 INFO: Updating manifest in C:\Users\seb\AppData\Roaming\pyinstaller\bincache00_py36_64bit\_socket.pyd
4095 INFO: Updating resource type 24 name 2 language 1033
4100 INFO: Updating manifest in C:\Users\seb\AppData\Roaming\pyinstaller\bincache00_py36_64bit\_scard.pyd
4100 INFO: Updating resource type 24 name 2 language 1033
5488 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully.
5490 INFO: Bootloader C:\Python36\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
5491 INFO: checking EXE
5491 INFO: Building EXE because out00-EXE.toc is non existent
5492 INFO: Building EXE from out00-EXE.toc
5493 INFO: Appending archive to EXE C:\Users\seb\ownCloud\rex\dist\testpyscard.exe
5499 INFO: Building EXE from out00-EXE.toc completed successfully.

C:\Users\seb\ownCloud\rex>.\dist\testpyscard.exe
Traceback (most recent call last):
  File "testpyscard.py", line 28, in <module>
    from smartcard.Exceptions import NoCardException
  File "C:\Python36\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\smartcard\__init__.py", line 29, in <module>
  File "C:\Python36\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\smartcard\System.py", line 28, in <module>
  File "C:\Python36\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\smartcard\reader\ReaderFactory.py", line 34, in <module>
  File "C:\Python36\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\smartcard\pcsc\PCSCReader.py", line 28, in <module>
  File "C:\Python36\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\smartcard\pcsc\PCSCContext.py", line 27, in <module>
  File "C:\Python36\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\smartcard\scard\__init__.py", line 1, in <module>
  File "C:\Python36\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\smartcard\scard\scard.py", line 119, in <module>
  File "site-packages\smartcard\scard\scard.py", line 118, in swig_import_helper
  File "importlib\__init__.py", line 126, in import_module
ModuleNotFoundError: No module named 'smartcard.scard._scard'
[7924] Failed to execute script testpyscard

注意:pyinstaller 的可选参数没有任何区别,我尝试了有和没有 --onefile、--hidden-import 和 --win-private-assemblies 的 8 种组合。在 ubuntu 上,仅使用脚本名称和 --onefile 调用了 pyinstaller。

注意 2:我在 Windows7 机器上对 Python3.6-32 也有同样的问题。我卸载了我能在机器上找到的所有 python 东西,并重新安装了 Python3.6 x86-64 和匹配的 pyscard(使用 dist\pyscard-1.9.6.win-amd64-py3.6.msi 来自 https://ci.appveyor.com/project/LudovicRousseau/pyscard)

如果能帮助我理解或解决问题,我将不胜感激,我什至不知道问题是否与我的设置或 Pyinstaller 或 Pyscard 有关:-S

我在 Windows 7 和 Windows 10 上遇到了同样的问题。我认为问题的发生是因为 swig_import_helper[=24= 中的运行时导入] scard.py 中的函数。所以我在我的 PyInstaller 选项中添加了以下内容:

--add-data "path_to_python\Python36-32\Lib\site-packages\smartcard\scard\_scard.cp36-win32.pyd;.\smartcard\scard\"

这解决了我在 Windows 7 和 Windows 10 下的问题。

我的测试环境:

  • Windows 7 64 位
  • Python 3.6 32 位
  • pyscard 1.9.7
  • PyInstaller 3.3.1