Deploying Python application+dependencies on OSX from Ubuntu - python

Background:
I'm writing a non-commercial application in Python, that uses wxPython, and depends on pyPortMidi and SciPy (both available on PyPi). I would like to share this with a small circle of Mac users - who live in different countries.
I work on Ubuntu, and don't have access to OSX systems for testing.
What I'm looking for:
A end-user friendly means of deploying my application, especially given the dependencies
What I've found so far:
Like Ubuntu, OSX comes with it's own Python bundled
This answer
suggests py2app. However, it's not clear from the
documentation
whether I can build an OSX app on an Ubuntu platform. Ditto with cx-Freeze.
Specific Questions:
Can I use py2app to build an OSX app on Ubuntu? And will it automagically include the above dependencies, or do I need to specify it somehow?
If not, can I write some sort of OSX script that will install the package dependies (using easy install, perhaps), painlessly on the end-user system? I haven't used distutils before, and I'm unfamiliar with OSX scripting, so any pointers would be appreciated!
Apologies for the noob questions, and thanks in advance.

You can use py2exe for Windows
Freeze on Linux and as you say py2app for Mac

py2app doesn't work on non-mac machines. As suggested by #victor-castillo-torres, have a look at Freeze, as also suggested in the linked mailing list.

Py2app only works on OSX systems, the code does not support building
bundles for other platforms than the one it is running on. That
is, py2app uses the currently running python installation to build a
depedency graph and copies the files mentioned in that graph to the
application bundle.
From the point of view of building script to install the dependencies
for your script OSX is just like any other Unix system, but with
different GUI libraries. A script that uses easy_install to install
dependencies could be made to work, although I don't know if all your
dependencies are easily available that way (in particular wxPython).

Related

How to create executable file on mac from python script developed on windows

I admit at the beginning that I am a novice python developer, so I apologize in advance for questions that may seem stupid.
I prepared python script with GUI (Tkinter), which use plenty external libraries. I'm working on Windows. Currently, I share my program in the form of an .exe file for Windows users, who do not have Python installed on their PCs. Everything works.
Recently I received a query if I can compile my code on an executable file that is usable for Mac users.
I have a number of questions related to this:
1) can I do it from a PC with Windows or i need Mac? I guess I have to have access to a Mac.
2) will my code work on mac without any editing? What about external libraries? Do you know any easy way to copy/paste my "python with used libraries" from Windows to Mac?
3) will I create a executable file in the same way as Windows, I mean I will use a "Pyinstaller" type library?
Thank you in advance for your help.
1) can I do it from a PC with Windows or i need Mac? I guess I have to have access to a Mac.
You can use a virtual machine and build for mac.
2) will my code work on mac without any editing? What about external libraries? Do you know any easy way to copy/paste my "python with used libraries" from Windows to Mac?
Some libraries have different behavior between macOS and Windows, yes. You should consult the documentation for the libraries you are using.
3) will I create a executable file in the same way as Windows, I mean I will use a "Pyinstaller" type library?
If you're actually using pyinstaller then it is compatible with both mac and windows. You have to check that the library you're using is compatible with both by looking at their docs.
An example of this is to use virtualbox mac image.
Install virtualbox (see this)
Install MacOS (see this)
Install the correct python (obviously python.org)
git clone your repository
Setup your virtual environment (or just pip install -r requirements.txt with your requirements file but I'm a stickler about venvs)
Note: I know you said pyinstaller-like but pyinstaller has a note about macos.
Build!
You shouldn't have any trouble. Just make sure the version of python is the same, as I believe the default on mac is 2.7.
Also, python scripts aren't compiled, rather interpreted, so making the file executable doesn't really mean it's a binary file in this case. I'm not sure what method you used to run python on a computer that doesn't have python on it, as there are a few, but if you can manage to do that on windows, it should work on mac.

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.

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.

Distributing python on Mac, Linux, and Windows using cx_freeze: can I generate all apps from one platform?

I'm setting up a scripted build of a cross-platform python app (Python 3) and I'd like to create all the distributables from linux. Is that possible?
Short answer: no
I've been doing something similiar recently (using cx_Freeze with Python 3). If you set up Python inside Wine, you can generate a Windows build, but I had to copy some DLLs in before it worked properly (cx_Freeze calls a Windows API function that's not implemented in Wine). I've not run into any way of packaging applications for Macs without actually having a Mac.
Perhaps someone should set up a community build service so people could build distributables for different platforms for each other. That doesn't get round the problem of testing, though.

PyQt4 libraries and installation on Mac OS

it has been a while now I'm surfing the net trying to find the way to sucessfully install PyQt4 on different Mac OS's and I can't figure it out.
Does anybody know if there is out there a package I could easily install without having to go through the compiling?
I'm developing sone Python tools with PyQt guis and the idea is to be able to make them work on multy pltaforms.
Any hit would be gratly apreciated,
Cheers,
Lozo
Take a look at py2app. It will bundle up a python script as a Mac OS .app bundle. Inside of there, if configured correctly, will be the copies of the PyQt and Python modules needed to run the program.
For packaging for running on Windows, there's py2exe. Likewise, if configured correctly, it will handle copying all of the libraries into the directory the .exe is made.
From this point on either Mac or Windows, you can create an installer program, .dmg, etc., to distribute and install PyQt applications onto these platforms without having to worry about the end user having to build their own copies of PyQt.
You can install PyQt4 from PyPM Index.
Install libraries from QT and then use Homebrew to install pyqt (google is your friend). I suggest looking at this site for a tutorial on Mac OS X Lion.

Categories

Resources