Table of Contents
Even if you are not a programmer, sometimes you will have to run a Python file in CMD or Terminal (Command Prompt) or on Mac; for example, you can use Python to change TeamViewer ID on Mac. You first need to check if your computer already has Python; if not, you must install Python for Windows and Mac. Only then can you run the py file. Lucid Gen will guide you step by step in this article.
Check the Python version on your computer
On Windows, you open the CMD (Command Prompt) application, and on Mac, you open the Terminal application. Next, you enter the command line below to check the Python version on your machine. If the application returns a specific version of Python, your device can run the Python file in CMD or Terminal.
python --version
Run Python file in CMD or Terminal
To run the Python file in CMD or Terminal, enter the command line python
, the space, and the path of your Python file.
If you have trouble getting the Python file path, you can use the copy file path feature on your computer as follows.
Copy the Python file path on Mac
In the Finder application, you turn on displaying the file path by keystrokes Option Command P
. Next, click the Python script file, right-click on the file name in the path section, 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 Copy as path feature, you can choose Properties and copy the path in the Location section. Remember that you have to add the file name manually.
Run Python files with Python virtual environment
If you already have a Python virtual environment for this Python script file, you can enable your Python 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 the app finder on your computer to open IDLE. On IDLE Shell, choose File > Open to open the Python file you want to run.
After you have opened the Python script file, select Run > Run Module to run the Python file on Mac or Windows.
Running the Python file on IDLE will open an additional 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 create a Python virtual environment and install the modules into that virtual environment to avoid causing errors in the original Python.
How to fix Python is not recognized as an internal or external command error?
This error is similar to the “command not found” error on Mac. It indicates that your computer does not yet have Python. Try removing the previous Python and reinstalling it again. It is important to note that you must check the Add Python to PATH option when installing Python on Windows.
How to fix No such file or directory error?
This error is because you entered the wrong path of the Python file you want to run. Let’s review how to copy the link in the article.
Epilogue
In general, the way to run a Python file on Windows’ CMD or Mac’s Terminal is the same. The only difference is that Mac usually comes with Python, and Windows requires installing Python. Your machine must have all the packages that the Python script file needs to run. If you have any questions, don’t hesitate to comment below the article to interact with Lucid Gen!