Crypto Module not Found for pyinstaller - python

I want to turn my Python script into a single executable with all the dependencies in place. For this purpose I am using pyinstaller. I have installed it globally on my machine and set up a virtualenv for my project. While the virtualenv is active I am executing the following command:
pyinstaller --onefile --paths /path/to/venv/lib/python3.9/site-packages script.py
This creates an executable but when I try to run it it gives me:
ModuleNotFoundError: No module named 'Crypto.Util.Padding'
In the warning file it gives me the following line:
missing module named 'Crypto.Util.Padding' - imported by sp_api.base.helpers (top-level)
And during executable generation the output includes:
6427 INFO: Loading module hook 'hook-Crypto.py' from '/home/user/.local/lib/python3.9/site-packages/_pyinstaller_hooks_contrib/hooks/stdhooks'...
Traceback (most recent call last):
File "<string>", line 2, in <module>
ModuleNotFoundError: No module named 'Crypto.Math'
When executing the command
pyinstaller --onefile myscript.py
under windows, it works perfectly fine even without specifying the venv path. When performing the same step in linux, the executable fails with the first non-standard import (flask in this case).

After also adding the path for lib64 from the corresponding venv the Crypto library is found.
pyinstaller --onefile --paths /path/to/venv/lib/python3.9/site-packages \
--paths /path/to/venv/lib64/python3.9/site-packagesscript.py

Related

(Pyinstaller) 'tclError: can't find package tkdnd' How can I fix this error?

I'm trying to package my project into an executable file using pyinstaller. main.py, my program, should run when I run the exe file. Exe because program has to run somehow without the user, who is not a developer in any way, needing to install modules and python itself. If there's a better/easier way please do tell me.
i installed tkinterdnd2 by using pip install tkinterdnd2.
Not sure if this is necessary, but here is the directory of the program: (the pngs are only used for my program, I don't think that is the culprit):
Folder
- cardiologist.png
- img.png
.
. # a couple more pngs
.
- main.py
- patients' data csv.csv
- patients' data xlsx test.xlsx
- sample.xlsx
- sun-valley.tcl #this file and the 'theme' folder below are for my program's theme, also don't think these are the culprit
- theme
- dark
- a lot of pngs...
- dark.tcl
- light
- a lot of pngs...
- light.tcl
Pyinstaller created 2 files, build and dist as expected.
build
- main
- base_library.zip
- certifi
- IPython
- jedi
- (some other files)
.
. a lot of .dylib files
.
- main # exe file to execute to run program
- matplotlib (this and next 4 are folders)
- numpy
- pandas
- parso
- PIL
- Python # exe file, don't know what this was created for
- (some more folders)
- tcl (this and next 2 are folders)
- tcl8
- 8.4
- platform
- .tm file
- .tm file
- 8.5
- 2 .tm files
- 8.6
- .tm file
- tdbc
- .tm file
- tkdnd2.8 (tried renaming to just tkdnd but same error)
- tk
- ttkwidgets
- 2 other folders
The command I used:
python -m PyInstaller main.py. # without python -m, 'command not found error' would happen
Error when running the produced exe:
Traceback (most recent call last):
File "tkinterdnd2/TkinterDnD.py", line 53, in _require
_tkinter.TclError: can't find package tkdnd
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "main.py", line 16, in <module>
File "tkinterdnd2/TkinterDnD.py", line 285, in __init__
File "tkinterdnd2/TkinterDnD.py", line 55, in _require
RuntimeError: Unable to load tkdnd library.
[7015] Failed to execute script 'main' due to unhandled exception!
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
I've went on the internet and tried looking for answers, but they either did not work for me, or wasn't clear enough like this: *.py script with tkinterdnd2 module doesn`t compile to an executable file (this solution still gave me the same error).
I've also tried using other modules to package such as cx_freeze and py2app, but both produced a different error, so I went back to pyinstaller for now. I'm on macOS.
Any way to fix this error?
EDIT
also tried python -m PyInstaller --clean -y -n "output_name" --add-data="tkdnd:tkdnd" main.py but same error produced when running.
There is a section on PyInstaller in tkinterdnd2 website.
Basically the required steps are:
copy hook-tkinterdnd2.py from the website to your project folder or create that file with below content:
from PyInstaller.utils.hooks import collect_data_files
datas = collect_data_files('tkinterdnd2')
run PyInstaller as below:
pyinstaller -F main.py --additional-hooks-dir=.
Or based on the command in your question:
python -m PyInstaller -F main.py --additional-hooks-dir=.
I finally made it work without manually moving folders.
pip install tkinterdnd2
I used auto py to exe and it didn't work with the "--add-binary" so I used "add-folder" in the gui, which is basically just "--add-data".
pyinstaller --noconfirm --onefile --windowed --add-data "<python_path>/Lib/site-packages/tkinterdnd2;tkinterdnd2/" "<your_script>"
The issue here is that Drag n Drop requires two components: the TCL libraries and the Tkinter interface to them. I install both manually to config my environment. (See my answer to How to Install and Use TkDnD with Python Tkinter on OSX?). I know someone packaged something on PyPI for the TkinterDnD2, but I haven't looked into it.
I have a project which uses TkinterDnD2. I build it with PyInstaller and see the same error you see (more or less). Running PyInstaller with the --onedir option (rather than the --onefile option), I saw that tkdnd2.8 was missing from my dist directory.
To rectify this, on Windows, I added
--add-binary "C:/Python/Python38-32/tcl/tkdnd2.8;tkdnd2.8"
to the PyInstaller command line, and that did the trick. I can now build a --onefile executable, and it runs without error.
You tried something similar, but I used the --add-binary rather than --add-data, and I give the full path to the libraries.
I'm not sure about the first hand experience of the other answers, but I personally distribute tkdnd in my applications. Granted, I first did this before tkinterdnd2 was available in PyPi. I used both the tkdnd distribution along with the python wrapper.
In my build script, I have
copy_tree('build_files/tkdnd2.9.2', os.path.dirname(sys.executable) + '/tcl/tkdnd2.9.2')
copy_tree('build_files/TkinterDnD2', os.path.dirname(sys.executable) + '/Lib/site-packages/TkinterDnD2')
And in my spec file,
tkdnd = [(os.path.abspath(file), 'tkdnd2.9.2') for file in iglob('build_files/tkdnd2.9.2/*.*')]
data_files = [...] + tkdnd
a = Analysis9(..., datas=data_files,...)
You can further take a look at the my project, Music Caster

PyInstaller SSL error with requests module - missing module ssl (_ssl)

In my python script, I use the requests module to call an API to GET and POST data.
Python environment: anaconda3, python 3.7 / packages: requests 2.24.0, pyinstaller 3.6, openssl 1.1.1h...
I have the following problem with the EXE-file generated by PyInstaller: When I run this file, I get the following error message. This error doesn't occur when I run my script from Python:
Traceback (most recent call last):
File "site-packages\PyInstaller\loader\rthooks\pyi_rth_certifi.py", line 13, in <module>
File "c:\programdata\anaconda3\envs\...\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
File "ssl.py", line 98, in <module>
ImportError: DLL load failed: The specified module could not be found.
[20188] Failed to execute script pyi_rth_certifi
If I follow to the excepted error lines (ssl.py):
import _ssl # if we can't import it, let the error propagate
Why can't ssl be imported?
I searched a long time in serveral post on SO but all these answers didn't help, ex.:
Python Requests throwing SSLError
Python Requests - How to use system ca-certificates (debian/ubuntu)?
Twilio Python Module Errors After Compiling
Fixing SSL certificate error in exe compiled with py2exe (or PyInstaller)
Does anyone have an idea how to fix this?
If you need more informations, please write a comment. THX
EDIT: (for Mooncrater's comment)
added Screenshot from python console, entered:
import _ssl
EDIT 2:
Tested "FIX" from SO Question:
Python SSL Import Error in PyInstaller generated executable
-> this didn't fix my problem
EDIT 3: Answer by cbolwerk
THX for your answer, this works!
Python 3.7 anaconda environment - import _ssl DLL load fail error
An additional fix is to install the latest OpenSSL Lib, I used 1.1.1h package in my python script, on my PC was installed an older version and that causes the error too.
cbolwerk's answer I tested on a PC where NO OpenSSL is installed and , as I wrote, it works!
If you can import ssl in the python within your environment, it means that the ssl module is probably inside your environment. This answer mentions the files you can look for inside your environment. They are either inside your env/Library/bin or env/DLLs. I think you have these installed, but pyinstaller doesn't recognize them. To make sure that pyinstaller knows these files, you can add them to datas. This can be edited in the command when building the .exe file or in the .spec file that is probably created when you have run this command once. This link may be useful there.
In short, add the paths of the DLLs I mentioned to the datas (or binaries inside .spec actually) so that they are recognized by pyinstaller.
So either run the command
pyinstaller --onedir --add-data libcrypto-1_1-x64.dll;. --add-data libssl-1_1-x64.dll;. myscript.py
or change your .spec to something like
datas = [('/path/to/libcrypto-1_1-x64.dll', '.'), ('/path/to/libssl-1_1-x64.dll', '.'),
(...) ]
...
a = Analysis(...,
datas=datas,
...)
...
and then run
pyinstaller --onedir myscript.spec
This fixed DLL issues for me at least.

Pyinstaller create an executable with external libraries

I am trying to create an executable file (exe) for a Python script that I written in PyCharm.
When I run the script from the PyCharm is working fine but when I try to run it as an individual .py or try to run the exe I am getting an error.
The issue I believe is with the "from infi.devicemanager import DeviceManager" library.
from infi.devicemanager import DeviceManager # Used for the retrievement of Device manager information
dm = DeviceManager()
dm.root.rescan()
devs = dm.all_devices # Get all the devices to a list called devs
for d in devs:
print (d.description)
I get following error:
PS C:\Python> python.exe .\tests.py
Traceback (most recent call last):
File ".\tests.py", line 1, in <module>
import infi.devicemanager # Used for the retrievement of Device manager information
ModuleNotFoundError: No module named 'infi'
PS C:\Python> pyinstaller -F .\tests.py
PS C:\Python\dist> .\tests.exe
Traceback (most recent call last):
File "tests.py", line 1, in <module>
ModuleNotFoundError: No module named 'infi'
[15072] Failed to execute script tests
Is there a way to include this library to the exe file so anyone without python can run this script?
I am open to suggestions.
Thank you in advance!
=== UPDATE ===
Full script can be found here
https://github.com/elessargr/k9-serial
My answer assumes that the interpreter that has infi.devicemanager is C:\Python\venv\Scripts\python.exe as stated by OP in the comments
So there is virtual environment named venv. You need to activate the virtual environment and install PyInstaller in it.
C:\Python>venv\Scripts\activate
(venv)C:\Python>
Note (venv) in front of your shell - that means virtual environment venv is activated
Now install PyInstaller
(venv)C:\Python>pip install pyinstaller
-- here it will install pyinstaller --
now you can test that both pyinstaller and infi.devicemanager are installed in venv
(venv)C:\Python>pip list
it should list both packages among some others. now
(venv)C:\Python>pyinstaller -F C:\Python\tests.py --hidden-import=infi.devicemanager
--here it will create the exe --
if I was right it should work now
EDIT: It looks like infi is using __import__(pkg_resources), so the command should be
(venv)C:\Python>pyinstaller -F C:\Python\tests.py --hidden-import=infi.devicemanager --hiden-import=pkg_resources

Error message using pyinstaller

I am trying to convert a script into an exe using pyinstaller and Python3.70 (on Windows 10)
When I run the command
pyinstaller --onefile myfile.py
The program runs fine, except some warnings about libcrypto etc..
But when I try to run the exe file found in the dist folder I get this error message:
Fatal Python error: initfsencoding: unable to load the file system codec
zipimport.ZipImportError: can't find module 'encodings'
Current thread 0x00002afc (most recent call first):
Try down-grading to Python 3.6, it seems there are issues with Pyinstaller and Python 3.7 that have not yet been addressed. As per Pyinstaller:
PyInstaller works with Python 2.7 and 3.3—3.6

Cannot import distutils from within a py2exe-compiled script

I am on Windows Server 2012R2, trying to compile a script with py2exe, within a virtualenv, and I'm getting issues whenever one of the application scripts tries to "import distutils" (in my case, it's somewhere inside a 3rd-party library, but I reduced the problem here).
Steps to reproduce:
Create a virtualenv
virtualenv venv
call venv\Scripts\activate
Install py2exe inside the virtualenv
easy_install --always-unzip py2exe-0.6.9.win64-py2.7.amd64.exe
Create setup.py
from distutils.core import setup
try:
import py2exe
except:
pass
setup(
console=[
'py2exe_distutils.py'
]
)
Create py2exe_distutils.py
import distutils
Run py2exe
python setup.py py2exe
Try to run the generated executable
dist\py2exe_distutils.exe
It returns:
C:\Users\root\p\dist\library.zip\distutils\__init__.py:14: UserWarning: The virtualenv distutils package at %s appears to be in the same location as the system distutils?
Traceback (most recent call last):
File "py2exe_distutils.py", line 6, in <module>
import distutils
File "distutils\__init__.pyc", line 25, in <module>
ImportError: cannot import name dist
The script runs fine when I run it directly (python py2exe_distutils.py), even from within the virtualenv.
Am I trying to do something unsupported by py2exe, or is something wrong with my setup?
I had the same problem while creating an executable that used pandas 0.12.0. This worked for me: before you create the executable, copy the distutils folder from the base python installation
robocopy C:\Python27\Lib\distutils venv\Lib\distutils /E /COPY:DAT
I am using virtualenv 12.0.4 and py2exe 0.6.6 on Windows 7 Professional. Some extra insight can be found here. This answer pointed me in the direction of just copying the files.

Categories

Resources