我正在尝试在使用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:UserssebownCloudrex>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:UserssebownCloudrextestpyscard.spec
60 INFO: UPX is not available.
60 INFO: Removing temporary files and cleaning cache in C:UserssebAppDataRoamingpyinstaller
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:Python36python.exe
3106 INFO: Caching module hooks...
3111 INFO: Analyzing C:UserssebownCloudrextestpyscard.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:Python36python36.dll
3462 INFO: Found binding redirects:
[]
3465 INFO: Warnings written to C:UserssebownCloudrexbuildtestpyscardwarntestpyscard.txt
3501 INFO: Graph cross-reference written to C:UserssebownCloudrexbuildtestpyscardxref-testpyscard.html
3506 INFO: checking PYZ
3507 INFO: Building PYZ because out00-PYZ.toc is non existent
3507 INFO: Building PYZ (ZlibArchive) C:UserssebownCloudrexbuildtestpyscardout00-PYZ.pyz
3910 INFO: Building PYZ (ZlibArchive) C:UserssebownCloudrexbuildtestpyscardout00-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:UserssebAppDataRoamingpyinstallerbincache00_py36_64bitpython36.dll
3930 INFO: Updating resource type 24 name 2 language 1033
4036 INFO: Updating manifest in C:UserssebAppDataRoamingpyinstallerbincache00_py36_64bit_ssl.pyd
4036 INFO: Updating resource type 24 name 2 language 1033
4047 INFO: Updating manifest in C:UserssebAppDataRoamingpyinstallerbincache00_py36_64bitunicodedata.pyd
4048 INFO: Updating resource type 24 name 2 language 1033
4055 INFO: Updating manifest in C:UserssebAppDataRoamingpyinstallerbincache00_py36_64bitpyexpat.pyd
4056 INFO: Updating resource type 24 name 2 language 1033
4066 INFO: Updating manifest in C:UserssebAppDataRoamingpyinstallerbincache00_py36_64bit_hashlib.pyd
4066 INFO: Updating resource type 24 name 2 language 1033
4075 INFO: Updating manifest in C:UserssebAppDataRoamingpyinstallerbincache00_py36_64bitselect.pyd
4076 INFO: Updating resource type 24 name 2 language 1033
4083 INFO: Updating manifest in C:UserssebAppDataRoamingpyinstallerbincache00_py36_64bit_bz2.pyd
4083 INFO: Updating resource type 24 name 2 language 1033
4089 INFO: Updating manifest in C:UserssebAppDataRoamingpyinstallerbincache00_py36_64bit_lzma.pyd
4089 INFO: Updating resource type 24 name 2 language 1033
4095 INFO: Updating manifest in C:UserssebAppDataRoamingpyinstallerbincache00_py36_64bit_socket.pyd
4095 INFO: Updating resource type 24 name 2 language 1033
4100 INFO: Updating manifest in C:UserssebAppDataRoamingpyinstallerbincache00_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:Python36libsite-packagesPyInstallerbootloaderWindows-64bitrun.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:UserssebownCloudrexdisttestpyscard.exe
5499 INFO: Building EXE from out00-EXE.toc completed successfully.
C:UserssebownCloudrex>.disttestpyscard.exe
Traceback (most recent call last):
File "testpyscard.py", line 28, in <module>
from smartcard.Exceptions import NoCardException
File "C:Python36libsite-packagesPyInstallerloaderpyimod03_importers.py", line 631, in exec_module
exec(bytecode, module.__dict__)
File "site-packagessmartcard__init__.py", line 29, in <module>
File "C:Python36libsite-packagesPyInstallerloaderpyimod03_importers.py", line 631, in exec_module
exec(bytecode, module.__dict__)
File "site-packagessmartcardSystem.py", line 28, in <module>
File "C:Python36libsite-packagesPyInstallerloaderpyimod03_importers.py", line 631, in exec_module
exec(bytecode, module.__dict__)
File "site-packagessmartcardreaderReaderFactory.py", line 34, in <module>
File "C:Python36libsite-packagesPyInstallerloaderpyimod03_importers.py", line 631, in exec_module
exec(bytecode, module.__dict__)
File "site-packagessmartcardpcscPCSCReader.py", line 28, in <module>
File "C:Python36libsite-packagesPyInstallerloaderpyimod03_importers.py", line 631, in exec_module
exec(bytecode, module.__dict__)
File "site-packagessmartcardpcscPCSCContext.py", line 27, in <module>
File "C:Python36libsite-packagesPyInstallerloaderpyimod03_importers.py", line 631, in exec_module
exec(bytecode, module.__dict__)
File "site-packagessmartcardscard__init__.py", line 1, in <module>
File "C:Python36libsite-packagesPyInstallerloaderpyimod03_importers.py", line 631, in exec_module
exec(bytecode, module.__dict__)
File "site-packagessmartcardscardscard.py", line 119, in <module>
File "site-packagessmartcardscardscard.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 的可选参数没有任何区别,我尝试了 8 种组合,有和没有 --onefile、--hidden-import 和 --win-private-assemblies。在 ubuntu 上,pyinstaller 被简单地调用,只需使用脚本的名称和 --onefile。
注意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
我在Windows7和Windows 10上遇到了同样的问题。我认为出现问题是由于scard.py中swig_import_helper函数中的运行时导入。所以我在我的 PyInstaller 选项中添加了以下内容:
--add-data "path_to_python\Python36-32\Lib\site-packages\smartcard\scard\_scard.cp36-win32.pyd;.\smartcard\scard\"
这为我在Windows 7和Windows 10下解决了问题。
我的测试环境:
- 视窗 7 64 位
- 蟒蛇 3.6 32 位
- 皮斯卡 1.9.7
- PyInstaller 3.3.1