Pyinstaller не работает с Pyscard на Windows
Я пытаюсь использовать pyinstaller на простой скрипт, который использует pyscard.
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 не имеют никакого значения, я попробовал 8 комбинаций с --onefile и --hidden-import и --win-private-сборки. В Ubuntu pyinstaller вызывался просто с именем скрипта и --onefile.
Примечание 2: у меня была та же проблема с Python3.6-32 на компьютере с Windows7. Я удалил все содержимое Python, которое смог найти на компьютере, и заново установил Python3.6 x86-64 и соответствующую ему карту (используя dist\pyscard-1.9.6.win-amd64-py3.6.msi из https://ci.appveyor.com/project/LudovicRousseau/pyscard)
Любая помощь, чтобы понять или исправить проблему, будет принята с благодарностью, я даже не знаю, связана ли проблема с моей установкой или Pyinstaller или Pyscard:-S
1 ответ
У меня была та же проблема в 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.
Моя среда тестирования:
- Windows 7 64 бит
- Python 3.6 32 бит
- Пискарта 1.9.7
- PyInstaller 3.3.1