Elastic Compute ServiceElastic Compute ServiceBest PracticesSelf-service website creationBuilding environmentsSetting up a Python environment
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.
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.
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 yum update -y
Execute the following command to query installable Python packages in the software repository.
sudo yum list available | grep -E '^python3[0-9]\.(x86_64|aarch64|noarch)'
When the following output appears, Python 3.8.17 and 3.9.19 can be installed.
Execute the following command to install the Python package corresponding to the instance architecture.
sudo yum install -y python39.x86_64
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.19 has been successfully installed.
In the examples in this section, Python 3.9.22 is installed on Ubuntu 20.04 and CentOS Stream 8 operating systems. For other image versions, install dependency libraries based on your business requirements to ensure system compatibility.
Install various dependency libraries and tools required for Python compilation, including compilers, development libraries, and other necessary packages.
veLinux/Ubuntu/Debian
CentOS
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
Execute the following command to install various dependency libraries and tools required for Python compilation, including compilers, development libraries, and other necessary packages.
Execute the following command to install various dependency libraries and tools required for Python compilation, including compilers, development libraries, and other necessary packages.
In the example in this section, Python 3.9.22 is installed. Replace the download link with the download link of the Python version that you want to install. Learn more.
Since cross-border data downloads are involved, the download might take a long time. You can also download the source code package to your local environment and then upload the package to the ECS instance. Learn more.
Execute the following command to decompress the downloaded Python 3.9.22 source code package.
tar -xf Python-3.9.22.tgz
Execute the following command to switch the current working directory to the Python 3.9.22 source code directory.
cd Python-3.9.22
Execute the following command to execute the Python configuration script, configure the compilation environment for the Python interpreter, and enable performance optimization options.
./configure --enable-optimizations
Execute the following command to compile Python source code based on the configuration file (Makefile) by using the Make tool.
sudo make -j$(nproc)
Compilation success example:
Execute the following command to install the compiled Python interpreter in the /usr/local/bin/ directory.
sudo make altinstall
Installation complete example:
Execute the following command to view the Python version and verify whether Python 3.9.22 has been successfully installed.
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.
Execute the following command to create a virtual environment.
/usr/local/bin/python3.9 -m venv myenv
tip
Replace /usr/local/bin/python3.9 with the path of the target Python version.
Execute the following command to enter the virtual environment.
source myenv/bin/activate
Sample output:
Execute the following command to confirm the Python version used in the current environment.
python --version
Sample output:
You can also execute the following command to exit the virtual environment. For more Python virtual environment actions, view the official venv documentation.
Execute the following command to view the current default Python 3 version.
python3 --version
Sample output:
Execute the following command to view configurable Python 3 versions.
sudo update-alternatives --display python3
When the following output appears, you can select Python 3.8 or Python 3.9 as the default Python 3 version.
(Optional) If the configurable Python 3 versions do not include the version you want to use, execute the following command to add the desired Python version as an option.
Replace /usr/local/bin/python3.9 with the actual path to your Python version.
Sample output:
Select the default version.
Execute the following command to view the Python versions that you can select.
sudo update-alternatives --config python3
Sample output:
Enter the serial number of the file that you want to use as the default Python version based on your business requirements. In the example in this topic, Python 3.9 is specified as the default version.
Sample output:
Execute the following command to confirm the current default version of Python 3.
python3 --version
When the following output appears, you have successfully switched the default version of Python 3 to 3.9.22.
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.
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.
After the download is complete, double-click the downloaded Python executable file to start the installation process.
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.
After the installation is complete, click the "Close" button to close the installation page.
Click the Windows icon, and click "Windows System > Command Prompt".
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.
Click the Windows icon and click "Windows System > Run".
Enter the following content in the dialog box, and click the "OK" button to open the system attribute dialog box.
sysdm.cpl
Select the "Advanced" tab, and click the "Environment Variables" button.
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.
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:
Click the "OK" button in sequence to save the changes and close the "System Properties" dialog box.
Click the Windows icon and click "Windows System > Command Prompt".
Execute the following command to check whether the default Python version has been successfully changed.