shotgun_api3 not working in PyInstaller exe build - python

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

Related

PyInstaller throws error module not found

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"

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!

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'

py2exe cannot import from six.py

I'm trying use py2exe on a program that imports urlparse from six.moves.urllib_parse. Here is the program:
# hello.py
from six.moves.urllib_parse import urlparse
print('hello world')
And here is my setup.py:
from distutils.core import setup
import py2exe
setup(console=['hello.py'])
Running hello.py works fine. When I compile hello.py into an exe using python setup.py py2exe, a hello.exe file is produced. However, when I run hello.exe I get an error saying:
ImportError: No module named urlparse
I'm using Python 2.7.
With Python 3.4, I get an error saying KeyError: 'six.moves' when running python setup.py py2exe.
How can I stop these errors from occurring?
The problem is just that py2exe doesn't detect the modules that are proxied through six, so they're not bundled.
All you have to do is add the module in question (urlparse) to your includes in your setup.py:
options={
"py2exe": {
...
"includes": ["urlparse"],
...
That way the module will be packaged, and when six tries to import it, it will work.
py2exe released a new version recently that fixes this problem:
Changes in version 0.9.2.2:
- Added support for six, cffi, pycparser, openssl.
Using this version I was able to create an .exe and run it successfully.

How Can I Make A Windows Executable With Pyserial?

I am trying to make an executable of a Stackless Python 2.6 program. The setup file is
from distutils.core import setup
import py2exe
setup(console=['controller.py'])
and I run it with
python setup.py py2exe
However, when I try to run it, it raises an ImportError and says that there is no module named serial. When I try:
python setup.py py2exe --includes serial
or
python setup.py py2exe --includes pyserial
the build fails with an ImportError. Do you have any ideas?
Had same issue. Upgrading pyserial to 2.5 solved the problem: exec built without additional "includes" contains pyserial.

Categories