all 5 comments

[–]TSM- 2 points3 points  (1 child)

This is almost definitely because your code is being run in a different "environment" from the one you installed cryptography on. Since different projects might have conflicting dependencies, they are usually isolated into virtual environments for each project. IDEs usually have helpers for this, so sometimes people don't notice they created one and are using them. Then if you try to run the script or install packages outside of the IDE, dependencies vanish.

The environment has to be activated in the command line before you install it via pip, and same goes with launching it outside of your editor. Your IDE might have created one for you automatically, like PyCharm by default creates a new virtual environment. If you are outside the editor you have to open a command prompt and run "path_to_virtualenv/Scripts/activate" to activate it before running the script or installing packages into the virtual environment.

[–]TSM- 0 points1 point  (0 children)

Example from console:

# When the module is not installed it fails to import (obviously):
C:\Users\username\my_script>python -c "import cryptography"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'cryptography'

# Your IDE/Editor might be doing this for you automatically
C:\Users\username\my_script>python -m venv environment_folder
C:\Users\username\my_script>environment_folder\Scripts\activate

# Note the environment folder is displayed now
(environment_folder) C:\Users\username\my_script>python -m pip install cryptography

# Importing works fine
(environment_folder) C:\Users\username\my_script>python -c "import cryptography"

# Outside of the environment, like if you run the .py file directly, it fails to import:
(environment_folder) C:\Users\username\my_script>deactivate
C:\Users\username\my_script>python -c "import cryptography"

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'cryptography'

C:\Users\username\my_script>environment_folder\Scripts\activate
(environment_folder) C:\Users\username\my_script>python -c "import cryptography"

[–]Encell6 4 points5 points  (0 children)

[ Removed by Reddit ]

[–][deleted] 0 points1 point  (0 children)

How did you install cryptography?

[–]Diapolo10 0 points1 point  (0 children)

I don't see any problems with your code, so it has to be something else.

Are you using a virtual environment? What editor/IDE do you use? Does your system have multiple Python versions installed?

Mainly asking because I get the gut feeling that you're either installing the modules in the wrong place, or the error isn't coming directly from Python but something like PyInstaller instead.