How to install Python: The complete Python programmer's guide

Mary-Kate Olsen
Release: 2024-10-18 16:27:48
Original
590 people have browsed it

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.

How to install Python: The complete Python programmers guide

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.

Table of contents

  • Terminals: where and how to run a python script
  • How do I set up my system for programming in Python?
  • How do I install Python on my operating system?
  • What about Python IDEs?
  • Running your first script in a terminal
  • How do I update Python on my operating system?

Terminals: where and how to run a python script

Before we dive into setting up your system so you can program in Python, let’s talk terminal basics and benefits.

What’s a terminal?

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. 

What are the benefits of using a terminal?

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:

  • Install, update, and delete Python on some systems
  • Run Python files
  • Add Python libraries
  • Check the Python version

How does a terminal work?

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.

How do I set up my system for programming in Python?

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.

How to open a terminal in Linux

There are different ways to open a terminal depending on your Linux distributions:

  • Press Ctrl   Alt   T 
  • Access the application menu or launcher, which might be called Activities, Show Applications, or something similar 
  • Search for Terminal and click the Terminal icon

How to open a terminal in Windows

There are two different built-in options for terminals in Windows. You can open the command prompt or PowerShell:

  • Press the Windows button   R, type “cmd” in the Run dialog, and press Enter
  • Search for “cmd” or “PowerShell” in the Start bar and press Enter. If you don’t have a search box in the Start bar, click on the Windows icon first and type there.

How to open a terminal in macOS

On macOS, the Terminal app provides access to the Unix part of the OS. Here are two options to open it:

  • Use Spotlight search: Press cmd   Space to bring up the Spotlight search box, type “Terminal,” and press Enter.
  • Use the Finder window: Navigate to Applications > Utilities and click on Terminal

Check if Python is installed by checking the Python version

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
Copy after login
Copy after login
Copy after login
Copy after login

Or, for some systems, you might need:

      python3 --version
Copy after login
Copy after login
Copy after login
Copy after login

Or for Windows:

      py --version
Copy after login
Copy after login
Copy after login

This command will return the version number like the example below if Python is installed.

How to install Python: The complete Python programmer's guide

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.

How to choose the right 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.

But what about Python 2?

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.

How do I install Python on my operating system?

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).

How to install Python on Linux

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
Copy after login
Copy after login
Copy after login
Copy after login

And then type:

      python3 --version
Copy after login
Copy after login
Copy after login
Copy after login

How to install Python on Windows

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.

How to install Python: The complete Python programmer's guide

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.

How to install Python: The complete Python programmer's guide

How to install Python on macOS

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
Copy after login
Copy after login
Copy after login
Copy after login

Once you’ve installed Homebrew, you can easily install Python by running:

      python3 --version
Copy after login
Copy after login
Copy after login
Copy after login

This command installs Python 3 and Pip3. Pip3 is the Python package installer you’ll need when using Python.

Verify the Python installation

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.

What about Python IDEs?

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:

  • PyCharm by JetBrains
  • Visual Studio Code (VS Code)
  • Jupyter Notebook (commonly used for data analysis)

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.

Running your first script in a terminal

For this example, I’ll use VS code to create a file with a .py extension.

How to install Python: The complete Python programmer's guide

I’ve called my file hello.py and gave it the content:

      py --version
Copy after login
Copy after login
Copy after login

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:

How to install Python: The complete Python programmer's guide

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:

How to install Python: The complete Python programmer's guide

How do I update Python on my operating system?

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.

How to update Python on Linux 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. 

How to update Python on Windows

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
Copy after login
Copy after login
Copy after login
Copy after login

or

      python3 --version
Copy after login
Copy after login
Copy after login
Copy after login

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.

How to install Python: The complete Python programmer's guide

How do I uninstall Python from my operating system?

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. 

How to uninstall Python on Linux

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
Copy after login
Copy after login
Copy after login

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
Copy after login

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
Copy after login

These remove packages automatically installed to satisfy dependencies for Python that are no longer needed.

How to uninstall Python on Windows

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). 

How to install Python: The complete Python programmer's guide

Search for Python, click on the three dots next to the version you want to uninstall, and choose Uninstall.

How to install Python: The complete Python programmer's guide

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.

How to install Python: The complete Python programmer's guide

And boom, it’s gone!

You can also go to the Control Panel > Programs and select “Uninstall a program.”

How to install Python: The complete Python programmer's guide

It then shows a list of programs you can uninstall. Right click on the one you want and select Uninstall.

How to install Python: The complete Python programmer's guide

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!