PyInstaller throws error module not found - python

I am trying to convert .py to exe using pyinstaller using command
pyinstaller --onefile app.py
when i run the exe file it throws error
from bs4 import BeautifulSoup as bs
in this line , module not found
can some one help i am using python packages in my file
from bs4 import BeautifulSoup as bs
from csv import reader
import json, requests, time, gspread, sys, re, calendar, traceback, string, unidecode, datetime
from oauth2client.service_account import ServiceAccountCredentials
pyinsatller is unable to load the python packages without the imports exe is working fine
Removing python imports make it work 100%
I am using python 2.7
any help will be greatly aprreciated

Pyinstaller might be having a problem with the alias you created. Try adding --hidden-import "bs4"
Ex:
pyinstaller --noconfirm --onefile --console --hidden-import "bs4" "./app.py"
Also, make sure pyinstaller is packaging using the correct interpreter. If you have other python interpreters they could be interfering. You can get the full path of the python executable by running
import sys
print(sys.executable)
There is an open-source GUI for pyinstaller that helps:
pip install auto-py-to-exe
To run just activate your virtualenv or conda environment and run in terminal:
auto-py-to-exe

Try this it's worked for me :-
pyinstaller --noconfirm --onefile --console --hidden-import "beautifulsoup4" "app.py"

Related

shotgun_api3 not working in PyInstaller exe build

This is my run.py file, it works perfectly fine if you run it manually with something like py -3 run.py
import shotgun_api3
I use Python 3 to build the .exe using PyInstaller:
py -3 -m PyInstaller run.py
The build completes successfully. When I try to run the .exe I get this error:
ModuleNotFoundError: No module named 'xmlrpc'
So I tried adding import xmlrpc above the import shotgun_api3 in my run.py, then the error changed to this:
ModuleNotFoundError: No module named 'xmlrpc.client'
Definitely not the best solution, but I managed to build the executable. I had to remove httplib2 and six from shotgun api, pip-installed them by myself and updated imports in shotgun.py.
I had to add them as hidden imports as well as a few others:
pyinstaller --hidden-import urllib2 --hidden-import xmlrpc --hidden-import xmlrpc.client --hidden-import xmlrpclib --hidden-import cookielib main.py

using pyinstaller on a python with gui opens and closes the app without executing

I have a python program that use kivy and create a gui. I tried to make the program an executable using pyinstaller but when I try to run the exe file it opens and closes the app immediately. I tried to run the exe using the cmd but it just opened it in another window and closed it immediately, I also tried using the -c flag when using pyinstaller on the .py file but there was no change. what can I do to solve it?
Edit:
I tried adding a print("hello") in the first line and then exit right after it, and then I created an exe again, but the result hasn't changed, the file opened and closed immediately. Therefore I tried running the app with with the cmd with start /B forcing it to run in the cmd, I saw it got the following error
C:\Users\YShay\Desktop\New folder\dist>Traceback (most recent call last):
File "C:\Users\YShay\AppData\Local\Programs\Python\Python38\Lib\sitepackages\PyInstaller\loader\rthooks\pyi_rth_pkgres.py", line 13, in <module>
import pkg_resources as res
File "c:\users\yshay\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\pkg_resources\__init__.py", line 86, in <module>
ModuleNotFoundError: No module named 'pkg_resources.py2_warn'
[19380] Failed to execute script pyi_rth_pkgres
Does it mean it pyinstaller failed to import one of the modules? If so how could I import it or make it accessible for the application?
Those are all the imports of my program:
import socket
import sys
import threading
import psutil
import socket
import subprocess
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from functools import partial
First, if you are unable to install Kivy on Python 3.8 you can install it by typying:
python -m pip install docutils pygments pypiwin32 kivy_deps.sdl2==0.1.* kivy_deps.glew==0.1.*
python -m pip install kivy_deps.gstreamer==0.1.*
python -m pip install kivy_deps.angle==0.1.*
python -m pip install docutils pygments pypiwin32 kivy.deps.sdl2 kivy.deps.glew --extra-index-url https://kivy.org/downloads/packages/simple
pip install kivy[base] kivy_examples --pre --extra-index-url https://kivy.org/downloads/simple/
Then when you will pack the Kivy GUI app with Pyinstaller and you will see an error related to:
Failed to execute script pyi_rth_pkgres
Just uninstall the setuptools and install version 44.0.0 (link)
pip uninstall setuptools
pip install setuptools==44.0.0
Also, the packaging is a little tricky, because it is done it two steps official guide on how to pack Kivy with Pyinstaller:
navigate to your Kivy examples directory.
For me it looks like:
C:\Users\YOUR-WINDOWS-USERNAME\AppData\Local\Programs\Python\Python38-32\share\kivy-examples\demo\touchtracer
create a directory for the packaged application and cd to it.
Then in this directory type:
python -m PyInstaller --name touchtracer C:\Users\YOUR-WINDOWS-USERNAME\AppData\Local\Programs\Python\Python38-32\share\kivy-examples\demo\touchtracer\main.py
You will see that the *.spec file is created. Open it in the text editor.
On the beginning of the file, just after
#-*- mode: python ; coding: utf-8 -*
Press enter and type
from kivy_deps import sdl2, glew
Make sure that your COLLECT will look like this (add two Tree's):
coll = COLLECT(exe, Tree('examples-path\\demo\\touchtracer\\'),
a.binaries,
a.zipfiles,
a.datas,
*[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
strip=False,
upx=True,
name='touchtracer')
Now back to your CMD. Build the app using edited spec file:
python -m PyInstaller touchtracer.spec
Your compiled exe file will be in
C:\Users\YOUR-WINDOWS-USERNAME\AppData\Local\Programs\Python\Python38-32\share\kivy-examples\demo\touchtracer\paczka\dist\touchtracer
For the packaging purpose, you can check now does exe is working on a different computer.

Pyinstaller with pywinauto huge file size

I have a script that I want to compile into an executable. I'm using Pyinstaller for this task and the exe file generated is too large.
There are several imports, one of them is pywinauto. If I comment pywinauto imports I get an executable with 20mb. With pywinauto I get an executable with 232mb!
Note: I'm running pyinstaller from a virtual environment in which I have just installed the necessary python dependencies that I'm using in the script.
Here is a list of the imports I'm using:
import os,subprocess,datetime, argparse, math, win32con
import boto3
from botocore.exceptions import NoCredentialsError
from botocore.client import Config
import wx,wx.adv
from pubsub import pub
from win32api import GetSystemMetrics
from pywinauto.application import Application
from pywinauto.keyboard import SendKeys
from pywinauto.findwindows import find_window
from base64 import b64decode
from zlib import decompress
from io import BytesIO
Pyinstaller command that I'm running:
pyinstaller --noconsole --onefile --i=rec.ico --clean script.py
pip list in my virtal environment:
Package Version
--------------- --------
boto3 1.9.246
botocore 1.12.246
comtypes 1.1.7
docutils 0.15.2
jmespath 0.9.4
Pillow 6.2.0
pip 19.2.3
pubsub 0.1.2
pypiwin32 223
python-dateutil 2.8.0
pywin32 225
pywinauto 0.6.7
s3transfer 0.2.1
setuptools 40.8.0
six 1.12.0
urllib3 1.25.6
wheel 0.33.6
wxPython 4.0.6
Is there any way to reduce the executable size?
I'm using pywinauto basically to send a keyboard shortcut command to another application from my script. If there's an alternative library to do this that would make my executable smaller I would certainly try it.
Thanks!
====================
EDIT:
I tried to compile with:
pyinstaller --noconsole --onedir --i=rec.ico --clean script.py
Now I get a folder with 660mb!!! And the application loads WAY faster (it was taking about 15s to load before, now it's almost instantly).
The folder of the application has a LOT of dll files, the 19 largest of them summing up to 537 Mb.
Any suggestions on how to trim that?
I did a fresh install of python 3.7, since I was using Anaconda's Python distribution before, installed just the packages I needed and compiled it with pyinstaller again. Now I have a 35Mb file with --onefile or a 120Mb folder with --onedir. I'll probably be using onedir because the application loads faster that way. I don't think it's the ideal file size but it's certaily way better than before (open to suggestions in case anyone has an idea on how to further reduce the size of the application). Thanks Vasily for the help!

Unable to import PyPDF2 module in Python 3.6.0 Shell

I am receiving following error within Python 3.6.0 IDLE Shell
ModuleNotFoundError: No module named 'PyPDF2'
This occurs even after I have run the command python -m pip install --user pyPDF2 within the Command Prompt successfully.
I have tried running import sys followed by print(sys.path) which returns several file paths, however, none of them appear to be PyPDF2.
Perhaps you should use pip3 since your using python 3.6 I just did the following on my system:
pip3 install pypdf2
and then tested that it worked with the following import statement:
from PyPDF2 import PdfFileReader

Trouble using PyInstaller in Ubuntu

I have a python application where I'm using tesserocr, ocrmypdf. I am trying to create a stand-alone executable of it by using PyInstaller.
I can successfully create the executable by using the following command
$ pyinstaller -F --clean code.py
But when I run it
$ ./code
I get this error
ModuleNotFoundError: No module named 'tesserocr'
ModuleNotFoundError: No module named 'ocrmypdf'
code.py
from tesserocr import PyTessBaseAPI
import ocrmypdf
...
I have cross checked tesserocr and ocrmypdf are successfully installed in my system.
Try this
pyinstaller -F --clean code.py --hidden-import='tesserocr.PyTessBaseAPI' --hidden-import='ocrmypdf'

Categories