Future-proof Python GUI + file drag-and-drop + executable - python

I have a Python 2.7/Tkinter program that uses TkinterDnD2 and TkDnD (not part of the standard Python distribution) to provide file drag-and-drop in a Win7/Win10 environment. I have been using PyInstaller to create a single-file executable for ease of distribution within our organization. I hadn't touched it for a few years, but it's needing maintenance. Unfortunately, the current version of PyInstaller (3.2) does not produce a working executable with these Tk* modules. (I have a custom arcane hook-_tkinter.py that made it work with an older version of PyInstaller, but have not been able to port it to 3.2.)
I could try replacing PyInstaller; I could try a different GUI toolkit. I would like a solution that will work in Python 3. I need the file drag-and-drop capability. It's not clear whether the TkinterDnD2 and TkDnD are being supported any more.
What would be a viable combination of packages to build a Python 3 GUI with file drag-and-drop and distribute as a "portable" executable?
Python 3 + [tkinter|wxwidgets|pyside|??] + [pyinstaller|cx_freeze|py2exe|??]

I have successfully migrated my application to Python 3.4/PySide 1.2.4/Qt 4.8.7 running in Windows 10, and I can successfully package it into a single-file .exe with PyInstaller 3.2.
The packaging is much more straightforward than it was with Python 2.7 and Tkinter/TkinterDnD2/TkDnD. No custom hooks are needed:
pyi-makespec --windowed --onefile --noupx --icon=appicon.ico app.py
pyinstaller --clean app.spec
This produces app.exe. I use the --clean flag to clear out the pyinstaller cache each time to avoid any problems.
I am also very happy with Qt/PySide. It has solid support for drag-and-drop (which was my requirement from above), a mature set of widgets, and good documentation.
I also looked at the other GUI toolkit candidates, Tkinter and wxwidgets. Tkinter in Python 3 still does not support file drag-and-drop natively, and it did not look like TkinterDnD/TkDnD was being actively supported. Wxwidgets did not appear to support file drag-and-drop either.

Related

Remove dependencies when compiling .py to .exe [duplicate]

I need to package my Python application, its dependencies, and Python itself into a single MSI installer for distribution to users. The end result should desirably be:
Python is installed in the standard location
the package and its dependencies are installed in a separate directory (possibly site-packages)
the installation directory should contain the Python uncompressed and a standalone executable is not required
Kind of a dup of this question about how to make a python into an executable.
It boils down to:
py2exe on windows, Freeze on Linux, and
py2app on Mac.
I use PyInstaller (the svn version) to create a stand-alone version of my program that includes Python and all the dependencies. It takes a little fiddling to get it to work right and include everything (as does py2exe and other similar programs, see this question), but then it works very well.
You then need to create an installer. NSIS Works great for that and is free, but it creates .exe files not .msi. If .msi is not necessary, I highly recommend it. Otherwise check out the answers to this question for other options.
My company uses the free InnoSetup tool. It is a moderately complex program that has tons of flexibility for building installers for windows. I believe that it creates .exe and not .msi files, however. InnoSetup is not python specific but we have created an installer for one of our products that installs python along with dependencies to locations specified by the user at install time.
I've had much better results with dependencies and custom folder structures using pyinstaller, and it lets you find and specify hidden imports and hooks for larger dependencies like numpy and scipy. Also a PITA, though.
py2exe will make windows executables with python bundled in.
py2exe is the best way to do this. It's a bit of a PITA to use, but the end result works very well.
Ok, I have used py2exe before and it works perfectly except for one thing... It only works on executable windows machines. I then learned about Jython which turn a python script into a .Jar file. Which as you know is executable from any machine that has Java ("To your latest running version") installed. Which is great because both unix, windows, and ios (Most of the time) Run java. That means its executable from all of the following machines. As long as they run Java. No need for "py2mac + py2exe + freeze" just to run on all operating systems. Just Jython
For more information on how it works and how you can use it click here.
http://www.jython.org/

additional modules for python in tkinter issue

I'm making a tkinter app in python which requires a few python modules (for example paramiko) that need to be installed additionally.
When I will be done with the whole app and create an executable version of the app (.exe extension), and send this app to one of my friends' computer, can he run the app without having python, paramiko, etc. on his computer?
2nd question: What is the exe equivavelent in mac for that I could make my app runnable instantly (I think of some kind of portable version.)
Thanks!
You want Cx_Freeze
Yes the idea is to turn it into an application that can be run on computers without Python installed. If it does not run correctly then the script has not been properly "frozen".
Cx_Freeze works on all major platforms as described in their front page (see link above for details) but you must freeze it on each platform you wish to develop for (Windows on Windows, Mac on a Mac and so on) There is no executable that can run on all platforms.
There are alternatives such as Pyinstaller and Py2app. The latter works only for Python 2 and is no longer maintained. I would recommend Cx_Freeze or Pyinstaller because they are the only two still being maintained.
Pyinstaller is easier but cx_Freeze also offers a lot of options when building.

Compile python virtual environment

I created a Python script for a Freelance job and I can't find how to compile/build/package it for easy sharing. The person for which I created it is not a technical one, so I can't explain him how to activate a virtualenv, install requirements and so on.
What is the easiest way for him to run the project right after downloading it?
Can the whole virtualenv be compiled into an .exe? If yes, can this be done inside a macOS system?
Yes you can package your python programs and it's dependencies with
Cx_Freeze
There are other python modules that do the same, personally i prefer cx_Freeze because it's cross platform and was the only one that worked out of the box for me.
By default cx_Freeze automatically discovers modules and adds them to the exe to be generated. but sometimes this doesn't work and you might have to add them by yourself
To create a simple executable from a file. you can just use the bundled cxfreeze script
cxfreeze hello.py --target-dir dist
but more for more complex applications that have different files you'll have to create a distutils setup script.
Note that cx_freeze doesn't compile your code like a traditional compiler. it simply zips your python files and all it's dependencies ( as byte code) together, and includes the python interpreter to make your program run. So your code can be disassembled. (if anyone wants to) and you'll also notice that the size of your program would be larger than it was initially (Because of the extra files included)
I ended up using PyInstaller as this worked out of the box for me.

Canonical way to install modules and app onto offline system

We have an app (a bunch of Twisted classes actually) which runs on a specific Python version and depends on quite a bit of modules. This app needs to be deployed onto a Windows Server machine which has no access to Internet.
Currently we are choosing between:
having to install Python prior to everything else, and a Python script which unpacks all modules and runs setup.py,
making an NSIS installer which installs Python, then all modules with .exe installers, then unpacks smaller modules into some other dir, then adds the dir to %PYTHONPATH%.
What is the good accepted way of dealing with such situation? Obviously we cannot use pip, easy_install.exe and other blessed tools, and our approaches are silly and inelegant.
As a third option you can consider deploing the application as an executable using PyInstaller (http://www.pyinstaller.org). You dont need to install anything on the client machine (not even python)
PyInstaller is a program that converts (packages) Python programs into stand-alone executables, under Windows, Linux, Mac OS X, Solaris and AIX. Its main advantages over similar tools are that PyInstaller works with any version of Python since 2.4, it builds smaller executables thanks to transparent compression, it is fully multi-platform, and use the OS support to load the dynamic libraries, thus ensuring full compatibility.
I have used it in a project to deploy standalone application in both Linux and Windows. Worked like a charm. My project also used Twisted.
Between your current two choices the setup.py approach is more pythonic. But beware that if any of your modules has some c implementation for faster performance that need to be compiled, you can't do that on you client's machine.
Try to use wheels. It designed to cover your case, i.e. you build wheel once, downloading all required packages. Then you just copy wheel archive to the target machine and install your application without downloading anything.

Solutions for freezing a pyqt4 application in Windows?

Is there a workable and proven way to freeze a pyqt4 application in windows? I heard that there are some issues for py2exe.
I used py2exe for a PyQt4 project at work but ended up switching to PyInstaller.
py2exe worked great for the most part. I remember having to manually tell it to include the sip libraries in my setup.py file along with some others depending on the program. PyInstaller seems to handle this better in my opinion, although I often find myself removing DLL files from the final folders' qt4_plugins folder that it has decided to include that I don't actually need. For example, qt4_plugins\imageformats\qjpeg4.dll when I'm not using JPEG files at all. This does not hinder the frozen application in any way, it'll just increases the filesize.
The manual for PyInstaller is pretty good as well and with it's 'Getting Started' section you should quickly be able to get things set up. It's as simple as creating a .spec file per project which is automatically generated but is also a normal Python file so that you have the option to tweak it or add any extra tasks such as code signing or maybe creating a setup.exe program using NSIS.
I have a windows batch file named pybuild.bat in my path who's contents consist of:
python -O c:\python27\pyinstaller-1.5\build.py "%~f1"
so that I can easily build a project from the command line by running 'pybuild projectname.spec' on the projects .spec file. I've also added this to the registry's entry for the .spec file so that I can freeze a project quickly from Explorer:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\.SPEC\shell\Freeze\command]
#="C:\\Python27\\Scripts\\pybuild.bat \"%1\""
My main reason for switching from py2exe though is that some users complained that the frozen apps would not run on their machines. I kept running into the error:
The application has failed to start
because the application configuration
is incorrect. Reinstalling the
application may fix the problem.
but only on some machines, usually running Windows XP. I found that getting the user to install the Microsoft Visual C++ 2008 Runtimes made the problem go away.
Frozen apps created with PyInstaller didn't have this problem as it seems to package these runtimes into the output folder. If UPX is available somewhere in your path it will compress your DLL and PYD files as well, resulting in a smaller output folder.
Long story short - both py2exe and PyInstaller work fine. I hit a few issues with py2exe when using it and although there may be workarounds for these if you hunt around I found that PyInstaller worked better without any modifications. I've distributed frozen apps from PyInstaller that make use of image and database libraries from PyQt4 as well as a few other third party libraries and haven't hit any problems yet.

Categories

Resources