You need to enable JavaScript to run this app.
Elastic Compute Service

Elastic Compute Service

Copy page
Download PDF
Building environments
Setting up a Python environment
Copy page
Download PDF
Setting up a Python environment
You can install or upgrade the Python 3 environment on an ECS instance. This way, you can leverage the powerful features of the Python language and manage the development environment in a flexible manner.​
Prerequisites​
You have purchased an ECS instance and associated an EIP with the instance.​
Set up a Python environment in Linux​
Install/Upgrade Python​
You can use the Advanced Packaging Tools (APT) or manually download and compile the source code to install the required Python version.​
tip​
  • If you use the APT, you can quickly install Python. However, the Python version provided in the software repository may not be the latest version or the specific version you need. For example, you might require Python 3.9.22. Under such circumstances, we recommend that you manually compile the source code and install the required Python version.​
  • Many system tools, such as yum, apt, cloud-init, systemd, and netplan depend on the default Python version of the operating system. Therefore, when you upgrade the Python version, we recommend that you manually compile the code and install the new version of Python. We recommend that you do not uninstall the old Python version or replace the /usr/bin/pythonor /usr/bin/python3 symbolic link. Otherwise, system exceptions may occur or the instance may fail to start. After the upgrade, you can use the new version of Python by referring to the instructions in this topic.​
​
Use the APT tool
Manually compile the code
​
In the examples in this section, Python 3 is installed on Ubuntu 20.04 and CentOS Stream 8 operating systems by using the package manager. For other image versions, install the Python version based on your business requirements.​
​
veLinux/Ubuntu/Debian
CentOS
​
  1. Execute the following command to update the local software package index. This ensures that the package information in the system is up-to-date.​
  • ​
    sudo apt update​
    ​
  1. Execute the following command to query installable Python packages in the software repository.​
  • ​
    apt-cache search python | grep -E '^python3\.[0-9]+\s'​
    ​
  • When the following output appears, Python 3.8 and 3.9 can be installed.​
  • ​​​
  1. Execute the following command to install the Python package corresponding to the instance architecture.​
  • ​
    sudo apt install -y python3.9​
    ​
  1. Execute the following command to check whether Python 3.9 has been installed successfully.​
  • ​
    python3.9 --version​
    ​
  • When the following output appears, Python 3.9.5 has been successfully installed.​
  • ​​​
​
​
Use the specified version of Python​
If multiple versions of Python are installed on your ECS instance, you can use the specified version of Python based on your business requirements by using the following methods.​
warning​
  • After you install the new version of Python, we recommend that you use the specified version of Python by using the Python interpreter with a version number, specifying the default Python version for the current user, or using a virtual environment.​
  • If you want to specify the default Python version for the system, make sure that this action will not affect the operating system components and business operations.​
​
Use the Python interpreter with a version number
Specify a Python version for the target user
Use a virtual environment
Specify the default Python version of the system
​
  1. Use Python 3 interpreters with version numbers in locations, such as the command line and code. Example:​
  • View the Python version​
  • ​
    /usr/local/bin/python3.9 --version​
    ​
  • Sample output:​
  • ​​​
  • View the PyPI version​
  • ​
    /usr/local/bin/pip3.9 --version​
    ​
  • Sample output:​
  • ​​​
​
Set up a Python environment in Windows​
In this example, Python 3 is installed on the English version of the 64-bit Windows Server 2022 operating system.​
Step 1: Install Python​
  1. Visit the official Python website in your browser.​
  1. Select the Python version installer you want to install and download the installer.​
  • ​​​
tip​
  • In this example, 64-bit Python 3.12.5 is installed. Download the Python version installer you want to use.​
  • Since cross-border data downloads are involved, the download might take a long time. You can also download the Python version installer to your local environment and then upload the installer to the ECS instance. Learn more.​
  1. After the download is complete, double-click the downloaded Python executable file to start the installation process.​
  • ​​​
  1. Select "Use admin privileges when installing py.exe" and "Add python.exe to PATH" and then click the "Install Now" button.​
  • ​​​
tip​
In this example, all components in the Python executable file are installed. This is the best practice for developers who want to use Python for any type of project. You can also click "Customize installation" to manually select the components you want to install. Learn more.​
  1. After the installation is complete, click the "Close" button to close the installation page.​
  • ​​​
  1. Click the Windows icon, and click "Windows System > Command Prompt".​
  • ​​​
  1. Execute the following command to check whether Python has been successfully installed and configured.​
  • ​
    python​
    ​
  • Sample output:​
  • As shown in the figure below, you have successfully installed Python 3.12.5 on the instance.​
  • ​​​
Step 2: Specify the default Python version of the system (optional)​
If multiple versions of Python are installed on your ECS instance, you can modify the default Python version of the system.​
  1. Click the Windows icon and click "Windows System > Run".​
  • ​​​
  1. Enter the following content in the dialog box, and click the "OK" button to open the system attribute dialog box.​
  • ​
    sysdm.cpl​
    ​
  • ​​​
  1. Select the "Advanced" tab, and click the "Environment Variables" button.​
  • ​​​
  1. Select the "Path" parameter in the "User variables for Administrator" area, and click the "Edit..." button.​
  • ​​​
tip​
The path information of the Python versions installed in this topic is configured in the "User variables for Administrator" area, rather than the "System variables" area. Select the "Path" parameter where the Python path information is located based on actual conditions.​
  1. Select the Python version you want to use by default, and click the "Move Up" button to move it to the top.​
warning​
You need to move both paths of the target Python version to the top.​
  • Example before adjustment:​
  • ​​​
  • Example after adjustment:​
  • ​​​
  1. Click the "OK" button in sequence to save the changes and close the "System Properties" dialog box.​
  1. Click the Windows icon and click "Windows System > Command Prompt".​
  • ​​​
  1. Execute the following command to check whether the default Python version has been successfully changed.​
  • ​
    python --version​
    ​
  • Sample output:​
  • ​​​
Last updated: 2025.09.28 10:31:44