How to Install Python on UNIX/Linux/Windows/macOS

Beatme CodeBY-NC-ND
12:54 read·1677 words· modified

Python in the Operating System

On some UNIX/Linux systems, you can use Python directly, which is already pre-installed. When you want to do a fresh installation of Python, there are many ways to do it, because there are many distributions of UNIX/Linux that provide different package management tools and installation sources. In addition to using the installation method provided by the distribution, you can also download the Python source code from Python Official and compile it on UNIX/Linux.

On Windows systems, Python is usually installed using the official Python installer, or through other tools such as the Microsoft Store.

macOS is a UNIX-based operating system, and you can use Python2 or Python3 with it, or you can download and install other versions of Python from the official Python repositories, or install other versions through other package management tools.

Install Python on UNIX/Linux with the Installation Tool

Due to the open nature of the system, UNIX/Linux has a large number of branches and versions, and different branches and versions support different package management tools, some of which are listed below.

Debian

In Debian and its derivatives (e.g., Ubuntu), the package management tool is APT.

Fedora

The package management tool in Fedora is DNF.

openSUSE

The package management tool in openSUSE is Zypper.

FreeBSD

The package management tool in FreeBSD is PKG.

OpenBSD

In OpenBSD, packages can be installed or updated with the command pkg_add.

Installing Python on these different UNIX/Linux systems, then, requires an understanding of how to use the corresponding tools. And, because you can have your own installation sources, the version of Python available on each system may be different.

Package management tool APT

Install Python3 using APT, sudo apt install python3.

Package management tool DNF

Install Python3 using DNF, sudo dnf install python3.

Package management tool Zypper

Install Python3 using Zypper, sudo zypper install python3.

Package management tool PKG

Install Python3 using PKG, pkg install python3.

Command pkg_add

Use the pkg_add command to install Python3, pkg_add -r python. The -r option allows the replacement of existing Python packages.

Use the sudo command to elevate privileges

The sudo command is used to temporarily elevate our privileges, as the installation may require higher privileges.

Install a specific version of Python on OpenBSD using the pkg_add command

To install a specific version of Python, you can look up the corresponding tgz file from the official OpenBSD page and pass its URL to the pkg_add.

The following command is used to install version 3.9.18 of Python with the amd64 instruction set provided by OpenBSD 7.4, pkg_add https://ftp.openbsd.org/pub/OpenBSD/7.4/packages/amd64/python-3.9.18.tgz.

Install Python on Windows with the Installer

The official Python installer for Windows contains all the components needed to develop Python. The items with Windows installer in their name on the download page correspond to the installers, which are currently available in three versions, 32-bit, 64-bit, and ARM64.

Python Releases for Windows | Python.org

Differences between the 32-bit, 64-bit, and ARM64 versions of the Python Windows installers

The 32-bit' and 64-bit’ versions of the Python Windows installer target Windows operating systems that support x86 processors and x64 processors, respectively, and are typically installed on personal desktop computers or servers. The ARM64 version of the Python Windows installer targets Windows operating systems that support ARM processors, which are typically installed on mobile devices.

If you are using a desktop computer, then you should generally choose the 64-bit version.

After launching the Python Windows installer, you can see Install Now and Customize installation, as well as the following two options.

Use admin privileges when installing py.exe option

If the Use admin privileges when installing py.exe option is enabled, the Python launcher will be installed as a Windows administrator. It should be noted that this option does not determine whether Python itself is installed as a Windows administrator, but conversely, if the Install Python … for all users option is enabled, the Python launcher will be installed as an administrator.

Add python.exe to PATH option

If the Add python.exe to PATH option is enabled, then the Python installation directory and the path to the Scripts folder of the installation directory are added to the Windows environment variable PATH, which allows you to run Python or pip by typing python or pip on the command line.

When selecting Customize installation for the installer, you will be able to choose whether or not to install the following optional features.

Documentation feature

If you enable the Documentation feature, the relevant documentation for Python will be installed, and they will be located in the Doc folder of the Python installation directory.

pip feature

If the pip feature is enabled, pip for Python package management will be installed and its executable (exe) will be located in the Scripts folder of the Python installation directory. It is recommended that this feature be enabled, as pip is frequently used.

tcl/tk and IDLE feature

If the tcl/tk and IDLE feature is enabled, tcl/tk (including the Python package tkinter) and a simple Python development environment (as the Python package idlelib) will be installed.

Python test suite feature

If the Python test suite feature is enabled, the test-related Python package test will be installed.

py launcher feature

If you enable the py launcher feature, the Python launcher will be installed, and it will be located in a new installation directory. After a successful installation, you can uninstall the Python launcher separately in Windows.

for all users feature

This function serves the same purpose as the option Use admin privileges when installing py.exe. If this feature is not available, it may be necessary to uninstall the previous installation.

After clicking the Next button, you will be able to set the Python installation directory and choose whether to enable the following advanced options.

Install Python … for all users option

If the Install Python ... for all users option is enabled, Python will be installed for all Windows users, which of course requires the Windows administrator account.

Associate files with Python option

If the Associate files with Python option is enabled, Python files will be associated on Windows systems. If this option is not available, you need to enable the feature py launcher.

Create shortcuts for installed applications option

If the Create shortcuts for installed applications option is enabled, related shortcuts, such as Windows menu items, will be created.

Add Python to environment variables option

This option has the same effect as the option Add python.exe to PATH.

Precompile standard library option

If the Precompile standard library option is enabled, Python's standard library will be precompiled, which will generate the associated pyc files in the __pycache__ folder. Due to the general increase in performance of modern computers, precompiling or not will not make much difference.

Download debugging symbols option

If the Download debugging symbols option is enabled, symbols for debugging will be downloaded, which can be enabled to facilitate debugging of the code.

Download debug binaries option

If the Download debug binaries option is enabled, the debugging symbols for Visual Studio (some pdb files) will be downloaded.

When Install Now is selected for the installer, the following features and options are enabled by default, Documentation, pip, tcl/tk and IDLE, Python test suite, py launcher, Associate files with Python, Create shortcuts for installed applications, Precompile standard library.

Install Python on Windows with the Embeddable Package

Python is officially available as an embeddable package for Windows, which contains only the minimum components required to run Python (no pip, IDLE, etc.), and is primarily used for embedding Python into other applications, with the standard library already pre-compiled.

On the download page, the items with Windows embeddable package in their name correspond to the embeddable packages, which are available in three versions, 32-bit, 64-bit, and ARM64, with the same differences between the three versions as the Python Windows installer.

Python Releases for Windows | Python.org

Install Python on Windows Using the Microsoft Store

The Microsoft Store for Windows is also one way to install Python, just type python in the store’s search box to search for it, and then select the appropriate version to install; the installation will contain pip and IDLE components.

The permissions for Python provided by the Microsoft Store are restricted

It should be noted that Python installed through the Microsoft Store will be more restricted in terms of permissions, and you may not be able to actually read or write certain important files; it will be similar to a sandbox, where protected content will be accessed as a copy.

Install Python on Windows with NuGet

Although NuGet is a .NET package management tool, it provides installation of Python. Entering and executing the following command at the command line will install the 64-bit or 32-bit version of Python from NuGet in the current folder.

64-bit
nuget install python -OutputDirectory . -ExcludeVersion
32-bit
nuget install pythonx86 -OutputDirectory . -ExcludeVersion
-OutputDirectory argument

The -OutputDirectory argument is used to determine the installation directory, and Python will be located in the tools subfolder of the package folder (which exists in the installation directory). Of course, the -OutputDirectory . in the above command is redundant because the current folder is the default installation directory.

-ExcludeVersion option

The -ExcludeVersion option allows the package folder name to not include the version number of Python, which is included by default, e.g. python.3.12.1.

Install Python on macOS with the Installer

On Python’s official download page for macOS, you can select the desired version and download the corresponding pkg file, or dmg file for earlier versions.

Python Releases for macOS | Python.org

Do not uninstall the pre-installed version of Python on macOS

After you’ve installed one of the officially available versions of Python, don’t feel free to uninstall the pre-installed version for macOS, as some programs may still use it.

Verify Python Installation

After successfully installing Python, you can view Python’s version information by typing the following command at the command line of your system.

Windows
python --version
UNIX/Linux/macOS
python3 --version

Make sure that the directory where Python is located has been added to the PATH environment variable

To run correctly, make sure that the directory containing the Python executable or a link to the executable has been added to the environment variable PATH. Otherwise, you may encounter an error like “Command does not exist”.