I am trying to use pyinstaller on a simple script which use 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')
This is OK on Ubuntu but fails on Windows7 and Windows10 machines. The failure is the same on both versions of windows, here is the screen output on 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
Note: The optional arguments to pyinstaller do not make any difference, I tried the 8 combinations with and without --onefile, --hidden-import and --win-private-assemblies. On ubuntu pyinstaller was invoked simply with the name of the script and --onefile.
Note 2: I had the same issue with Python3.6-32 on the Windows7 machine. I uninstalled all the python stuff I could find on the machine and made a fresh install of Python3.6 x86-64 and the matching pyscard (using dist\pyscard-1.9.6.win-amd64-py3.6.msi from https://ci.appveyor.com/project/LudovicRousseau/pyscard)
Any help to understand or fix the issue would be greatly appreciated, I don't even know if the problem is related to my setup or Pyinstaller or Pyscard :-S
I had same issue on Windows 7 and Windows 10. I think the problems occurs because of the runtime import in the swig_import_helper function in scard.py. So I have added following to my PyInstaller options:
--add-data "path_to_python\\Python36-32\\Lib\\site-packages\\smartcard\\scard\\_scard.cp36-win32.pyd;.\\smartcard\\scard\\"
This fixed the problem for me both under Windows 7 and Windows 10.
My testing environment: