Cryptography module - pyinstaller. Module not found

I have a python script that uses Cryptography module. When executing the compile file (which compiles with no errors whatsoever) it halted saying the Cryptography module was not found.
However, when using python in the same virtual environment where the compile process irun Cryptography module can be imported without any issue.

Any help will be highly appreciated!

What do you mean “compiles”? What are you running here? You mean the pyinstaller?

Compile means to generate an exe standalone file that can be run out of the python environment required by the script. In other words, the said compiled file does the same of the script when double click on it.

It is not really compiling anything, though. It’s just bundling all your python code into a self-contained file and when you click on that file it’s basically running python behind the curtain. So it’s not surprising that it doesn’t detect anything wrong.

From a little googling, it looks like PyInstaller doesn’t know you’re using a virtual env for your dependencies. Did you install it in the venv, or in your global context?

Thanks for the clarification. I thought pyinstaller did a little more, as you cannot (allegedly) see the original code. However, that is not the issue here and I will research a little more on what exactly pyinstaller do. Meanwhile, thanks for the explanation, I’ll appreciate it.

I create the Venv as the first task, then install pyinstaller there as well as Cryptography, the module which is giving problems to me…

As a solution, do you suggest to install cryptography at a global level too?

Or how can I make pyinstaller where to look for is dependencies?

I’m not that familiar with how pyinstaller works exactly (I was just pretty confident it’s not a python compiler :sweat_smile: ). I suppose it creates pyc file which are sort of “compiled” to byte code, but your code is not truly hidden if you care about these things.

I only search a little bit for pyinstaller + venv but it seems like this question is common…I am surprised that it doesn’t work if you set it up as described.

Yes, I did as I said.
Just confirmed that cryptography is under venv/lib. However, since the script was working in another Mac when a begin tested it in my Mac, at first I haven’t cryptography, so, MAYBE pyinstaller (at the time of that first run) didn’t find it and switched to look into the global env (where it wasn’t either). Subsequently it looks only into the later.
I’m not saying that is logic, but offer an explanation of the observed phenomena.

  1. Is this the cryptography package that you are using?
  2. How are you importing the cryptography package?
  3. What cryptography method are you using?

I’m using Fedora Linux and I have no problem with the cryptography package and pyinstaller.

from cryptography.fernet import Fernet

key = Fernet.gernerate_key()
f = Fernet(key)

token = f.encrypt(b'Hello, World!')
(venv: Test) $ pyinstaller --onefile`
(venv: Test) $ ./dist/test
output: b'gAAAAABlATLMVsb-_fPEvX7dZzVKg_BuWsZmG_gQux9Bdo_5nle5n4yEsMwBD29qRumhGAMD3lcjbtvVAYfjEoK_djoCIhjmrQ=='


That is what I did. And it works perfectly when running the python script itself.

But when compiling the said script using pyinstaller, it somehow overlooks the cryptography module, and omit it from the created exe (one file exe). Then, the exe halted claiming that it doesn’t have the said module.

So the issue is from where pyinstaller takes the modules (I placed all of the within the dev environment for the app; that is they landed inside de Virtual Env /venv/bin).

Perhaps you could try pyinstaller --debug=imports <your script + args>.

I have to say, I’ve never used pyinstaller, but I think it’s a mistake to have students use a program like that in any introductory Python course. Imo it’s much better to just use only ‘venv’ and pip install so you (1) get a perfectly clear picture where everything is on disk and (2) don’t learn to rely on (and are not distracted by) whatever quirks and magical abilities those tools have.