How did you create the virtual environment? Did you activate it once it is created?
Sometimes, we can miss activating the environment and installing the necessary dependencies on it.
I personally use uv
for creating and managing virtual environments:
$ uv venv
$ source .venv/bin/activate
$ (venv) pip install python-gnupg==0.5.4
If you don't use uv
, you can always follow this tutorial venv — Creation of virtual environments, you'll get the same result.
I've just run that snippet of code and it worked perfectly fine within my virtual environment. Make sure you activate and install the correct gnupg
python dependency.
Regarding to the formatter of the result. What you're printing is a byte-formatted string.
In case you want a more human-readable output you can always do the following:
if encrypted_data.ok:
print("Data encrypted successfully.")
encrypted_str: str = bytes(encrypted_data.data).decode(encoding="utf-8")
with open(file=out_file, mode="w") as encrypted_file:
encrypted_file.write(encrypted_str)
print(encrypted_str)
I've just casted the encrypted_data.data
to bytes()
and decoded it in UTF-8.