How do I include poppler to pyinstaller generated exe when using pdf2image? - python

I made a simple script that converts pdfs inside the current directory to images. I want to make it into a standalone .exe file so that someone who doesn't have python installed on his PC can use it.
The problem is that pyinstaller fails to include poppler to the exe file so pdf2image does not run correctly and the built exe fails. Here's the error message:
pdf2image.exceptions.PDFInfoNotInstalledError: Unable to get page
count. Is poppler installed and in PATH? [32024] Failed to execute
script bulk_pdf2img
I'm currently working on conda environment that has pyinstaller and pdf2image and poppler installed from conda install command. It works just fine when I execute the python script from the prompt but when the script is converted to exe, it raises the above error.
I tried the following approaches:
1. Add --add-data option
I tried to add poppler data by doing this.
$ pyinstaller --onedir --add-data="C:/Users/myusername/anaconda3/pkgs/poppler-0.89.0-h20e2fe3_4/Library/include/poppler/*;./poppler" bulk_pdf2img.py
Doesn't work.
2. Add additional-hooks-dir option
I added projectdirectory/hooks/hook-pdf2image.py that has
from PyInstaller.utils.hooks import collect_all
data, binaries, hiddenimports = collect_all('pdf2image')
inside and ran
$ pyinstaller --onefile --additional-hooks-dir=hooks bulk_pdf2img.py
Also doesn't work.
I googled almost every Stackoverflow question that is apparently having the exact same problem as I am but couldn't find any valid solution. What should I do now?

From the docs
You will then have to add the bin/ folder to PATH or use poppler_path = r"C:\path\to\poppler-xx\bin" as an argument in convert_from_path.
Now, if you are using the --onefile option of pyinstaller it will unpack all the files into a temporary folder during the execution, you might want to look into this answer and the related post, to get the path right.
You can do any of the following
Execute this at the beginning to add the bin folder of poppler to PATH.
os.environ["PATH"]+=os.pathsep+os.path.join('path/to/poppler','bin')
pdf2image.convert_from_path('path/to/pdf',poppler_path=r"path\to\poppler\bin")

Related

pyinstaller to creating wrong 1 _MEIxxxx folder, but trying to open another

If this is happening to you, the error (In this case) is a broken installation of pyinstaller or python, remove it from the computer and do a fresh reinstallation.
When trying to create a exe with pyinstaller, it works fine and the bundled .dll file is included an it unpacks the _MEI folder with the necesarry files the correct places. BUT i creates one called _MEIxxx but tries to open a _MEIxxY which does not exist (yes, both changes name everytime it is launched)
I cannot see anywhere you can manually set the name of the _MEI folder which would make it much easier.
The cmd command i am running is:
pyinstaller script.py --add-data "PATH TO DLL\python39.dll;test" -F --runtime-tmpdir .
reproducible problem:
creating a fresh .py project with python 3.9(i use pycharm)
include code of:
print("HI")
then in cmd use:
pyinstaller main.py -F (we want it to be a onefile exe)
Then the .exe file is copied to another pc
Here we run it with CMD to see the error output.
It returns the error:
Error loading Python DLL: "path to local\Temp\_MEIXXXX\python39.dll
the error (In this case) is a broken installation of pyinstaller or python, remove it from the computer and do a fresh reinstallation.

missing module aspose in file exe

I wrote a program which combines two or more PDFs in one and converts PDFs in Jpeg or PNG files.
I tried to create the .exe file but the "aspose" module was not found.
I used these command (I found a sample in this question with the same problem Problem using aspose-words after exe generation by auto-py-exe:
myDirectory CombinaPDF_004.py --onefile --collect-binaries "aspose" --collect-submodules "aspose"
What else could I do?
Example of how to create one-file bundled executable using PyInstaller (https://pyinstaller.org):
pyinstaller --noconfirm --onefile --console --collect-binaries "aspose" --collect-submodules "aspose" path/to/script
Its works fine for me.
'Digital.qubit' answer didn't work for me as it missed a step. My aspose modules being imported in my script are aspose.slides and aspose.pydrawing
The steps i took include:
Open command prompt
change directory to where your python script is located using the following command
cd path/to/script
Enter in the command
pyinstaller --noconsole --collect-binaries "aspose" --collect-submodules
"aspose.slides" script_name.py
To check if you need to include any submodules, check your imports in your script file, if your aspose import is it's own folder separate to aspose found in site packages (Mine is located here 'C:\Users\User\AppData\Roaming\Python\Python37\site-packages')
Then include it in the submodules quotations.
FOR EXAMPLE aspose.pydrawing is included in the aspose folder in site-packages so i didn't need to add it as a sub-module command as it is already included when pyinstaller collects the aspose-binaries through the '--collect-binaries "aspose"' command.
Hit enter on your command prompt to create the exe file.
After you have done this, navigate to your pythons site packages folder again usually located here ^
'C:\Users\User\AppData\Roaming\Python\Python37\site-packages'
and copy the aspose and aspose submodule folder, in my case it was "Apose.Slides-22.8.0.dist-info", into your 'dist/application_folder/' created when pyinstaller is finished. When it asks if you want to replace the files or skip, select Skip.
Launch the application and your ready to go.
Not sure if the op managed to get his working but hopefully this helps other people that have run into this issue

Module not found error: No module named "PIL" [duplicate]

I am creating a standalone exe file with pyinstaller but I get the 'modulenotfound' error when I try to run it. It cannot locate the PIL module even though it is in the same 'site-packages' folder as all my other modules. How do I tell it where the PIL module is?
I have tried the following flags in my cmd line when using pyinstaller, none of them worked:
--hidden-import=Pillow
--hidden-import='PIL'
--hidden-import='Pillow'
My script uses the following line of code in regards to the PIL module:
from PIL import Image, ImageTk
Everything works fine in the script, it is only when I use pyinstaller that I have a problem.
I found a solution: I had to install pyinstaller within the PyCharm terminal, then I had to run pyinstaller within the PyCharm terminal as well using
pyinstaller --onefile -w "foo.py"
I was previously running a cmd right from my file directory outside of PyCharm and running pyinstaller there, but running pyinstaller inside PyCharm works.
I just copied the library folder of PIL from the Python folder within the drive and move it to the same folder as the .py file, then I run the cmd with the line --hidden-import=PIL and it just works, just remember to put the executable file in the same directory you put within your line of codes, might not run if it's not in the same directory.
To whom might help
I got lots of issues trying to user pymssql with Pyinstaller and I finally got it right so posting here might help someone
after you install pymssql using pip install pymssql
use this parameters in pyintaller to get it included
--onefile --paths=.\venv\ --hidden-import='pymssql' --hidden-import='uuid' --collect-all='pymssql' --collect-all='uuid'

How to convert Python project into executable

I have a Python project that I want to convert into an executable. I have installed Pyinstaller. I only know how to convert one single script into .exe, but I have multiple packages with multiple python scripts.
The command line I used with success is:
pyinstaller --noupx --onefile --add-data="cprofiles.ui;." cprofiles_lmfit.py
pyinstaller manages relatively well the multiple '.py' files that you import, no need to cite them. Under the 'add-data' option, you list the non-py files and in my example, the 'cprofiles_lmfit.py' file is the one containing the main.
But as indicated here need help to compile python with pyinstaller (and in few other posts), I am a beginner with pyinstaller. I was never able to use the 'theano' module and I did not optimize. I still have to test the suggestions in the answer.
Converting the main script into .exe should solve the problem, use -onefile to convert it to one exe and rest of the .py files should be included.
1) Open the command prompt
2) Install pyinstaller
Pip install pyinstaller
3) Test your python script first just to check python script should work with normal
.py extension. Saurabh is the name of python file
python saurabh.py
4) Convert the python into executable file
pyinstaller --onefile Saurabh.py
Notice we have passed “–onefile” as a argument which tell pyinstaller to create only one file
5) Go to directory and navigate to the dist folder.
Prerequisite : Install PyQt5 to avoid error.
pip install PyQt5

Windows- Pyinstaller Error "failed to execute script " When App Clicked

I am having a tough time overcoming this error, I have searched everywhere for that error message and nothing seems relevant to my situation:
"failed to execute script new-app"
new-app is my python GUI program. When I run pyinstaller using this command:
pyinstaller.exe --onedir --hidden-import FileDialog --windowed --noupx new-app.py
It does work smoothly. In addition, when I execute the command line to run the gui program, it works perfectly and the GUI is generated using this command:
.\dist\new-app\new-app.exe
But when I go to that file hopefully to be able to click the app to get the GUI, it gives me the error said above. Why is that?
I am using python2.7 and the OS is Windows 7 Enterprise.
Any inputs will be appreciated and thanks a lot in advance.
Well I guess I have found the solution for my own question, here is how I did it:
Eventhough I was being able to successfully run the program using normal python command as well as successfully run pyinstaller and be able to execute the app "new_app.exe" using the command line mentioned in the question which in both cases display the GUI with no problem at all. However, only when I click the application it won't allow to display the GUI and no error is generated.
So, What I did is I added an extra parameter --debug in the pyinstaller command and removing the --windowed parameter so that I can see what is actually happening when the app is clicked and I found out there was an error which made a lot of sense when I trace it, it basically complained that "some_image.jpg" no such file or directory.
The reason why it complains and didn't complain when I ran the script from the first place or even using the command line "./" is because the file image existed in the same path as the script located but when pyinstaller created "dist" directory which has the app product it makes a perfect sense that the image file is not there and so I basically moved it to that dist directory where the clickable app is there!
So The Simple answer is to place all the media files or folders which were used by code in the directory where exe file is there.
Second method is to add "--add-data <path to file/folder>"(this can be used multiple times to add different files) option in pyinstaller command this will automatically put the given file or folder into the exe folder.
In my case i have a main.py that have dependencies with other files. After I build that app with py installer using this command:
pyinstaller --onefile --windowed main.py
I got the main.exe inside dist folder. I double clicked on this file, and I raised the error mentioned above.
To fix this, I just copy the main.exe from dist directory to previous directory, which is the root directory of my main.py and the dependency files, and I got no error after run the main.exe.
Add this function at the beginning of your script :
import sys, os
def resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)
Refer to your data files by calling the function resource_path(), like this:
resource_path('myimage.gif')
Then use this command:
pyinstaller --onefile --windowed --add-data todo.ico;. script.py
For more information visit this documentation page.
In case anyone doesn't get results from the other answers, I fixed a similar problem by:
adding --hidden-import flags as needed for any missing modules
cleaning up the associated folders and spec files:
rmdir /s /q dist
rmdir /s /q build
del /s /q my_service.spec
Running the commands for installation as Administrator
I was getting this error for a different reason than those listed here, and could not find the solution easily, so I figured I would post here.
Hopefully this is helpful to someone.
My issue was with referencing files in the program. It was not able to find the file listed, because when I was coding it I had the file I wanted to reference in the top level directory and just called
"my_file.png"
when I was calling the files.
pyinstaller did not like this, because even when I was running it from the same folder, it was expecting a full path:
"C:\Files\my_file.png"
Once I changed all of my paths, to the full version of their path, it fixed this issue.
I got the same error and figured out that i wrote my script using Anaconda but pyinstaller tries to pack script on pure python. So, modules not exist in pythons library folder cause this problem.
That error is due to missing of modules in pyinstaller. You can find the missing modules by running script in executable command line, i.e., by removing '-w' from the command. Once you created the command line executable file then in command line it will show the missing modules. By finding those missing modules you can add this to your command :
" --hidden-import = missingmodule "
I solved my problem through this.
I had a similar problem, this was due to the fact that I am using anaconda and not installing the dependencies in pip but in anaconda. What helped me was to install the dependencies in pip.
I found a similar issue but none of the answers up above helped. I found a solution to my problem activating the base environment. Trying once more what I was doing without base I got my GUI.exe executed.
As stated by #Shyrtle, given that once solved my initial problem I wanted to add a background image, I had to pass the entire path of the image even if the file.py and the image itself were in the same directory.
In my case (level noob) I forgot to install library "matplotlib". Program worked in Pycharm, but not when I tried open from terminal. After installed library in Main directory all was ok.

Categories

Resources