Even if you are not a programmer, you may sometimes need to run a Python file in CMD (Command Prompt) on Windows or in Terminal on Mac. First, check whether your computer already has Python; if not, you need to install Python for Windows and Mac. Only then can you run the .py file. I will walk you through it step by step in this article.
Check the Python version on your computer
On Windows, open the CMD (Command Prompt) app; on Mac, open the Terminal app. Then enter the command below to check the Python version on your machine. If it returns a Python version number, your computer can run Python files in CMD or Terminal.
python --version
Note: On a Mac, the python command usually returns “command not found”, because Apple removed the bundled Python 2.7 starting with macOS 12.3. Use python3 --version instead, and type python3 in place of python in the commands below. If Python 3 is not installed yet, macOS will offer to install the Command Line Tools, or you can follow the Python installation guide linked above.

Run a Python file in CMD or Terminal
To run a Python file in CMD or Terminal, type python, a space, and then the path to your Python file.

If you have trouble getting the path of the Python file, just use your computer’s copy path feature as follows.
Copy the Python file path on Mac
In Finder, turn on the path bar with the shortcut Option Command P. Next, select the Python script file, right-click the file name in the path bar, and select Copy x as Pathname.

Copy the Python file path on Windows
On Windows, you can copy the Python file path by right-clicking the Python file and selecting Copy as path.
If your machine does not have the Copy as path option, you can choose Properties and copy the path in the Location field. Remember to add the file name yourself.

Run a Python file in a Python virtual environment
If you already have a Python virtual environment for this Python script, activate that virtual environment before running the Python file in CMD or Terminal.
conda activate myenv
python your/file/path.py

Run Python files with IDLE
If you cannot run a Python file in CMD or Terminal, you can use Python IDLE instead. Use your computer’s app search to open IDLE. In IDLE Shell, choose File > Open to open the Python file you want to run.

Once the Python script is open, select Run > Run Module to run the Python file on Mac or Windows.

When you run a Python file in IDLE, it opens another window to show the results, as shown below.

Frequently asked questions
How to fix ModuleNotFoundError?
Use the command pip install module-name to install the missing module. You should also create a Python virtual environment and install the modules into that virtual environment so you do not break your base Python installation.
How to fix the “python is not recognized as an internal or external command” error?
This error is similar to the “command not found” error on Mac. It means your computer cannot find Python. Try uninstalling your existing Python and installing it again. Importantly, you must check the Add Python to PATH option when installing Python on Windows.
How to fix the “No such file or directory” error?
This error means you entered the wrong path to the Python file you want to run. Go back to the section above on copying the file path.
Conclusion
In general, running a Python file in CMD on Windows or in Terminal on Mac works the same way. The main difference is that on a Mac you usually type python3 instead of python, and since macOS 12.3 Python is no longer preinstalled, so you need to install it yourself just like on Windows. What matters most is that your machine has all the packages the script needs to run. If you have any questions, feel free to leave a comment below!





