10 ms·
I just want to run this one Python script
- ddtaylor 5y agoPython 2 should be dead by now. Please, just let it die.
- rsj_hn 5y agoFriend, we're still running COBOL. There are still shops with computers running windows XP, performing various scripted automation or batch tasks. The idea that some running system will be taken offline just because the language is old is incompatible with a lot of real world needs.
- int_19h 5y agoThe problem isn't so much so that Python 2.7 the language is old, but rather that the implementation is no longer supported - including security issues. Having said that, 2.7 still has commercial third-party support, e.g. from ActiveState. Which, presumably, will be around for as long as there are enough users willing to pay for it.
- at_a_remove 5y agoTechnology sticks around for a lot longer than people think, for reasons most cannot imagine until they are faced with these very real business scenarios. I should know, I am living with Python 2 right now.
- nomel 5y agoWith existing, mostly stable, offline code, I've seen that switching to python 3 is not easy to justify, especially if you're short staffed. I would say that type hints are the most tangible benefit, but most IDEs support the, less than ideal, python 2 versions.
- nijave 5y agoIt should be but it's included in LTS software which is still supported CentOS 7, for instance, still has 3 more years of patch support
- Spivak 5y agoOne problem is that the command `readlink —f` isn’t portable, to macOS for example, and there’s not really a satisfying solution. https://github.com/whatwg/html-build/issues/90 https://github.com/whatwg/html-build/issues/90
- ktpsns 5y agoTo be fair, this blog article is not much about python but more about the bash (shell), the weirdness of quoting and the miracles of exec(3).
- callfx 5y agoI don't get it either. This is pretty basic shell knowledge. Is the goal to have at least one Python article on the front page every day?
- teddyh 5y agoWell, it also wouln’t have worked if not for Python ignoring raw quoted strings. It’s those two behaviours of the two languages which makes this limited polyglot script possible.
- tyingq 5y agoI had commented on something related to this a couple of weeks ago here: https://news.ycombinator.com/item?id=28930278 https://news.ycombinator.com/item?id=28930278 The top reply had a great suggestion. Putting this at the top of a python 3.6+ required script results in a reasonable error for those with older /usr/bin/python interpreters, instead of a generic syntax error stack trace. #!/usr/bin/python 0_0 # Python >= 3.6 is required
- slightwinder 5y agoWhile this looks funny and smart, it's not really a good solution as it lacks well communicated documentation. Python has a proper way to check for version, which is backward-compatible till version 2.0, so around 20+ years. import sys if sys.version_info.major < 3: raise Exception('Need Python 3 or higher') EDIT: correction, sys.version_info.major is only there since Python 2.7. For older versions sys.version_info[0] should be used.
- tyingq 5y ago>so around 20+ years. Right, but if there's anything below that that would cause a syntax error, like an f-string, that Exception you created never happens. The end user just sees a syntax error. It's unfortunate there's not a better way to handle it, especially with so many people still having /usr/bin/python linked to a 2.x version.
- deleted 5y ago[deleted]
- maple3142 5y agoI think this is worse because once you have a python 3 only syntax in your script (such as f-string), python 2 will simply throw syntax error because it need to parse the file to execute. The original solution looks hacky, but it can actually inform user to use python 3.
- BiteCode_dev 5y agoWhy use a hack when you can do it cleanly and give a good error message to your user ? import sys if sys.version_info < (3, 6): print('This script requires python 3.6+) else: from module import main main() If you want all of it to be in one file, you can use a zipapp: https://docs.python.org/fr/3/library/zipapp.html https://docs.python.org/fr/3/library/zipapp.html If you don't want to make the zipapp manually, or want it to automatically extract everything on first run, use shiv: http://shiv.readthedocs.io http://shiv.readthedocs.io
- hprotagonist 5y agothis is reasonably close to what setuptools’ entry_points does for creating little shell scripts that call a callable inside your installed python packages.
- HerrMonnezza 5y agoPerl has had a similar trick for quite a while: eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}' if 0; # not running under some shell See https://stackoverflow.com/questions/9143726/what-means-not-running-under-some-shell-in-perl-scripts https://stackoverflow.com/questions/9143726/what-means-not-r...
- chillfox 5y agoWhy make it bash specific instead of sh?
- thundergolfer 5y agoI engaged in this sort of shenanigans recently when I wanted to pull a Python interpreter out of a .zip file, save it somewhere temporary, and then execute the .zip file with that extracted Python interpreter, all when you did ./runme.zip. It's then a (mostly) hermetic Python executable. When I got it working, it felt like the first genuine 'hack' of my career. Something that's awful, novel*, and awesome at the same time. * novel at least to me. It wasn't something I copied off stack overflow or a blog post.
- syllogism 5y agoYou might be interested in pex: https://pex.readthedocs.io/en/latest/ https://pex.readthedocs.io/en/latest/
- BiteCode_dev 5y agoPex doesn't embed the executable. Besides, it doesn't work on Windows, if you want a zipapp, shiv is a better alternative: http://shiv.readthedocs.io http://shiv.readthedocs.io
- gompertz 5y agoYou may also want to look into 'EEE'. EEE is a program that combines multiple files and actions into a compressed Windows, Linux or Mac OS X (Darwin). executable. http://www.erikveen.dds.nl/eee/index.html http://www.erikveen.dds.nl/eee/index.html
- brnt 5y agoUnrelated, but it's been some time since I last heard or thought about DDS.nl! This was an early (90ies?) Dutch internet community which completely left my field of view for decades but apparently is still a thing!
- thundergolfer 5y agoThanks, yeah I'm familiar with Pex, and used google/subpar for a few years. This was hacking I was doing with a google/subpar (.par) file.
- sdeer 5y agoAny reason why he can't use #!/usr/bin/env python here?
- maweki 5y agoIf you're living in limbo where "python" still means "python2", which isn't installed, as opposed to "python3", then this wouldn't work. Or theoretically also the other way around.
- talideon 5y agoIf you don't care which version of python it runs under, you do this: #!/usr/bin/env python If you do, then `python2` or `python3` will be around, and you should specify them explicitly instead of `python`. I'm happy to be corrected on this one, but I always slap this at the top of my scripts: #!/usr/bin/env python3 I'm not aware of anywhere that wouldn't invoke a Python 3 interpreter if one is installed. The original post just seems like one big faff.
- maweki 5y agoPEP 394 (which is now superseeded) clearly states that "python" shall invoke python2 and not python3 https://www.python.org/dev/peps/pep-0394/ https://www.python.org/dev/peps/pep-0394/ "The python command should always invoke Python 2 (to prevent hard-to-diagnose errors when Python 2 code is run on Python 3)." So there was indeed a time where the mentioned scenario was possible. Edit: love those people who got to downvote reality. At no point was python pointing to either 2 or 3 will-nilly, but there was always a PEP specifying best practice behaviour.
- remram 5y agoThe PEP is not superseded, still active. You got this quote from the "history of this PEP" section. This is not what the PEP currently mandates and it hasn't for 2.5 years: https://github.com/python/peps/pull/989 https://github.com/python/peps/pull/989
- drewg123 5y agoWhy make this bash specific by using command -v? How is that better than "which" which works on any shell I can think of? Running FreeBSD, one of my pet peeves is folks who assume /bin/bash is going to exist everywhere.
- tyingq 5y agoLooks like FreeBSD added "command -v" to /bin/sh some time ago. Though, yes, putting bash in the interpreter line doesn't help that. https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=77259 https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=77259
- SuchAnonMuchWow 5y agoI always though that "command -v" was more portable than "which": "command -v" is specified in posix and should always be present but "which" is not. But all my knowledge about that comes from a small drama some times ago when a debian maintainer wanted to remove "which" from its current package (see https://lwn.net/SubscriberLink/874049/bf89a969ed3dde87/ https://lwn.net/SubscriberLink/874049/bf89a969ed3dde87/).
- macintux 5y agoDiscussed extensively here recently: https://news.ycombinator.com/item?id=29026623 https://news.ycombinator.com/item?id=29026623
- warvariuc 5y agoOr #!/bin/sh """:" # bash code here; finds a suitable python interpreter and execs this file. for pyver in 3.6 3.7 3.8 3.9; do which python$pyver > /dev/null 2>&1 && exec python$pyver "$0" "$@" done echo "No appropriate python interpreter found." >&2 exit 1 ":"""
- ByThyGrace 5y agoIs it still bash code if you're running /bin/sh?
- dagw 5y agoThat approach will break now that python 3.10 is out
- mattnewton 5y agoOr, it won't break your code if you haven't tested in 3.10 yet ;)
- cozzyd 5y agodon't you want to go in descending order?
- gnubison 5y agoWhich(1) doesn’t necessarily return a non-zero exit code if it can’t find a program. Instead, the portable alternative for scripts is command -v.
- nunez 5y agoi usually do this when I want to create docstring-like behavior cat <<-DOC line 1 line 2 line 3 DOC
- teddyh 5y agoI once created a set of shell functions which I wanted to have a docstring-like functionality. The solution I came up with was to have each shell function start with the : command with a string as an argument. Since : is an actual command, not a comment, it was preserved as part of the function, and could be extracted at runtime using relatively simple parsing to do introspection. Example: foo(){ : "This is a docstring for the foo() function" bar --verbose | baz --quiet } For those who don’t know: ":" is a shell built-in command which does nothing and ignores all arguments. It was the way in which shell scripts had comments before the modern syntax of using # for comments was introduced.
- julienpalard 5y agoapt install python-is-python3 ♥
- kazinator 5y agoOne fix would have been for distros to have a special symlink like `/usr/bin/pythonx`. This could be a link to any version of Python, guaranteed to exist as long as at least one Python is installed, to be used by programs that can work with any version. Probably too late for that; if you add this, you'are just complicating things: scripts then have to worry about pythonx not existing. The env program could have a feature to help here. GNU Coreutils could adopt an env feature whereby it searches for multiple programs. Because of argument limits in the hash bang command line this would have to be encoded as one argument. #!/usr/bin/env python|python3 So here, env sees there is a pipe in the middle of the program name. It splits the program name into two, and first searches the PATH for python. If that is not found, it searches the path for python3. Alternately, rather than giving preference to the left item, it could prefer the first one that occurs in the PATH. It could search the PATH once, stopping whenever any of the indicated names is found. Or, how about supporting regex in env, if the first character is circumflex: #!/usr/bin/env ^python3? The advantage is that the specification is condensed. On some systems, there is a strict limit, like 32 characters, into which the hash bang line has to fit, so this could be helpful against that. The problem with that is that *env now has to read directory entries in user space to do the matching; it can't do a simple access() test on the name in every directory. This wholesale directory reading fails to take advantage of whatever name lookup caching mechanisms there are in the underlying kernel.
- jfrunyon 5y agoThat's what /usr/bin/python is, though? A link to any version of Python so long as any version is installed.
- kazinator 5y agoIf that were the case, the "I just want to run this one Python script" article and its trick would have no basis. The issue is that on some systems /usr/bin/python does not mean "any version of python", but specifically Python 2. And so, in those systems, if the Python 2 package is not installed, then there is no /usr/bin/python. They can't easily change the meaning of /usr/bin/python, because the distro users adopted to the convention and depend on unqualified python being python2, and python not being found as an indication that Python2 is not installed.
- lambic 5y agoWhen I saw the headline I thought the article was going to be about the ridiculously confusing tooling landscape around python development. I'm never sure what package manager or virtual environment thingy I'm supposed to be using.
- ed_elliott_asc 5y agoI’ve settled on using private venv environments for each repo/app and that seems to work well enough tbh.
- runjake 5y ago> I'm never sure what package manager or virtual environment thingy I'm supposed to be using. pip3 and virtualenv. Both are pretty easy to learn. Everything else is pretty much an abstraction layer on top of one or both of the above.
- nomel 5y agopip3 assumes you have python 3, and the correct version of it, already installed. virtualenv requires that you manually juggle command line contexts, by running some nested scripts. For virtualenv, you can set up bash script that detect when you cd into a folder, but wow.
- orf 5y agoJust use poetry[1], there isn’t any reason not to. You can create and manage virtualenvs by hand if you’d like, but why do that to yourself? 1. https://python-poetry.org/ https://python-poetry.org/
- fmakunbound 5y agoI sure want to believe this is the last tooling I’ll need to use for Python tooling nirvana, but that’s what they told me about pip, pip3, venv, vitrualenv, conda.. And one coworker just suggested I just run the script in Docker. Ugh
- mistrial9 5y agocould the zealots at Debian-Ubuntu please stop purposefully damaging the Python 2.7 interpreter, please? thx
- W0lf 5y agoFirefox has been using these hacks described in the article for quite some time now for its build command script mach[1] [1] https://searchfox.org/mozilla-central/source/mach https://searchfox.org/mozilla-central/source/mach Pretty neat, although its support for Windows hosts seems to rely on Powershell (or dispatches completely to anything other than Python. Not sure about that)
- jfrunyon 5y agoYes, I maintain several packages which are* compatible with either Python 2 or 3. Those packages have a defined entrypoint which begins with `#!/usr/bin/python`. For packages which are Windows-compatible, they end in .py. If those two things don't cause the script to be executed by Python then frankly the user's system is broken and I'm not going to worry about it. (Depending on what you're writing, `#!/usr/bin/env python` may be a better option, so that you respect the PATH - including venv's.) * Well, they were compatible, but I no longer test against Py2.