Search code examples
pythonpkcs#11keyerror

Module pkcs#11 from Python


I'm trying to use pkcs11 from Python and I have a problem. I had view many examples and all of them start with the same code. However, when I execute it, console raises error in the third line of the code.

Code:

import pkcs11
import os
# Initialise our PKCS#11 library
lib = pkcs11.lib(os.environ['PKCS11_MODULE'])
token = lib.get_token(token_label='DEMO')

Error in the line:

lib = pkcs11.lib(os.environ['PKCS11_MODULE'])

Error:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/UserDict.py", line 40, in __getitem__
raise KeyError(key)
KeyError: 'PKCS11_MODULE'

Solution

  • pkcs11 wraps a native library. It expects to find that library in a path given by the PKCS11_MODULE environment variable.

    You should set that environment variable to where you have installed the native component.

    $ export PKCS11_MODULE='/some/path'
    $ python myscript.py