我试图在一个使用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机器上失败。在两个版本的窗口上,失败都是相同的,下面是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的可选参数没有任何区别,我尝试了8种组合,包括和不带--onefile,-隐藏-导入和--双赢的私有程序集。在上,只使用脚本的名称和-onefile调用。
注2:在Windows7机器上,Python3.6-32也有同样的问题。我卸载了机器上可以找到的所有python东西,并重新安装了Python3.6x86-64和匹配的pyscard (使用https://ci.appveyor.com/project/LudovicRousseau/pyscard中的dist\pyscard-1.9.6.win- and 64-py3.6.msi)
如果能帮助理解或解决这个问题,我将不胜感激,我甚至不知道问题是否与我的安装程序或Pyinstaller或Pyscard有关:-S
发布于 2018-07-08 11:22:38
我在Windows 7和Windows 10上也有同样的问题,我认为问题的出现是因为swig_import_helper函数在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下的问题。
我的测试环境:
https://stackoverflow.com/questions/49718551
复制相似问题