Python vs Java bytecode - python

I want my python GUI apps to run on all three OSs without need to recompile on each platform. Is python executable created by pyinstaller (by or similar utility) platform independent like Java's bytecode? Java's bytecode runs on Windows, Mac and Linux using JRE.

It's right there in the documentation
The output of PyInstaller is specific to the active operating system
and the active version of Python. This means that to prepare a
distribution for:
a different OS
a different version of Python
a 32-bit or 64-bit OS
you run PyInstaller on that OS, under that version of Python. The
Python interpreter that executes PyInstaller is part of the bundle,
and it is specific to the OS and the word size.
In other words: no, it's not portable. You need to compile for each platform/architecture.
As stated by #juanpa.arrivillaga in the comments, this has nothing to do with the bytecode python normally executes. That is portable. The problem here is that PyInstaller bundles a python interpreter that is os- and architecture- specific (that is, this doesn't work for the same reason you wouldn't normally be able to run a Windows executable on MacOs).

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/

How to create OS X app with Python on Windows

I need to automate a cross-platform application build. Entire build runs on Windows machine. Part of it is written in Python and compiles for OS X. Currently this part of build is done manually on OS X.
I tried pyinstaller but it looks like it only building for the platform that it is running on. I also tried py2app but it did not install on Windows.
Are there any tools to compile Python script to OS X app on Windows machine?
Short answer:
Apparently, no simple way to do this with the standard set of tools you have mentioned. I outline a completely unprobable solution in the end that's probably too complex to consider.
End result: Keep doing it manually, it's probably the best option so far.
Drawing from credible and/or official sources:
There's already a long and curated list of options specified in micheal0x2a's excelent answer in Create a single executable from a Python project which outlines most tools available for creating standalone distributions of a Python program. One line stands out:
Also, unless otherwise noted, all programs listed below will produce an exe specifically for the operating system it's running in.
I might have read things wrong but there's no explicit note of cross platform support.
Similar lists can be found in in Distribution Utilities and Freezing Your Code — The Hitchhiker's Guide to Python. From these we can take a look at the Windows supported projects:
bbfreeze: Not maintained, no documentation on limitations generally superceded by the rest in this list.
PyInstaller: According to their documentation:
PyInstaller is tested against Windows, Mac OS X, and Linux. However, it is not a cross-compiler: to make a Windows app you run PyInstaller in Windows; to make a Linux app you run it in Linux, etc. PyInstaller has been used successfully with AIX, Solaris, and FreeBSD, but is not tested against them.
PyInstaller once tried to support cross-compilation (From Linux -> Windows) but later dropped the idea.
cx_freeze: Looking at their Freezing for other platforms question in their Frequently Asked Questions list:
cx_Freeze works on Windows, Mac and Linux, but on each platform it only makes an executable that runs on that platform. So if you want to freeze your program for Windows, freeze it on Windows; if you want to run it on Macs, freeze it on a Mac.
py2app: This is not an option since py2app is supported only OSX machines based on their note:
NOTE: py2app must be used on OSX to build applications, it cannot create Mac applications on other platforms.
So installing it on windows is a no-go.
This wraps out the tools available on for creating standalone applications on Windows. Even if not on Windows though, solutions don't exist for creating an OS agnostic tool.
The general consensus for achieving these sort of things is via Virtual Machines; you create a VM image of the target OS, run the dedicated tool for that OS inside the vm and then tranfer the bundled executable to compatible machines. Since OSX is generally not easy to virtualize, from what I know, you kinda run out of luck here.
One complex probable way:
Now, the reason why I said there is no simple way to do this is because there might be one but, from what I can see, it is so tedious you shouldn't even consider it. The general issue we're facing here is that executables for windows are simply not compatible with OSX, heck, Linux executables aren't either. So what we need is the ability to cross-compile things.
One compiler that I've heard supports cross-compilation is clang. So, in order remedy the incompatibility of executables, you could theoretically use clang as a cross compiler. Problem is, this is not a simple task; it is probably way harder than what you're willing to do since it is riddled with complex issues (From OSX to Windows example).
If you do actually find a way to that you now need a .cpp/.c file from your python scripts. Thankfully this is something that can be done by using tools like Nuitka or Cython.
The gist of these is the same, enter .py exit .cpp/.c. The second tricky part might be setting up the clang compiler to be used with these. I honestly have no idea if anyone has done this.
I haven't looked into them much, but for Nuitka, I know that it can also be used to create a standalone executable when passed the --standalone flag (See: Moving to other machines). So, theoretically you could use Nuitka with clang configured to cross-compile for OSX. Then you can rest; you've earned it.
You can use a docker image https://github.com/sickcodes/Docker-OSX like this to simulate a mac computer.
Then from this simulated mac you could install the pyinstaller and run your command from there.
This would then produce the desired file.
Some people do the same way to create windows executables using pyinstaller on linux.
I dont see why this could not work from windows to mac.
Install fabric (Python module, easily installed with pip) on your Windows machine so that you can run the build on your Mac as part of the same automated build process.
Fabric allows you to utilize SSH from Python. So long as you know how to access the Mac over SSH (just need the IP, username, and password), this is easy. Set it up like this:
from fabric.api import env
env.host_string = '<IP of the Mac Here>'
env.user = '<Username on the Mac>'
env.password = '<Password of the user>'
Then copy over the necessary source files from the Windows machine to the Mac like this (once fabric has been set up as above):
from fabric.operations import put
put(r'\path\to\local\source\files', '/path/to/where/you/want/them')
Now you want to run your build tool, whatever it is. You'll need to use run or sudo for that, depending on if the tool requires admin privileges or not.
from fabric.operations import run, sudo
# sudo works the same as run if you need that instead
run('/path/to/build/tool arguments for build tool')
Finally you have the build output which you can retrieve using get.
from fabric.operations import get
get('/path/to/dist/on/mac', '\local\path\to\store\on\windows')
There we go. Pretty simple. No VM needed - just automate the process that you were already manually doing before. The only real requirement is that your Mac has to be available to be connected to during the build process. If you don't have a readily available Mac server, consider just picking up a used Mac Mini - you can get them for as little as $50 on ebay, if budget is a concern.

Compiling python script to standalone program

Is it in any way possible too compile a python script into a standalone program, that would be possible to run on another machine that does not have python installed?
Preferably in a way that the script (or, compiled program) would be able to be installed on other machines, so that it's not just a random file but an actual program that can be launched from the start menu on windows.
Being able to do this in windows is the minimum, but if any cross-platform method exists that would be a big plus.
Any help appreciated.
Actually, there are at least two decisions, as of April, 2015 both work with both Python 2 and 3.
Both I have personally used and can confirm working.
1. cx_Freeze
http://cx-freeze.sourceforge.net/
Works with Windows, OS X and Linux, although you have to compile (or should I say, "freeze") your app on each system. You may use compile your code on different machines or just use virtual machine. Beware that you should use 32-bit Python if you want your app to run on 32-bit systems and compile against it!
2. py2exe
http://www.py2exe.org/
It has added support of Python 3 just recently.
The advantage is that it is possible to wrap the whole program in single executable, while with cx_Freeze you usually end up with Python itself in one file, all your dlls and pythons libraries in separate files and all your code in library.zip file, which is compiled to .pyc files, but this operation is easily reversable, so beware that some of your users might easily hack your software!
The main disadvantage of py2exe is that it is Windows-only.

Can Python program developed on 64-bit Windows run on all version of Windows?

I have developed a python application with 64-bit Windows 8 (the non metro version which looks like Windows 7 interface). I want to distribute it to all version of 64-bit Windows such as Windows XP, Windows 7 and etc. Is it possible for program developed with python to do that? Also, can the software run on 32-bit Windows os as well?
Well-written pure Python programs (just .py files) are extraordinarily portable across all platforms. If you're using some way of packaging your program in a Windows executable (.exe file), then you have worlds of other possible problems.
There are cases where a 64-bit program won't work on a 32-bit system, such as if your program uses massive data structures and you simply run out of address space on a 32-bit system. But, barring things like that, you should be fine.
If you want more specifics, I'm afraid you'll need to be more specific ;-)
If you have not used any 64 bit specific items the your code should run fine on all versions of windows from source code with a minimum installation of python and the dependencies.
For the python code itself, this won't very much be the problem, python code is quite portable.
However, you do need to using some porting tool specific for 32bit Windows to convert .py to .exe.
check this, http://www.pyinstaller.org/

Writing python on mac to use on Windows

I've seen from some sources that although you can make an exe or mac equivalent app using py2exe or py2app, you can only make the one your system is. Makes sense when I think about it.
But my problem is sometimes I want to write python scripts and send them to my Windows-using friends to test and play with. But Windows doesn't come with python installed, and I don't want to make them have to install Python.
Is there any way to use a MAC to create a python-made file that can be opened without python or any installation ON WINDOWS?
If there's not I suppose I could try using the emulated Windows on my system to make it an exe, but I'd rather not boot that every time I need to change something.
You can't make a native py2exe-style executable on Mac. Use Virtualbox to run Windows inside your Mac environment. No need to reboot the whole machine.
Depending on how many dependencies you're willing to install on your machine (your friends with Windows shouldn't have any installation to do), another alternative would be to use IronPython. It won't compile all python code, but it works for a very large subset of it.
And the resulting .exe files can be run on your Mac using the Mono runtime, and on your friend's Windows system using the very likely already installed .NET runtime.
Here's what you would need to do:
Install the Mono runtime.
Install IronPython 2.7.2 or later.
Run mono ipy.exe pyc.py /main:your_program.py /target:exe /embed
This will produce a .exe file that can be run on Windows, Mac, and Linux. On your Mac, you'd do mono your_program.exe. On Windows, your friends can just double-click the .exe file if they have .NET 4.0 installed (very good chance that they do).

Categories

Resources