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!
Related
My python script is perfectly running in vs code, but when I run it via terminal I get the error message
"ImportError: No module named concurrent.futures"
I use a venv and a pip list shows:
Package Version
-------------- ----------
appdirs 1.4.4
attrs 19.3.0
beautifulsoup4 4.9.1
black 19.10b0
certifi 2020.4.5.2
chardet 3.0.4
click 7.1.2
futures 3.1.1
idna 2.9
pathspec 0.8.0
pip 19.2.3
regex 2020.6.8
requests 2.23.0
setuptools 41.2.0
soupsieve 2.0.1
toml 0.10.1
typed-ast 1.4.1
urllib3 1.25.9
The import in my code looks like this:
import concurrent.futures
import csv
import os
import re
import time
from datetime import date
import requests
from bs4 import BeautifulSoup
I also tried:
from futures import ThreadPoolExecutor
import concurrent.futures
installing futures not in a venv
updating futures
uninstalling futures
EDIT:
my goal is to run the script daily so I followed this tutorial Link
and I'm stuck at the point were I create a Unix executable file. When I run this file the terminal shows the error.
I figured it out my self and it was quite simple...
In the tutorial they create this file with
#!/bin/sh
Python /Users/yanissilloul/Documents/instabot-master/examples/like_hashtags_copy.py neonphotography
The solution was to activate the venv before running the .py file.
#!/bin/sh
source .venv/bin/activate
Python /Users/yanissilloul/Documents/instabot-master/examples/like_hashtags_copy.py neonphotography
Thanks to Niklas Mertsch, your question brought me there.
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"
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.
I downloaded multiple modules (Discord API, cx_Freeze) (pip download, Windows 10) and now I wanted to use them.
But when I want to import them, it says there isn’t any module.
From my former Python using (before resetting computer) I‘ve added a pycache folder and it worked for one module. I‘m not able to reproduce it for other modules. What to do?
I‘ve only one Python version (3.6.5) on PC.
I‘ve checked the \site-packages folder and they‘re there.
If you are using python3 then try downloading the library using
pip3 install libname
but if you are using python2 then install the library using
pip2 install libname or just pip install libname
try with these command and reply
try installing your library using the command prompt in normal user and with the admin user so that you will get to know that what is happening and also if it is still not working then try installing the library into the same folder of your project using pip custom install command
pip install -t <direct directory> <package>
then use the import statement
For Example I used
pip2 install -t c:\Users\Nav\Desktop\projectss cx_freeze
then i imported the library using
#from cx_Freeze import setup, Executable
import cx_Freeze
from cx_Freeze import *
it worked.
Previously i was getting error like :
File "C:\Python27\lib\site-packages\cx_Freeze\__init__.py", line 10, in <module>
from cx_Freeze.finder import *
ImportError: No module named finder
After custom install it is working
In Python 3.4 from Anaconda, I created a program and it is giving me and import error each time I run it.
Using Spyder.
ImportError: No module named 'win32api'
I already have the pywin32 installed. And I can see the win32api.pyd under C:\Anaconda3\Lib\site-packages\win32
This is the import code on my program:
from tkinter import *
from tkinter import ttk
import tkinter.messagebox
import time
import requests #needs to be installed
import pymysql #needs to be installed
import csv
import win32com.client #needs to be installed
import datetime
This is the whole error:
File "C:\Anaconda3\lib\site-packages\win32com\__init__.py", line 5, in <module>
import win32api, sys, os
ImportError: No module named 'win32api'
This is the only instance of Python I have installed. I uninstalled everything else and installed only Anaconda.
Searching online I got to something that said that it could be a problem with the PYTHONPATH. But honestly, I have no idea what they are talking about.
So any noob help would be really appreciated.
I am using Windows 7 by the way.
Thank you
The installation for pywin32 must have failed, or it is not for same OS bit architecture (say anaconda 64 bit and pywin32 32 bit). I recommend
you uninstall pywin32,
check what bit version of anaconda you are using, then
install pywin32 for same version,
verify that the installer indicates 100% success and there are no errors flagged in the installer's log window (it's rare but something may fail and the installer doesn't know).
Then open a python console and type "import win32com".
If #5 fails to import win32com, then:
try installing a different version of python, for example from python.org
repeat steps 2 to 5 above but for new python instead of anaconda
Could be that anaconda distributors did something to the python interpreter (although I didn't think so), or that some libs aren't registered right (see answer https://stackoverflow.com/a/17061853/869951 for some more things to try).
This should work:
pip install pypiwin32
I had the same problem and solved it installing the module pywin32:
In a normal python:
pip install pywin32
In anaconda:
conda install pywin32
My python installation (Intel® Distribution for Python) had some kind of dependency problem and was giving this error. After installing this module I never more saw it.
As mentioned by outforawhile in comment, simply restarting the laptop fixed this for me.
It may be that this is required for Windows to register the DLL.
try this before install pywin32
pip install pywinutils