LudovicRousseau / pyscard

pyscard smartcard library for python
http://pyscard.sourceforge.net/
GNU Lesser General Public License v2.1
379 stars 108 forks source link

Partially initialized module 'smartcard.scard' after installing from sources under mingw64 #108

Closed kristinux closed 3 years ago

kristinux commented 3 years ago

Installing pyscard using pip under msys2/mingw64 produces an enormous trace, hence I resolved to building and installing from sources.

To build I added one more condition to setup.py that would trigger similar parameters as for cygwin (if get_platform() contains 'mingw' check).

Once built and installed, I ran python interpreter and executed simple import: from smartcard import System This statement resulted in: Python 3.8.8 (default, Feb 20 2021, 07:16:03) [GCC 10.2.0 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information.

from smartcard import System Traceback (most recent call last): File "", line 1, in File "C:/msys64/mingw64/lib/python3.8/pyscard-2.0.0/smartcard/init.py", line 29, in from smartcard.System import listReaders File "C:/msys64/mingw64/lib/python3.8/pyscard-2.0.0/smartcard/System.py", line 28, in import smartcard.reader.ReaderFactory File "C:/msys64/mingw64/lib/python3.8/pyscard-2.0.0/smartcard/reader/ReaderFactory.py", line 34, in from smartcard.pcsc.PCSCReader import PCSCReader File "C:/msys64/mingw64/lib/python3.8/pyscard-2.0.0/smartcard/pcsc/PCSCReader.py", line 28, in from smartcard.pcsc.PCSCContext import PCSCContext File "C:/msys64/mingw64/lib/python3.8/pyscard-2.0.0/smartcard/pcsc/PCSCContext.py", line 27, in from smartcard.scard import File "C:/msys64/mingw64/lib/python3.8/pyscard-2.0.0/smartcard/scard/init.py", line 1, in from smartcard.scard.scard import File "C:/msys64/mingw64/lib/python3.8/pyscard-2.0.0/smartcard/scard/scard.py", line 114, in from . import _scard ImportError: cannot import name '_scard' from partially initialized module 'smartcard.scard' (most likely due to a circular import) (C:/msys64/mingw64/lib/python3.8/pyscard-2.0.0/smartcard/scard/init.py)

Any suggestions to resolve this are greatly appreciated.

LudovicRousseau commented 3 years ago

The Python wrapper is in 2 parts: a Python part and a native code part. The error you see is because the Python part cannot load the native part. It should be a file called _scard.cp38-win_amd64.pyd (or something like that) on Windows.

Depending on the Python environment you use you can have or not this problem. On Windows I am using miniconda and it works for me. I also tried "standard" python but I had the problem you report. But not always.

If you find the problem and can propose a solution that would be perfect. I am not a Windows user.

kristinux commented 3 years ago

Thank you for the response. I come from Linux background, all Windows issues are extremely foreign. So far I am using MSYS2/MINGW64 combination with python installed from mingw repositories. I have successfully built pyscard once in the same environment but with Python 3.8.7. I was forced to upgrade to 3.8.8 and that is where the issues started.

  1. I will look for the file you have mentioned.
  2. I will see if I can compare 3.8.7 and 3.8.8 based builds.
kristinux commented 3 years ago

@LudovicRousseau I am not quite sure what has happened on my system. But perhaps after a few rebuilds I got the library working! I noticed that after a day of work and a reboot of my system.

At the moment my issue could be closed as "cannot reproduce", at least for now. I could also commit the minimalistic change I added to setup.py to get it built under msys2/mingw64.

LudovicRousseau commented 3 years ago

Please share your setup.py changes with a Pull Request. I will have a look.