Python Forum

Full Version: Run Python 3 script with the same command on Windows, Linux, and macOS?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I use Python 3 scripts to automate various steps in continuous integration (CI) in Azure DevOps and GitHub Actions that runs on Windows, Linux, and macOS. I prefer to set it up so one step is shared across all platforms. To do that, I need to be able to run the exact same command to run the Python script on all platforms. Unfortunately, this is surprisingly complicated.

Problem 1: There isn't a consistent way to launch Python 3 across all platforms:
  • python launches Python 3 on Windows, but launches Python 2.x on macOS and some distros of Linux
  • python3 command works on macOS and most distros of Linux, but doesn't exist on Windows
  • py can use the -3 option to run Python 3 but it's only available on Windows

Problem 2: Running the script without any sort of python, python3, and py commands is subject to shell inconsistencies across the platforms
On macOS and Linux, add #!/usr/bin/env python3 to the first line of the script and run chmod +x script.py to make it executable. Even with those changes, we still have these problems:
  • ./script.py will work on macOS and Linux, but not Windows command prompt or PowerShell
  • script.py works on Windows command prompt, but nowhere else
  • Regardless of ./ or .\ or nothing preceding the script in PowerShell, it launches a separate terminal window and never shows any of the Python script output (not sure if it runs at all).

So as a result of all of this, I can either struggle to get one shell to run on all platforms (either get PowerShell working on macOS and Linux or get bash working on Windows) or give up and set variables to define the Python command differently on Windows versus macOS/Linux (my current workaround).

Am I missing an obvious solution that's already available? If not, I think the best solution to this problem would be if py was available on non-Windows platforms. Having python3 available on Windows would be an ok solution too. Is there any reason why it isn't? I'd be happy to play the long game and actually look into contributing such a thing to Python, but I wanted to chat about it and understand it better first. Python is great for this sort of cross-platform automation. Consistent launching of the script would would make it awesome!
Just to be sure you are aware - we are not associated with PSF or python core development team.
Check https://python-forum.io/misc.php?action=help&hid=48

Otherwise you are welcome to discuss any python-related topics with the community here.
I am aware, thanks. I figured this would be a good first step before trying to engage more officially.
ssrobins Wrote:I use Python 3 scripts to automate various steps in continuous integration (CI) in Azure DevOps and GitHub Actions that runs on Windows, Linux, and macOS.
Can't you just configure a variable that contains the name of the command in those tools? I mean that the fundamental theorem of software engineering also solves this issue :)