如何在Mac终端带命令行参数运行exe文件?解决命令未找到报错
Got it, let's break down why you're hitting this error and fix it step by step:
Why the Error Happens
The .exe file extension is specific to Windows executable files—macOS (a Unix-based system) doesn't recognize this format natively. When you try to run p1.exe or just p1, your terminal has no idea how to execute it, hence the "command not found" message. The good news is you have two solid solutions depending on your situation:
Solution 1: Re-Package the Script for macOS (Recommended)
Since you originally used pyinstaller to generate a .exe (likely on a Windows system), you'll need to re-run the packaging process directly on your Mac to create a macOS-compatible executable:
- Open your Mac terminal and navigate to the folder where your
p1.pyscript lives:cd /path/to/your/p1.py/folder - Re-run PyInstaller with the same
--onefileflag:pyinstaller p1.py --onefile - PyInstaller will create a
distsubfolder inside your current directory. Insidedist, you'll find a file namedp1(no.exesuffix)—this is your macOS executable. - Run it with your command-line arguments like this:
(The./dist/p1 -f input.txt./tells the terminal to look in the specified directory for the executable.)
Solution 2: Use Wine to Run the Windows EXE (If You Can't Re-Package)
If you need to use the existing .exe file (e.g., you don't have access to the original p1.py script on your Mac), you can use Wine—a tool that lets you run Windows applications on Unix-based systems:
- First, install Homebrew if you haven't already, then run this command to install Wine:
brew install wine - Once Wine is installed, run your
.exefile with the arguments like this:
Wine will simulate a Windows environment to execute the file.wine p1.exe -f input.txt
Quick Note
The reason python p1.py -f input.txt works fine is because Python is cross-platform—the script itself isn't tied to a specific system's executable format. PyInstaller, however, compiles the script into a system-specific binary, so you need to match the target OS when packaging.
内容的提问来源于stack exchange,提问作者Miffy

