You’ll now learn to open Python on Linux, Windows, and MacOS. First of all, you should know that there are two ways of using Python:
- Start an interactive shell, also called a REPL, short for read-evaluate-print-loop.
- Start a Python program that you stored in one or more files with the .py extension.
This tutorial will start with the interactive shell because it’s ideal for exploring the language. But at some point, using the REPL will not be enough, and you’ll have to start creating Python files instead.
If you installed Python on your local machine, you first need to start a terminal or command prompt before you can start the Python interactive shell. On all platforms, you should be able to start Python 3 with the command python3
(or sometimes just python
). Just be sure you are running Python 3, not 2, because some systems can have both versions installed.
On Windows, you can start Python from a terminal. I highly recommend you follow my Computer Fundamentals course, in which I explain all the basics you need to know before you begin programming! It does a great job of introducing you to files, folders, the basics of how a computer works, and using the command line.
To start PowerShell, hit the Windows key and start typing PowerShell
. You can use the ‘Command Prompt’ program if you don’t have PowerShell. Once you are in a shell or command prompt, enter one of the following commands (try them in the given order):
- py
python3
- python
The first command (py
) is a wrapper script that allows you to start the latest version of Python. If it works, great. Just remember that I’ll often refer to python
or python3
in the tutorial. You will need to use py
in those cases.
How to open Python on Mac
On MacOS, search for a program called terminal. You can do so by pressing the command key (⌘) + space bar. This will open up the Spotlight search bar, in which you start typing the word ‘terminal’.
Once inside the terminal, enter python3
to open the Python REPL. If that doesn’t work, try entering python
instead (without the 3).
I highly recommend you follow my Computer Fundamentals course, in which I explain all the basics you need to know before you begin programming! It does a great job of introducing you to files, folders, the basics of how a computer works, and using the command line.
How to open Python on Linux
On Linux, you first need to start a terminal. This can often be done with the shortcut ctrl
+ alt
+ T
. Alternatively, you can search for the terminal program in your start menu. The name and where to find it differ from distribution to distribution. Once you have a terminal running, enter python3
to start the Python REPL. If that doesn’t work, try python
instead (without the 3).
I highly recommend you follow my Computer Fundamentals course, in which I explain all the basics you need to know before you begin programming! It does a great job of introducing you to files, folders, the basics of how a computer works, and using the command line.
How to close the REPL
Now that you know how to open the REPL, it would be nice to close it properly, too. If you simply close the terminal window by pressing the close button, you will be able to close the REPL inside it as well. However, it won’t be a clean exit; your terminal will usually warn you about this. So, how do you exit the REPL cleanly?
What works on every OS is the following Python command: exit()
You can also use a little trick if you’re on Linux or MacOS. By pressing control + d, the REPL will exit immediately. Control + d sends the ‘end of file’ character the to terminal, and the REPL interprets this as a request to exit Python. On Windows, you can do something similar by pressing control + z and then hitting enter.
Learn Python properly through small, easy-to-digest lessons, progress tracking, quizzes to test your knowledge, and practice sessions. Each course will earn you a downloadable course certificate.
Download Article
Download Article
This wikiHow teaches you different ways to open and run a Python script on Windows, macOS, and Linux systems. Simply installing the latest version of Python
3 from Python.org (or by using your Linux distribution’s package manager) gives you the tools you need to edit and run scripts in an Integrated Development Environment (IDE) called IDLE. You’ll also be able to run scripts using the python command in a terminal or command prompt window. Also, if you’re using Windows or macOS, you can use Python Launcher to quickly run Python scripts from Finder or the File Explorer.
Things You Should Know
- If you have IDLE installed, you can simply double-click the Python script to open it.
- Open Terminal, or Command Prompt, navigate to your file, then enter «python [filename]» to open the script in Python.
-
If you haven’t already done so, you’ll need to install the latest version of Python (which is 3.12 as of 10/02/2023), which comes with an Integrated Development Environment (IDE) called IDLE. Here’s how to get it:
- If you’re using Linux, use your distribution’s package manager to install the latest version of Python 3 and then move to Step 2. If you’re using Windows or macOS, keep reading.
- Go to https://www.python.org/downloads/.
- Click the Downloads link at the top of the page and select your operating system.
- Click the Latest Python 3 Release link at the top of the page.
- Scroll to the «Files» section at the bottom of the page.
- If you’re using a Mac, click the macOS 64-bit installer link. If you’re using Windows, click the Windows x86-64 executable installer link.
- Once the download is complete, double-click the installer (it has a name like python-3.8.3-macOS10.9.pkg/exe) and follow the on-screen instructions to install.
- If you’re using Windows, make sure you choose the option to install IDLE when prompted.[1]
-
If you’re using a Mac, it’ll now be on the Launchpad as well as in the Applications folder. If you’re using Windows, you’ll find it the Start menu. If you’re using Linux, just type idle at the command prompt and press the Enter key.
Advertisement
-
It’s at the top-left corner of the screen on a Mac, and at the top-left corner of IDLE in Windows or Linux.
-
The file picker will appear.
-
The script should end with the «.py» file extension. This opens the script for editing.
- If you want to execute the Python script, continue to the next step. Otherwise, feel free to edit the script as needed in IDLE.
-
It’s in the menu bar at the top of the screen (or at the top of IDLE in Windows and Linux).[2]
-
This runs your Python script in IDLE.[3]
Advertisement
-
If you’re just getting started with Python, make sure you install the latest version before you continue.
- See Step 1 of the Using IDLE method for installation instructions.
- If you’re using Linux, you may already have Python 3 installed. If not, use your distribution’s package manager to get the latest version.[4]
-
Here’s how:[5]
- Mac: Open Finder, and then navigate to Applications > Utilities. Double-click Terminal in the list.
- Windows: Type command prompt into the Windows search bar, and then click Command Prompt in the search results.
- Linux: Press the Control + Alt + T keys, or click the Terminal icon on your desktop.
-
The file you’re looking for should end with the «.py» file extension.[6]
- For example, if you’re using a Mac and your .py file is on the desktop, you’d type cd ~Desktop and press the Return key.
- If you’re using Windows and your .py file is in your Documents folder, type cd Documents and press Enter. Or, if you’re not already in your user directory, use the full path (cd C:\Users\Yourname\Documents) instead.
-
Картинка с сайта: www.wikihow.com
For example, if the file is called script.py, you’d type python script.py. This runs the script in Python.
- If you’re using Linux or macOS and have an older version of Python installed, try using python3 <filename> instead. This ensures you’re using the Python 3 interpreter instead of Python 2.
Advertisement
-
If you don’t have Python 3 installed, you can get it from https://python.org.
- See Step 1 of the Using IDLE method for installation instructions.
-
The file should end with the «.py» file extension.[7]
-
A list of applications will expand.
-
This runs the script in Python Launcher.
- You can also open Python Launcher first and drag the Python script to the application window. You’ll find Python Launcher in the Applications folder on a Mac, and in the Start menu on Windows.
Advertisement
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement
Video
Thanks for submitting a tip for review!
Thanks for reading our article! If you’d like to learn more about computer programming, check out our in-depth interview with Kevin Burnett.
References
About This Article
Article SummaryX
1. Install Python 3.
2. Open IDLE.
3. Click File.
4. Click Open.
5. Select your Python file and click Open.
6. Use Run > Run Module to execute the script.
Did this summary help you?
Thanks to all authors for creating a page that has been read 183,299 times.