12 ms·
Python is Actually Portable
- topnde 4y agoHey, I am a JS dev and don't know a lot about python. Can someone ELI5 what is going on here? Thanks.
- JrProgrammer 4y agoI think what is happening here is that the python interpreter can be used as an executable. This way you don't have to install python on the host machine and can carry your programs on a USB stick for example. Correct me if I'm wrong
- jabbany 4y agoThe author compiled the Python interpreter as an Actually Portable Executable (APE) file. APE files (usually using a `.com` extension) are single executable files that can be run natively on many major operating systems. Kind of a "polyglot" executable file that most major operating systems just understand natively. The implication is that you can now package Python code as a single file that can be directly run regardless of the operating system the user is on. No installers, no dependencies on what must already be installed, no separate downloads for different operating systems. Just one single file, click and run.
- keithalewis 4y agoPython is not slow enough. You can now give people a collection of bits that will run python even slower on multiple architectures. As a bonus, you cannot use any of the C packages like numpy or pandas that might cause it to run faster.
- ahgamut 4y agoRequest to change the title to "Python is Actually Portable", as the blog post describes what I did to port Python 2.7/3.6 to Cosmopolitan Libc. Edit: thank you @dang! That was super fast!
- rpdillon 4y agoAgreed. "actually portable" carries special significance in the context of αcτµαlly pδrταblε εxεcµταblε.
- fieryskiff11 4y agois there some background as to why it's written with epsilons, etc?
- cristoperb 4y agoJustine gives a brief explanation in this thread from last year: https://news.ycombinator.com/item?id=26276084 https://news.ycombinator.com/item?id=26276084
- _ZeD_ 4y agoSo just to upset greeks?
- marssaxman 4y agoNot just Greeks - the irritation is portable across all nationalities.
- ozguroz 4y agoIt’s a rabbit hole once you go through that link :)
- kristianp 4y agoWhat's python.com, and what does "python.com APE" mean? Is python.com an executable that runs on multiple OSs? Also why is the flask demo so slow? I think some explanations might be helpful. I'm familiar with python, but not cosmopolitan libc.
- truffdog 4y agoCheck this out- https://justine.lol/ape.html https://justine.lol/ape.html
- ahgamut 4y agoCosmopolitan Libc allows you to build programs that run on six operating systems (Linux, Mac, Windows, FreeBSD, NetBSD, OpenBSD). I ported Python 2.7.18 and 3.6.14 to build on Cosmopolitan Libc because I wanted to have the same Python executable and my scripts with me everywhere :) I've updated the blog post for context. The flask demo is unnaturally slow because my old computer is slow (running python tests in the background). plus I was using `_dummy_thread` Python3.6's pure-python threads implementation.
- kristianp 4y agoThanks for the info, Cosmopolitan Libc looks like a cool project.
- ActorNightly 4y agoYep, and author is a HN user https://news.ycombinator.com/user?id=jart https://news.ycombinator.com/user?id=jart Her comment history is full of good info about the project.
- jart 4y ago/me waves
- nodja 4y agoAPE stands for Actually Portable Executable, an executable file that can run on the major platforms unmodified (windows/linux/mac/etc.)
- hit8run 4y agoFits on every usb drive lol
- agiacalone 4y agoI can't help but feel this will be either a useless toy or an absolutely horrific malicious software injector.
- agiacalone 4y agoDownvote, eh? Okay then. > Cosmopolitan Libc does come with some tradeoffs as well > (static compilation, C codebases, no multithreading > (Update 2022-07-27:, no multithreading yet, > the pthreads API takes a while to fill Toy. > Regarding python packages with C extensions, the build > process for adding them to the APE is rather unwieldy > (2022-07-27 adding C extensions to the APE is much more > elegant now because of the Cosmopolitan monorepo Horrific malware. Unchecked C (even libsafe) is bad. Edit: Nevermind that each app using this software will need to be individually updated every time a new security patch for Python comes out.
- baq 4y agowhile you aren't wrong, this is exactly the same as any statically built software (e.g. all of go) and people still build software this way.
- krageon 4y agoThe downvotes are because you're not really adding anything (I think). This just reads as empty complaining. You have an edited-in point that isn't widely seen as an issue (it is the case, but why do you think it's bad?). That makes it tedious to read, not very stimulating.
- jart 4y agoIf you're concerned about security, then you can use pledge.com to sandbox python.com https://justine.lol/pledge/ https://justine.lol/pledge/
- ahgamut 4y agoI wanted Python to work across at least Windows/Linux because I have a bunch of these tiny scripts that I keep running everywhere. (For example, I always use `python.com -m http.server` when I want to share files in the local network without SSH). Cosmopolitan Libc made it easy for me to run my scripts across Linux/Windows and more, and the running scripts can be accessed via a browser GUI, which makes it very convenient if someone not use to CLI has to monitor what is going on. Plus I can update my scripts any time by just adding them to the executable via a `zip` command. Toys are still fun to play with :)
- buster 4y agoI'm truely impressed. I don't know if this will become useful in real software, because you'll likely have dependencies (which in turn depend on OS features) pretty fast. But nevertheless, one of the coolest projects out there!
- davidshepherd7 4y agoAny use of OS features by calling functions in the standard library should work though, right? I think it's only dependencies that contain C code that would need further changes. I think you would have to recompile then with cosmopolitan libc.
- niux 4y agoHas anybody managed to compile the executable?
- btach 4y agoYes. I initially ran into binfmt_misc issues, did a bit of reading on Justine's github repo for Cosmopolitan and found it can be an issue, especially if you have WINE installed. Once I disabled it systemctl stop systemd-binfmt" on Manjaro I just followed the directions. build/bootstrap/make.com -j$(nproc) o//third_party/python/python.com I tested it on Win10, MacOS Monterey, and Linux Manjaro (built on MacOS and Linux), all worked on all platforms.
- malkia 4y agomadness....
- Uptrenda 4y agoHoly shit, this is frigging cool. Python is my language of choice and I'm amazed at the result here. Wow, even the socket implementation works. Python 3.6.14+ (Actually Portable Python) [GCC 9.2.0] on cosmo Type "help", "copyright", "credits" or "license" for more information. >>: print("hello world") hello world >>: import asyncio Traceback (most recent call last): File "<stdin>", line 1, in <module> ModuleNotFoundError: No module named 'asyncio' >>: import socket >>: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) >>: s.connect(("www.google.com", 80)) >>: s <socket.socket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('192.168.21.19', 63468), raddr=('142.250.70.196', 80)> This work is going to be of interest to a lot of Python devs! Edit: here's the link to the file: https://justine.lol/ftrace/python.com https://justine.lol/ftrace/python.com Download it and see if it will run on your OS. I'm getting the standard prompt on every OS so far. On Windows you just type python.com. On Mac OS X I had to chmod +x, allow it in security settings, and use bash -c './hello.com' to execute it. Simple chmod +x python.com; ./python.com on Linux.
- flohofwoe 4y ago...in addition, on my ARM Mac I had to start the program like this, otherwise there's an "exec format error": arch -x86_64 ./python.com
- woodrowbarlow 4y agothe APE format and cosmo libc provide operating system portability, not instruction set portability.
- Abishek_Muthian 4y agoDoes this mean I can build portable Python applications like I do with Go without having to worry about the glibc version on the running machine in case I had used CGO?
- Uptrenda 4y agoI'm thinking this python.com file would be so, so useful for DevOs / admin tasks. Use it as a lightweight binary to run Python scripts for all platforms. Would probably be very useful for setup / installation scripts or install commands.
- ahgamut 4y agoYup, this is one big reason why I wanted Python to be Actually Portable -- I have a bunch of tiny scripts that are slightly complex for sh (some of them were Windows CMD .BAT files lol) which I like to use everywhere. By building Python with Cosmopolitan Libc, I can take a single binary and my scripts with me! I just add my scripts into the APE ZIP store via `zip -r ./python.com ./.python/*.py` and it just works. I only have VMs now, but a while ago I had Debian 10/Fedora 35/Windows 10/FreeBSD 13 all on the same network, and it was super convenient to have the same script/executable work everywhere.
- baq 4y agoPython's biggest issue - distribution - solved without any containerization. once this gets threads, TLS and a recent Python version, I can see it becoming a non-toy fast. well done!
- bamboozled 4y agoIt's solved by using Go, 6 years ago.
- Epa095 4y agoGo builds a single binary for both Linux, Windows and osx?
- oflebbe 4y agoit builds single binaries even for other processors like arm, arm64.
- estebank 4y agoOne of us is confused, and it very well might be me and GP, but I thought that Go can cross-compile to other platforms (different OS and arch combinations), but cannot produce (at least by default?) a single binary that can run on multiple platforms, like this article does.
- ReactiveJelly 4y agoYeah Go does not do that. I don't think any lang does I think it's pretty much a unique feature of APE, because APE binaries modify their own code after the first run to nativize themselves. (Right? Or do the new versions work different?) I can't tell if my disdain for this is real (Since I really like having my exes read-only and having a consistent hash) or just sublimated envy (Since most desktops will in practice have every file marked as read-write, and if it works it's not stupid)
- 4y ago
- kaba0 4y agoAPE is cool, but other than it being a bar trick I fail to see much usage out of it. Especially in case of an interpreter - like, just have the native version installed on the target OS.
- baq 4y ago> just have the native version installed on the target OS this is literally the problem
- wokwokwok 4y agoHow is it the solution? End user applications, sure… but the raw python interpreter? If you want it, you have to install it. If you want to install it, you go to python.org and install it. This solves… a headache for the maintainers, having to build multiple packages? Not having it installed is definitely not solved by this. Bundling python packages into a single binary is definitely not solved by this.
- baq 4y agoit isn't the solution, but it is a solution. it might be the solution for a particular subset of tools, which is still great, because it usually was a PITA even for those constrained use cases.
- ElectricalUnion 4y ago> End user applications, sure… but the raw python interpreter? If you want it, you have to install it. If you want to install it, you go to python.org and install it. If you want just the raw python interpreter, you're not the end user? > Not having it installed is definitely not solved by this. But not having to install it is the point? > Bundling python packages into a single binary is definitely not solved by this. It is? There's even a working example in the article? mkdir -p Lib/site-packages # specify python version to pip if necessary /usr/bin/python2 -m pip download flask -t . # start "build" cp ./python.com ./my-release.com printf '%s\n' '-m' >./.args printf '%s\n' 'my_module_name' >>./.args # wheels are just ZIPs, use unzip if pip complains ./python.com -m pip install flask*.whl -t ./Lib/site-packages ./zip.com -qr ./my-release.com ./Lib/site-packages/ ./zip.com ./my-release.com ./.args # optional cleanup rm -rf ./*.whl ./Lib/site-packages/ ./my-release.com
- bondant 4y agoDoes it work with tkinter? So I can have a GUI for every OS in a single executable ?
- ptspts 4y agoNot yet, because Tk itself hasn't been ported to Cosmopolitan Libc yet.
- codedokode 4y agoThis APE format is not "write once run anywhere", because it targets a specific architectrure (64-bit x86) and won't run on other architectures. Just a toy format and I see no use for it. What we need instead is better support in Linux for legacy Linux and Windows applications so that Linux could run any proprietary software written for it or for Windows (because there are no sources for it and it cannot be recompiled). Today it is not so: for example, very old Firefox builds don't run or crash on modern Linux.
- spyremeown 4y ago> Just a toy format and I see no use for it. Can you see how the problem that you described in the subsequent paragraph could easily be solved if the applications were compiled in such a format?
- codedokode 4y agoThey wouldn't be solved because the problem is that newer versions of shared libraries are not compatible with older applications. There is no problem with ELF or PE formats - ELF is supported on Linux natively, and PE is supported using Wine. APE format doesn't solve the problem with shared libraries changing over time, it dosn't solve the problem that different plaforms provide different APIs (for example, windowing APIs, audio APIs etc).
- ElectricalUnion 4y ago> APE format doesn't solve the problem with shared libraries changing over time APE doesn't use shared libraries at all and it's intended to be statically linked? > APE (...) dosn't solve the problem that different plaforms provide different APIs APE provides the most important POSIX APIs (or shims) for every platform it supports.
- ElectricalUnion 4y ago> This APE format is not "write once run anywhere", because it targets a specific architectrure (64-bit x86) and won't run on other architectures. > What we need instead is better support in Linux for legacy Linux and Windows applications (...) Today it is not so: for example, very old Firefox builds don't run or crash on modern Linux. But that is the exact reason why "legacy Linux and Windows" doesn't work, and why APE works: you should target the "specific architectrure" directly instead of hopefully trusting that the entire underlying system is a completly static target. That or using containers/shim layers, those of course bringing such entire underlying system with them.
- ingenieroariel 4y agoAnother benefit of cosmo compiled apps is debugging. I won't get into pledge/unveil but check this out: On my compiled python.com I try to open an app with a socket already in use: ./o/third_party/python/python.com run.py --strace SYS 15995 443'293'703 read(3, [u"egister(self, selectors.EVENT_READ)◙◙ "...], 8'192) → 8'192 SYS 15995 443'440'589 close(3) → 0 self.socket.bind(self.server_address) SYS 15995 443'471'588 write(2, u" self.socket.bind(self.server_address"..., 42) → 42 OSError: [Errno 98] EADDRINUSE/98/Address already in use SYS 15995 443'523'517 write(2, u"OSError: [Errno 98] EADDRINUSE/98 /Addres"..., 57) → 57 SYS 15995 443'533'391 getenv("PYTHONINSPECT") → NULL
- Liquid_Fire 4y agoCould you clarify how this is better than what you can get with strace without using Cosmopolitan Libc? Or is it just that it's "built in"?
- ingenieroariel 4y agoJust that it is built in I guess, and that cosmo libc places special attention to allowing you to use gdb/strace/memzoom and other debugging tools.
- samwillis 4y agoThis is awesome! I like the idea of combining this with pyScript to create portable “GUI” apps. Use this for a local server, open the users browser and point it at it. Websockets/pickle to bridge to a UI written in pyScript. Does cosmopolitan have an API to open the users default browser? I know Python itself does… https://pyscript.net/ https://pyscript.net/
- ahgamut 4y agoNice idea! IIRC redbean has the `LaunchBrowser` function (https://redbean.dev/2.0.html#LaunchBrowser https://redbean.dev/2.0.html#LaunchBrowser) to open the user's default browser, and the `cosmo` module in Python is exactly for providing Cosmopolitan Libc goodies via a Python API. You can probably copy some of the LaunchBrowser code into `third_party/python/Python/cosmomodule.c` and implement the feature you're looking for. Submit a PR :)
- intelVISA 4y agoWow, an αcτµαlly pδrταblε snαkε. Jart needs to slow down producing so many good hacks -- it's putting the rest of us to shame!