An easy guide on how set up your operating system so you can program in Python, and how to update or uninstall it. For Linux, Windows, and macOS.
Whether it’s your first time programming or you’re a seasoned programmer, you’ll have to install or update Python every now and then --- or if necessary, uninstall it. In this article, you'll learn how to do just that.
Some systems come with Python, so to start off, we’ll first check to see if it’s installed on your system before we proceed. To do that, we’ll need to open a terminal. Since you might be new to programming, let’s go over how to open a terminal for Linux, Windows, and macOS.
Before we dive into setting up your system so you can program in Python, let’s talk terminal basics and benefits.
As a user, you’re familiar with clicking on your screen to control your computer (this is called using a graphical user interface, or GUI). A terminal, also known as a command line or console, is a different way to control the computer. Instead of clicking, we enter text commands to give the computer instructions.
You might wonder why we need a terminal since clicking a mouse seems a lot more convenient than typing commands—especially when you don’t yet know which commands to type. But there are quite a few benefits to using a terminal, including quick access to all available commands and easier task automation.
However, let’s focus on why we need it for working with Python. We use a terminal, or command line, to:
When you open the terminal, it waits for you to type a command, and then the shell executes it. The shell is a special program that turns the command into actions for the computer’s operating system. You’ll get the result or feedback from the command in the terminal.
Don’t worry about the commands. Once you manage to open the terminal, I’ll tell you which commands to use.
Now let’s dive into the steps you can follow to set up your system to program in Python. First, we’ll open a terminal. Here’s how to open a terminal in Linux, Windows, and macOS.
There are different ways to open a terminal depending on your Linux distributions:
There are two different built-in options for terminals in Windows. You can open the command prompt or PowerShell:
On macOS, the Terminal app provides access to the Unix part of the OS. Here are two options to open it:
Now that we have the terminal open, we can check if Python is installed. In your terminal or command prompt, type the following command:
python --version
Or, for some systems, you might need:
python3 --version
Or for Windows:
py --version
This command will return the version number like the example below if Python is installed.
If Python isn’t installed, you'll see an error message saying Python is unknown or isn't installed, recognized, or added to your system's PATH.
Depending on the outcome, you might be done. If you check the version and it has the version number you want, you’re ready to go.
Let’s make sure you know how to pick the correct Python version.
Unfortunately, I can’t tell you which version you’ll need. The version depends on what you need to do.
The two major versions of Python are 2 and 3. But versions can look a little different. For example, the latest version at the time of this article is 3.12.3. As you can see, the version number is a lot longer than just the number 3. But since it starts with 3, I'm talking about Python 3.
Let’s start with a very common use case for people installing Python: starting a new project. If you want to start a new project, grab the latest version of Python 3.
Good question. If you’re working on an existing project, the Python versions should be the same. For example, you’ll need version 2 when working on an existing project that uses Python 2. (We typically call Python 2 “legacy.”) Most projects will be Python 3. If you need to work on an existing project that uses a specific version of Python 3, you can use the latest version of Python 3 on your computer.
But—and this is an important but—if you work on an existing project that’s on version 3 but a lower version of Python 3 on the server, it might not support all the newest Python features. To ensure compatibility, choose the specific version of 3 the project is on.
Here’s a good rule of thumb: use the latest version unless you have project requirements that need an older one.
Now that you know which version you’ll need, let’s install it. Jump to the section that applies to your operating system (Linux, Windows, or macOS).
Most Linux distributions come with Python pre-installed. If you need to install Python 3, you can use the package manager for your Linux distribution. Here’s what you can use for Ubuntu and other Debian-based systems:
First, type:
python --version
And then type:
python3 --version
We’ll need to download Python first to install it on Windows. You can get the Python installer from the official Python website. Scroll down to the version you need.
After downloading it, you’ll need to run the installer, which you can find in your Downloads folder.
Make sure to check the box that says “Add python.exe to PATH” to make Python accessible from the command line.
After that, click Install Now and follow the installer prompts to complete the installation.
Like Linux, your macOS probably has a version of Python because the system comes with Python 2 installed by default. However, it’s likely you’ll need to install Python 3. Here’s the easiest way to do it.
If you’re not using Homebrew yet, let’s install it. (You can thank me later.) Homebrew is a package manager for macOS that allows you to use simple commands in the terminal to install, upgrade, and uninstall all sorts of applications. Fun fact: Being able to write Python is an application in itself.
Here’s how to install Homebrew. Head to the Homebrew website and copy-paste the command into your terminal. This is what the command currently looks like:
python --version
Once you’ve installed Homebrew, you can easily install Python by running:
python3 --version
This command installs Python 3 and Pip3. Pip3 is the Python package installer you’ll need when using Python.
After installing Python, we can check to see if we installed it correctly. Close the terminal you opened and open a new one. Then check the version again by typing in the version-checking command from earlier in this guide.
You should see the Python version number you installed if the installation was successful.
You can use the most basic text editor, like Notepad on Windows, to write Python code. However, it’s likely you’ll benefit from a slightly fancier application.
An integrated development environment (IDE) is an application that can make your coding experience a lot smoother. It includes features like syntax highlighting, code completion, and built-in debugging and running tools. The three most popular IDEs for Python are:
I use all three but for different purposes. I’ll go for PyCharm when I want to develop applications such as APIs or apps with a GUI. I prefer VS Code when I want to use Python for automation tasks. And Jupyter Notebook is my preferred option for statistical and data purposes.
For this example, I’ll use VS code to create a file with a .py extension.
I’ve called my file hello.py and gave it the content:
py --version
There are different ways to run your code from VS Code. You can see mine shows the play icon (▷) in the right upper corner due to an extension I have called Code Runner. I want to show a more universal way of running this code.
We need to open the folder with the hello.py file in the terminal. There are different ways to do it, but the easiest is to right click in the folder and choose an option such as Open with Terminal.
It looks like this on Windows:
In the terminal that pops up, type “python” or “python3” or “py”—whichever gave you the successful version—followed by the name of the file, including the extension. Press Enter. It should show you the “Hello world” message in the terminal.
This is what the little program did while executing it:
You’ll want to make sure you’re running the latest version of Python on your system. Here’s how to update Python for Linux, Windows, and macOS.
To update Python on Linux and macOS, simply download the latest
version from the official Python website and run the installer. It will
replace the older version with the newer one.
To update Python on Windows you download the latest version and run
the installer. It will add the version as opposed to replacing it, and
you can select which version to choose with the version flag. For
example:
python --version
or
python3 --version
This will run using the specified version of Python. If you don’t specify the version, it will grab the one that’s on the PATH. Which typically is the latest one.
Now that we know how to update Python on Linux, Windows, and macOS, let’s see how to uninstall it. And of course, the process to uninstall Python varies by operating system.
Uninstalling Python from a Linux system varies slightly depending on the distribution and how Python was installed. We’re going to use the package manager apt for Debian-based systems.
Note: Many Linux distributions rely on Python for system
operations and various software tools. Uninstalling the default system
Python could cause system stability issues—and we don’t want that.
First, make sure Python is installed. Go ahead and open a terminal and type:
py --version
This command will display the version of Python 3 if it's installed. If it is, we can go ahead and uninstall Python with the apt-get command:
sudo apt update
If you have multiple versions installed, and you want to uninstall a specific version, you can replace “python3” with the specific version (e.g., “python3.9”).
You can also remove files and dependencies you don’t need anymore after uninstalling. You can do that by entering the following two commands in the terminal:
sudo apt install python3
These remove packages automatically installed to satisfy dependencies for Python that are no longer needed.
You have a couple options to uninstall Python on Windows. First, you can uninstall Python via the Add or Remove Programs feature (search for it in the search box).
Search for Python, click on the three dots next to the version you want to uninstall, and choose Uninstall.
Confirm this option in the pop-up box by clicking Uninstall. Python will then start uninstalling. Windows confirms the successful uninstall when it’s done.
And boom, it’s gone!
You can also go to the Control Panel > Programs and select “Uninstall a program.”
It then shows a list of programs you can uninstall. Right click on the one you want and select Uninstall.
No confirmation needed. It’s gone!
The above is the detailed content of How to install Python: The complete Python programmer's guide. For more information, please follow other related articles on the PHP Chinese website!