including necessary packages in .pyc file - python

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

Related

Pyinstaller automatically includes unneeded modules

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.

Distribute a python script in bytecode precompiled + all necessary libraries

I made a (one file) scrip in python for my client, the program is a success and now it needs to be distributed to 12 of my client employees.
The script I made uses a lot of libraries (imports), some of then are not popular at all so here goes the question:
Is there a way to distribute my program already compiled in bytecode? So the users can run it by just simply doing "python myProgram.pyc" or just "myProgram.pyc" (if it has +x property), I know this is entirely possible in Java by compiling the libraries inside a JAR file, is there anything similar for python?
Please don't recommend me py2exe since is far away for what I want, either other similar tools, I just want to distribute a package with all the necessary libraries already pre-compiled in bytecode so the final users don't need to worry about installing libs, pip, github, custom stuff, or anything. Hope you can help me, if not I will have to port the whole project to Java.
If your client employees machine are Windows go for py2exe http://py2exe.org/
If Mac go for py2app https://pypi.python.org/pypi/py2app/
cx_Freeze http://cx-freeze.sourceforge.net/ is cross-platform and it should spit out executable that would run on any OS with Python installed.
PyInstaller http://www.pyinstaller.org/ is a good one too.
However, these methods do not compile and hence improve run-time performance improvements. Rather a way to distribute your script as a single executable with all the necessary modules.
You could use the compiled .pyc file with a wrapper around it for execution and package it as a single executable. However, performance improvements of doing so is debatable.
EDIT:
It's been long though, recently started with cython and it could be a plausible solution for this problem. If not all, defining the variable types should do that is asked in the question.

How to distribute a Python program with external libraries

I have a Python program that uses the following libraries: Tkinter, NumPy, SciPy, SymPy, and Matplotlib. And probably it will include more libraries in the near future while being developed.
How can I distribute such a program to Mac, Windows, and Linux users, without requiring users to install the right version of each library, and hopefully by just downloading a single file and executing it?
What I initially wanted was compiling the program into a static binary program, but it seems that it's not an easy goal.
Probably I can ask users to install Python, but that's the maximum requirement that I can ask for them, and I want to avoid it if possible.
I don't care about hiding the code at all; in the end I will distribute both the code and the program. What I want is to make the program very easy for any user to download and run it.
Even such an advice as 'a Python program is not suitable for such a distribution' is welcome. I had a fair amount of experience with distributing C programs but I don't know what to expect with a Python program.
For convenient, you could try something like pyinstaller.
It will package all of needed module into one folder or or one executable as you like. And it can run in all platforms.
The simple command to make a directory contains an executable file and all needed library is
$pyinstaller --onedir --name=directory_name --distpath="path_to_put_that_directory" "path to your main_program.py"
You can change --onedir into --onefile to make that folder into an one executable file which has all the thing it need to run inside.
You can use Setuptools to do the packaging stuff .
It create eggs, which are the equivalent of jars.
https://pypi.python.org/pypi/setuptools#using-setuptools-and-easyinstall
https://pythonhosted.org/setuptools/setuptools.html
You can have a look at Py2exe , even though you risk the application becoming bigger than it already is, and some packages need to be installed manually .

How to create Python module installer on Windows without including source files

I would like to create a Windows Installer of my Python module (as explained here https://docs.python.org/2/distutils/builtdist.html) without including the sources, meaning that the installer should only embed the Python bytecode.
I followed the solution explained here https://stackoverflow.com/a/3444346/343201 and it only works for ZIP distributions. In other words, if I create a ZIP built distribution, I manage to exclude all sources and export only bytecode (.pyc). On the other hand, if I create a MSI/WININST built distribution, it will only contain sources (.py).
I looked at the output of bdist_wininst and looks like when creating a Windows installer, only source files are exported, bytecode is ignored. Why? Is there a way to enforce the installer to embed only bytecode and no source files?
Thanks
You can use py2exe, though it uses your script files. It will make a .exe file and the source code stays locked.
You can try every way around. Head over here and try http://www.py2exe.org/old/

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