0

If I call "generateKey" from the script below, everything works fine and the keyfile will be generated. But if I call "cryptkey" the error No module named 'cryptography' will occur.

import cryptography.fernet

def generateKey(keyfile : str):
    key = cryptography.fernet.Fernet.generate_key()

    with open (keyfile, "bw") as fp:
        fp.write (key)

def cryptkey(stringData : str, keyfile : str) -> str:
    symkey = None

    with open (keyfile,"r") as fp:
        symkey = fp.read()

    crypter      = cryptography.fernet.Fernet(symkey)
    crypted_text = crypter.encrypt(stringData.encode())

    return(crypted_text.decode())

I'm working in a virtual environment, with this modules installed:

(venv) E:\Projekte\...>pip list
Package            Version
------------------ ---------
artifactory        0.1.17
certifi            2023.7.22
cffi               1.15.1
charset-normalizer 3.2.0
coverage           7.2.7
cryptography       41.0.3
idna               3.4
oracledb           1.3.2
pathlib            1.0.1
pip                23.2.1
pycparser          2.21
python-dateutil    2.8.2
requests           2.31.0
setuptools         65.5.0
six                1.16.0
urllib3            2.0.4
xmltodict          0.13.0
        
My python-version is
        
(venv) E:\Projekte\...>python --version
    
Python 3.10.11

What did I tried:

  • Reinstalled the module
  • Removed all "pycache" folders
  • Rebooted my computer
  • Removed and reinstalled the virtual environment

Commands and output:

(venv) E:\Projekte...>python -c "import keyFunctions.keyFunctions as kf; kf.generateKey('keyfile.txt')"

(venv) E:\Projekte...>python3 -c "import keyFunctions.keyFunctions as kf; print(kf.cryptkey('blubb', 'keyfile.txt'));"

Traceback (most recent call last): File "", line 1, in File "E:\Projekte...\keyFunctions\keyFunctions.py", line 1, in from cryptography import fernet ModuleNotFoundError: No module named 'cryptography'

2 Answers2

0

By any chance you are using PyCharm? You could try adding and/or switching your interpreter. It's located at the bottom left. Left of your current working git branch.

Configure a virtual environment in PyCharm

I had a somewhat similar issue where a package I imported was not found although in my terminal I see my virtual environment was set to (venv), which was the virtual environment that had this package installed into. Even did a pip list just like you and it was there. I ran the script on my terminal and it worked fine but directly running it on PyCharm by right clicking the file and running did not work.

If that did not help, try running the script via the terminal with the correct virtual environment active and see if that works.

  • I'm using Visual Studio as my development environment. It does not work in interactive-windows, and as it doesn't work in a freshly openend terminal window. Do you think PyCharm could help? – Wolfgang Ebert Aug 11 '23 at 08:05
  • Hmm I'm unsure. I won't promise you changing your IDE to PyCharm could help. But you're always welcomed to try. Strange issue. The only thing I could think of next is to truly check if you're in the correct virtual environment, which I really doubt that you are not. Could you try `which python` in your terminal and check if the output is showing the correct directory? – thamodrensinniah Aug 11 '23 at 08:34
  • where python (because I'm developing under Windows) shows the right output: (venv) E:\Projekte\...>where python E:\Projekte\\...\venv\Scripts\python.exe – Wolfgang Ebert Aug 11 '23 at 09:00
  • Could you help me add the full error log either here or in your question above? Could be helpful to me and other people watching this viewing this question. – thamodrensinniah Aug 11 '23 at 10:13
  • Thx for hint. Added in my question – Wolfgang Ebert Aug 11 '23 at 12:48
0

The solution is really simple: the problem sits in front of the computer. It was the call of python3, which calls the python-executable outside the virtual environment, instead the python inside the venv. And because I'm not installing any modules in the global environment, the missing module couldn't be found!

Thx to @bereal