How to set up a Python environment in Linux?
Configuring a Python environment in a Linux system can be done through the following steps:
- Check the system’s built-in Python version: Enter the following command in the terminal to view the system’s built-in Python version:
- What is the version of Python installed?
- Installing Python: If your system does not have Python or you need to install a new Python version, you can do so through a package manager. For example, on Ubuntu systems, you can use the following command to install Python3:
- Update and install Python3 using the following commands:
sudo apt update
sudo apt install python3 - Using virtual environments: To prevent the system Python environment from being altered and affecting other programs, it is recommended to use virtual environments. You can install virtual environment tools using the following command:
- Please execute ‘sudo apt install python3-venv’ command.
- Create a virtual environment: Use the following command in the project directory to create a new virtual environment:
- Create a virtual environment named ‘env’ using Python 3.
- Activate the virtual environment using the command:
- Activate the environment by sourcing the ‘env/bin/activate’ script.
- Install Python packages: In a virtual environment, you can install the necessary Python packages, for example, installing the requests package.
- install requests using pip
- Exit the virtual environment: After completing your work, use the following command to exit the virtual environment:
- turn off
By following the steps above, you can configure a Python environment on a Linux system and independently manage the Python versions and packages required for projects.