Python Forum
Run Python 3 script with the same command on Windows, Linux, and macOS?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Run Python 3 script with the same command on Windows, Linux, and macOS?
#1
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!
Reply
#2
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
I am aware, thanks. I figured this would be a good first step before trying to engage more officially.
Reply
#4
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 :)
ndc85430 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas + PIL on Linux (Python 32) Clives 3 2,127 Mar-25-2022, 07:46 PM
Last Post: snippsat
  implementing the Linux touch command Skaperen 0 1,592 Apr-06-2021, 12:06 AM
Last Post: Skaperen
  run command or do it in the script Skaperen 3 2,482 Feb-25-2020, 11:13 PM
Last Post: Skaperen
  Best compiler for windows or mac linux Shadowdevelopment 0 1,625 Jan-21-2020, 02:25 AM
Last Post: Shadowdevelopment
  Is there a method to completely uninstall on macOS? mjcsb 2 2,251 Jan-08-2020, 07:23 AM
Last Post: perfringo
  Learning Python. Which is a good text editor for MacOS? birb 4 3,347 Feb-18-2019, 08:26 AM
Last Post: Larz60+
  how to install python on linux manchun 5 4,544 Mar-01-2018, 09:09 AM
Last Post: wavic
  many versions on python on linux Skaperen 8 5,762 Oct-17-2017, 02:22 AM
Last Post: Skaperen
  Which is best for python?Linux or windows? PySoD 21 33,032 Jun-19-2017, 04:52 PM
Last Post: Ofnuts
  Windows/VBA on at work, Linux+Haskell (and Python) on weekends Ofnuts 4 4,642 Feb-09-2017, 03:11 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020