How to create a virtual environment in Python?
Published on Aug. 22, 2023, 12:15 p.m.
To create a virtual environment in Python, you can use the venv
module that comes included with Python. Here are the steps to create a virtual environment:
- Open up your terminal and navigate to the directory where you want to create the virtual environment.
- Run the command
python3 -m venv env
to create a new virtual environment named “env”. You can replace “env” with any name of your choice. - Activate the virtual environment by running the command
source env/bin/activate
on macOS/Linux or.\env\Scripts\Activate
on Windows. - You should see the name of your virtual environment appear in your command prompt.
You can now install any packages you need within this virtual environment without affecting the global installation of Python packages on your system. To exit the virtual environment, simply run the command deactivate
in your terminal.
using Conda
Sure, here are the steps to create a virtual environment using Conda:
- Open up your terminal and run the command
conda create --name env
to create a new virtual environment named “env”. You can replace “env” with any name of your choice. - You can specify the version of Python you want to use in the virtual environment by adding the argument
python=X.X
, where “X.X” is the version number. For example,conda create --name env python=3.7
will create a virtual environment with Python 3.7 installed. - Activate the virtual environment by running the command
conda activate env
. You should see the name of your virtual environment appear in your command prompt. - You can now install any packages you need within this virtual environment without affecting the global installation of Python packages on your system. To exit the virtual environment, simply run the command
conda deactivate
in your terminal.
One of the main advantages of using Conda is that it allows you to create and manage isolated Python environments, which can be especially useful when working on multiple projects that have conflicting dependencies or require different versions of packages. Conda also provides a convenient way to install, update, and remove packages within these environments.
Another advantage of Conda is its support for non-Python packages and libraries, which can be installed and managed using the same tool. This makes it easy to work with a wide range of scientific computing tools and libraries that may be required for data analysis, machine learning, or scientific research.
Conda is also platform-independent, and can be installed on Windows, macOS, and Linux operating systems. Additionally, Conda is open-source and community-supported, which means that it is continuously evolving and improving to meet the needs of its users.
How to install Conda on Linux,
you can follow these steps:
- Download the appropriate Conda installation script for your Linux distribution from the official Conda website. Make sure to choose the version of Python you want to use in your environment.
- Open up your terminal and navigate to the directory where you downloaded the Conda installation script.
- Run the command
bash Anaconda3-<version>-Linux-x86_64.sh
to start the installation process. Replace<version>
with the version number of the Conda installation script you downloaded. - Follow the prompts to complete the installation. You will be prompted to review and agree to the license terms, as well as to choose the installation location and other settings.
- Once the installation is complete, you can use the
conda
command to create and manage virtual environments and install packages.
To install Miniconda on Linux
you can follow these steps:
- Download the appropriate Miniconda installation script for your Linux distribution from the official Conda website. Make sure to choose the version of Python you want to use in your environment.
- Open up your terminal and navigate to the directory where you downloaded the Miniconda installation script.
- Run the command
bash Miniconda3-<version>-Linux-x86_64.sh
to start the installation process. Replace<version>
with the version number of the Miniconda installation script you downloaded. - Follow the prompts to complete the installation. You will be prompted to review and agree to the license terms, as well as to choose the installation location and other settings.
- Once the installation is complete, you can use the
conda
command to create and manage virtual environments and install packages.