Writing python on mac to use on Windows - python

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).

Related

Cross compile Windows Python executable in Linux

To explain it simple, I'm currently making a program for the Gomocup on Linux in Python 3.10, but every tool is made for Windows
To test my program I need to compile it from a Windows environnement, and got no way to do it
I want to avoid Windows VM option if possible
Any tips ?
I tried to use pyinstaller in wine in a embedded python so I can compile directly from a Windows environnement, but I couldn't manage to get it working in a generic way :(
(from this post pip with embedded python)

How to create an executable from a Python script in Windows that can run on a Mac?

I need to share the script with someone who does not have Python over their system and can't install the libraries required. I can create an .exe on Windows but how do I create something that will run on a Mac too?
I intend to create an executable on my Windows system and share it with a user who has a Mac so they may run it.
I can think of three solutions to your case and I hope it provides useful information:
The easiest solution is to install Python that someone's Mac and use py2app to create a Mac executable so that they can delete Python later and just keep the executable file.
Tell them to install winebottler, one of the many solutions to run Windows executables on Mac. Keep in mind that since Mac OS Catalina, Macs only run 64-bit applications, so make sure you use a 64-bit Python Version if you go with this option.
It is possible to run Mac on your Windows machine using an Oracle VirtualMachine VirtualBox or similar (shown in this tutorial). You would still have to install the version of Python you are running along with the necessary libraries and your script/files with yourself through something like Github or Dropbox. You will need a significant amount of extra RAM to run Mac OS on Windows and not hate your life while at it.
As someone who has been down that road because my job demanded me to, I highly recommend the first option, it's the shortest one and it doesn't demand any sort of IT prowess from the person you are sharing the application with. Also, as someone not too experienced with Mac OS, I feel like Apple makes it tricky to build executable files that run in more than one of their OS versions.

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.

Using cx_Freeze (Python 2.7) on a Windows box OOTB

I have been plagued with this problem ever since I started with Python, I want to write a Python script, and then export it as an EXE that I can run on any Windows XP (and up) machine.
cx_Freeze covers all of this perfectly, the only problem is that it required Visual C++ Runtime to be installed on the client computer before the resulting EXE will run on it...
Is it possible to convert my beautiful *.py file into a nice distributable EXE that will run on a fresh install of Windows XP and up?
AFAIK if you have a Visual Studio licence, you have the right to bundle the appropriate msvcrXY.dll with your application. That will make it run without having to install the runtime files.
If you don't have a Visual Studio licence, I can think of two solutions:
One is to bundle the VS runtime installer with your application (if that is allowed by the licence), and make a tiny batch file/program that runs the installer if necessary, and then your program. This is not ideal if e.g. the user doesn't have admin rights.
The other option I can think of is for you to compile Python with Mingw-gcc, and then use that Python to create your frozen executable. Then it won't depend on the VS runtime libraries. This approach is of course much more complicated and will probably require quite a bit of tinkering. Perhaps someone has already done it though.

Categories

Resources