Pyinstaller automatically includes unneeded modules - python

I am trying to create a .exe from a very simple script that I have written. The script only include glob and pandas. But pyinstaller is including matplotlib,numpy,scipy,qt4,ipython, and a bunch of other stuff. The .exe won't run because there is an error with matplotlib, but I don't even need matplotlib. What am I doing wrong to make pyinstaller not recognize that only glob and pandas are needed?
I have manually excluded scipy,matplotlib,PyQt4,and iPython and the .exe is still 160mb!
P.S. I'm doing this in winpython with python 3.4.
Edit:
With a little further testing I have narrowed this down to Pandas. Even a script that only consists of:
import pandas
will create a dist folder that is 460MB or a single file .exe that is 182MB. What is the easiest way that I can find out which modules are being imported so that I can properly exclude all of them?
Edit2:
I have tried making a hook-pandas.py file that contains:
excludedhooks=['scipy','matplotlib','PIL','cython','PyQt4','zmq']
The console output indicates that imports are being removed due to the hook file, but tons of files from these modules still end up in the dist folder.
I have also tried excluding these modules in the .spec file as well as in the console using --exclude-module but the files from those modules still show up.

Pyinstaller may have figured those dependencies from your current ones. If you're sure, use --exclude-module flag to list all the modules you want to exclude.
http://pythonhosted.org/PyInstaller/#general-options

Not sure if this really counts as a solutions. But by ignoring winpython all together and using a standard installation of python that only had pip installs of pyinstaller and pandas added to it I was easily able generate a functional .exe that was 18MB. I guess it had something to do with winpython.

Related

How to make a setup file which will install Python and dependencies on one's computer

I have been trying to package my Tkinter app. I tried Cx-freeze and Pyinstaller. I am having some troubles with correctly creating text files, writing into and reading from them. The app works fine when not converted into an exe but does not when converted. I am using absolute paths.
All this led me into asking this question:
Is there any way to create a setup file that, when run, will install Python (downloaded already) and .whl files of the dependencies? This way, the app will work on others computer in its original form.
My program is dependent on Tkinter, os, time, PyMuPDF, re, pandas and numpy.
Initially, I tried to work out a solution but could resolve [Errorno2] from pyinstaller. I searched the internet but could not find a satisfactory solution. I am really hoping to have a setup file instead.
I read this and this.

How to add a folder to Python path?

Basically, I can only reference my other files as modules when they are in a very specific location:
C:\Users\Dave\Desktop\Programming\Python.
If I want to create a new folder for a large project with multiple modules, say
C:\Users\Dave\Desktop\Programming\Python\Project1,
I can no longer import any modules and keep getting a ModuleNotFoundError. I've looked into it and it seems I need to add that folder to the Python Path somehow, but I couldn't find any answers on how to do it. My computer runs on Windows 10 if that matters.
I think the immediate solution to your problem/the answer to your question would be to use sys.path.append() at the top of your script.
import sys
sys.path.append("<ABSOLUTE/PATH/TO/YOUR/CUSTOM/MODULES/FOLDER>")
import custom_module
This isn't an ideal solution for any kind of prod use, however. Based on what you wrote in your question, this may not be what you're looking for. More info might help to craft a more stable solution:
Are you using virtual environments when running python? Do you just run python script.py or is there a specific version of python you're using that's installed elsewhere than the common C:\Program Files\Python?
When you say that you work on a project with multiple modules, does that mean there are custom modules that you/someone wrote or is it just that that project uses non-standard library modules, i.e. you had to pip install them?
Can we get an example of the code you're running and the folder structure of your project/where the modules are that you need?

including necessary packages in .pyc file

I have a small program I am trying pass around to friends. It includes nonstandard libraries like the PIL and mtTkinter packages, and thus usually requires them to be included in the pythonpath.
Is there a way I can generate the the .pyc file so that all required code for the program to run is included in the file, and not required to be installed on my friends computers. I want this to be able to work between different OS like windows, mac, and linux.
Does it have to a pyc file? I use pyinstaller to package programs and share. The first time I used it, I couldn't believe how easy it is.
Here's a link to the portion on packaging for multiple operating systems: https://pyinstaller.readthedocs.io/en/stable/usage.html#supporting-multiple-operating-systems

Cx_Freeze's extra stuff

Whenever I build an exe with cx_Freeze and Python I get a bunch of extra stuff like the Library.zip and all the .dll files. Is there a way I can make it just one executable file that I can just send over to someone and have them run without having to give them all the extra files also? Python 3.4. Thank you!
Not really1. You're best option for a single-file distribution is probably to create an installer.
You can however append the library.zip to your executable:
params['options'] = {
'append_script_to_exe': True,
'create_shared_zip': False,
...
}
setup(**params)
But this only reduces the number of files by 1.
There are two reasons why you can't do this. The first is that some modules are not "zip safe" (those that contain data files that are read with open()). The second, and more important reason, is that Python requires various DLLs in order to run, and Windows's dynamic linker doesn't know how to find and load those DLLs if they're inside a zip file.
See: http://cx-freeze.readthedocs.org/en/latest/faq.html#single-file-executables
1 If you're really ambitious, you could theoretically create an entirely static build of Python (statically link all of the library source code, and the C runtimes, etc.), and do the same with any C modules that you might be using. That plus appending the Library.zip file to the exe might give you a single-file distribution.
However, tracking down and building all those dependencies would be a very large effort.
Yes, If your on windows this method works.
Run ->> iexpress
Follow the instructions.
This will compile all the files into on exe but first you need to create the exe using cx_freeze then browse to the directory in iexpress and it will do the rest.

Trimming Python Runtime

We've got a (Windows) application, with which we distribute an entire Python installation (including several 3rd-party modules that we use), so we have consistency and so we don't need to install everything separately. This works pretty well, but the application is pretty huge.
Obviously, we don't use everything available in the runtime. I'd like to trim down the runtime to only include what we really need.
I plan on trying out py2exe, but I'd like to try and find another solution that will just help me remove the unneeded parts of the Python runtime.
One trick I've learned while trimming down .py files to ship: Delete all the .pyc files in the standard library, then run your application throughly (that is, enough to be sure all the Python modules it needs will be loaded). If you examine the standard library directories, there will be .pyc files for all the modules that were actually used. .py files without .pyc are ones that you don't need.
Both py2exe and pyinstaller (NOTE: for the latter use the SVN version, the released one is VERY long in the tooth;-) do their "trimming" via modulefinder, the standard library module for finding all modules used by a given Python script; you can of course use the latter yourself to identify all needed modules, if you don't trust pyinstaller or py2exe to do it properly and automatically on your behalf.
This py2exe page on compression suggests using UPX to compress any DLLs or .pyd files (which are actually just DLLs, still). Obviously this doesn't help in trimming out unneeded modules, but it can/will trim down the size of your distribution, if that's a large concern.

Categories