#993574 python3-virtualsmartcard: Cannot import virtualsmartcard module

#993574#5
Date:
2021-09-03 09:17:55 UTC
From:
To:
Dear Maintainer,

after installing the python3-virtualsmartcard package, I cannot import
it with python3:

    $ python3
    Python 3.9.2 (default, Feb 28 2021, 17:03:44)
    [GCC 10.2.1 20210110] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import virtualsmartcard
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ModuleNotFoundError: No module named 'virtualsmartcard'

As far as I see, this is because the package is installed into
/usr/lib/python3/site-packages instead of <..>/dist-packages:

    $ apt-file show python3-virtualsmartcard | grep __init__.py
    python3-virtualsmartcard: /usr/lib/python3/site-packages/virtualsmartcard/virtualsmartcard/__init__.py
    python3-virtualsmartcard: /usr/lib/python3/site-packages/virtualsmartcard/virtualsmartcard/cards/__init__.py

    $ python3 -m site
    sys.path = [
        '/home/robin',
        '/usr/lib/python39.zip',
        '/usr/lib/python3.9',
        '/usr/lib/python3.9/lib-dynload',
        '/usr/local/lib/python3.9/dist-packages',
        '/usr/lib/python3/dist-packages',
    ]
    USER_BASE: '/home/robin/.local' (exists)
    USER_SITE: '/home/robin/.local/lib/python3.9/site-packages' (doesn't exist)
    ENABLE_USER_SITE: True

If I manually put that folder in the Python path, for example by linking
/usr/lib/python3/site-packages/virtualsmartcard/virtualsmartcard to
~/.local/lib/python3.9/site-package/virtualsmartcard, I can at least
import the virtualsmartcard module.  But there seem to be missing
dependencies:

    >>> import virtualsmartcard.VirtualSmartcard
    Traceback (most recent call last):
      File "/home/robin/.local/lib/python3.9/site-packages/virtualsmartcard/CryptoUtils.py", line 29, in <module>
        from Crypto.Cipher import DES3, DES, AES, ARC4  # @UnusedImport
    ModuleNotFoundError: No module named 'Crypto'

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/home/robin/.local/lib/python3.9/site-packages/virtualsmartcard/VirtualSmartcard.py", line 31, in <module>
        from virtualsmartcard.CardGenerator import CardGenerator
      File "/home/robin/.local/lib/python3.9/site-packages/virtualsmartcard/CardGenerator.py", line 34, in <module>
        from virtualsmartcard.SmartcardSAM import SAM
      File "/home/robin/.local/lib/python3.9/site-packages/virtualsmartcard/SmartcardSAM.py", line 24, in <module>
        import virtualsmartcard.CryptoUtils as vsCrypto
      File "/home/robin/.local/lib/python3.9/site-packages/virtualsmartcard/CryptoUtils.py", line 35, in <module>
        import sha as SHA1
    ModuleNotFoundError: No module named 'sha'

#993574#10
Date:
2021-09-03 14:20:50 UTC
From:
To:
Hi,

It seems very logical to me that it cannot import virtualsmartcard if
it's in site-packages and not dist-packages.
I'll have a look at the debian rules to patch it and upload it here, or
if Philippe allows me, package it and have it uploaded by my mentor
nicoo.

Best regards,

#993574#15
Date:
2021-09-06 08:19:26 UTC
From:
To:
Hi Guinness,

It is indeed an error in the rules file. If you already have the patch ready, your can upload a NMU release if you wish.
If not, I can handle it during the current week.

Keep me informed of what you prefer :-)

Best regards,

#993574#20
Date:
2022-07-23 22:08:32 UTC
From:
To:
Hi,

is there any chance to get a fixed package released sometime soon?

The following change fixed this for me in a local rebuild of the
package, but there might be a better way to handle it:

diff --git a/debian/python3-virtualsmartcard.install
b/debian/python3-virtualsmartcard.install
index 47ee4d3..ded283b 100644
--- a/debian/python3-virtualsmartcard.install
+++ b/debian/python3-virtualsmartcard.install
@@ -1 +1 @@
-usr/lib/python3.*/site-packages/virtualsmartcard 
/usr/lib/python3/site-packages/virtualsmartcard
+usr/lib/python3.*/site-packages/virtualsmartcard
usr/lib/python3/dist-packages/


Side note: With this in place, I still ran into another problem,
described in upstream issue
https://github.com/frankmorgner/vsmartcard/issues/218
("ModuleNotFoundError: No module named 'sha'").
With a logical backport of the suggested upstream PR
https://github.com/frankmorgner/vsmartcard/pull/228 , running `vicc`
then works without errors for me. Backport in the patch-queue branch of
my local package looks like this:

diff --git a/virtualsmartcard/src/vpicc/virtualsmartcard/CryptoUtils.py
b/virtualsmartcard/src/vpicc/virtualsmartcard/CryptoUtils.py
index 56e0ed4..552cff1 100644
--- a/virtualsmartcard/src/vpicc/virtualsmartcard/CryptoUtils.py
+++ b/virtualsmartcard/src/vpicc/virtualsmartcard/CryptoUtils.py
@@ -27,12 +27,11 @@ from virtualsmartcard.utils import inttostring
  try:
      # Use PyCrypto (if available)
      from Crypto.Cipher import DES3, DES, AES, ARC4  # @UnusedImport
-    from Crypto.Hash import HMAC, SHA as SHA1
+    from Crypto.Hash import HMAC

  except ImportError:
      # PyCrypto not available.  Use the Python standard library.
      import hmac as HMAC
-    import sha as SHA1

  CYBERFLEX_IV = b'\x00' * 8


Best regards,
Michael

#993574#25
Date:
2022-08-08 20:47:00 UTC
From:
To:
FWIW, that upstream pull request has been merged now.
#993574#30
Date:
2023-02-16 13:09:06 UTC
From:
To:
I'm forwarding the debdiff I submitted to
https://launchpad.net/bugs/2007568 in hope that it will be useful.