To create a Python virtual environment in Linux, follow these steps:
Open a terminal and make sure you have Python 3 installed. You can check your Python version by running the following command:
python3 --version
Install the venv
module, which is used to create virtual environments. You can do this by running the following command:
sudo apt-get install python3-venv
Navigate to the directory where you want to create your virtual environment.
Run the following command to create a virtual environment with the name myenv
:
python3 -m venv myenv
Activate the virtual environment by running the following command:
source myenv/bin/activate
Your virtual environment is now ready to use. You can install packages and run Python scripts inside the virtual environment without affecting the packages and scripts outside of it. To deactivate the virtual environment, simply run the deactivate
command.