How to create a Python virtual environment using Conda

Installing Conda will help you create a Python virtual environment more flexibly because you can choose the version of Python you want. For parts that are rarely used in work, Hieu will guide you with GUI (Graphical User Interface) to help you remember longer. Hieu will guide with CLI (Command Line Interface) for frequently used parts.

Compare Anaconda and Miniconda

  • Conda: This is a popular virtual environment and package management system for Python and R.
  • Anaconda Navigator: This application helps Conda (Miniconda, Anaconda) have an intuitive interface.
CompareAnacondaMiniconda
IncludeMiniconda and many quality packages for data science in the base environment.Conda and Python.
GUIData scientists, who need packages highly compatible with each other, do not pay much attention to the Python virtual environment but mainly use the base environment.Install Anaconda Navigator yourself.
CapacityHeavy.Light.
FitData scientists, who need packages that are highly compatible with each other, do not pay much attention to the Python virtual environment but mainly use the base environment.Programmers who want to install the packages they need themselves often create Python virtual environments for projects.

Imagine you are doing data science and need two packages from two authors. If you use Python or Miniconda, then there is a possibility that the latest versions of these two packages conflict with each other.

Anaconda has included the best versions of each data science package to ensure they are as compatible as possible. You won’t have to spend time finding errors and the best package version, but you will waste a lot of space on Anaconda.

Install Conda

Whether you install Anaconda or Miniconda, they still have a GUI called Anaconda Navigator. The difference lies in the number of packages available in the base environment.

Anaconda Navigator interface after installing Conda
Anaconda Navigator interface after installing Conda

Install Anaconda

You visit the Anaconda download page to download the Anaconda installation file. Installing Anaconda is also simple; click the Continue button until the installation is successful.

The installation process for Anaconda and Miniconda is the same
The installation process for Anaconda and Miniconda is the same

Go to Anaconda’s Environments, and you will see that the base environment has more than 500 packages available. If you do data science, using Anaconda’s base environment is best.

The difference is that Anaconda's base environment has many available packages
The difference is that Anaconda’s base environment has many available packages

To uninstall Anaconda, see Uninstalling Anaconda Distribution.

Install Miniconda

You visit the Miniconda download page to download the Miniconda installation file. If your device is macOS, download the pkg file. The installation method is the same as Anaconda.

After successfully installing Miniconda, open Terminal (CMD) to install Anaconda Navigator with this command.

conda install anaconda-navigator

Miniconda’s base environment only has about 150 packages.

Create a virtual Python environment using Conda

Creating a Python virtual environment using Conda is essential for the following purposes:

  • To share information about the necessary packages of a project to the public or members of that project.
  • To avoid conflicts for some packages incompatible with existing packages in virtual environments.

To create a new Python virtual environment using Conda, go to Environments, click the Create button, enter the environment name and select the Python version.

Create a Python virtual environment in Anaconda Navigator
Create a Python virtual environment in Anaconda Navigator

You can also create a Python virtual environment with this command in Terminal (CMD).

conda create -n <env_name> python=3.12

When creating a Python virtual environment, the results of Anaconda and Miniconda are the same, do not inherit from the base and only contain nearly 20 basic packages.

Use Python virtual environment

We often activate the Python virtual environment and install packages daily, so Hieu will guide you on how to do it on CLI. If you frequently use an IDLE like Pycharm, read more about adding a Conda virtual environment to Pycharm.

Activate virtual environment

Activate virtual environment base.

conda activate

Activate the Python virtual environment you created.

conda activate <env_name>

Deactivate the Python virtual environment.

conda deactivate

Once you have activated the Python virtual environment, you can use Python on Terminal (CMD) according to the instructions in the article on how to install Python.

Install packages

It would be best to prioritise installing packages from Conda because they will be the best versions, have been patched, and are the most compatible.

conda install <package>

If you need a package that is only available on PyPI.org or the latest version of that package, use Pip.

pip install <package>

Backup and import Python virtual environment

To export information about a Python virtual environment using Conda, select that virtual environment and click the Backup button.

Backup Python virtual environment with Conda
Backup Python virtual environment with Conda

To import a Python virtual environment using Conda, click the Import button and select the file in YAML format.

Import Python virtual environment using Conda
Import Python virtual environment using Conda

In addition, you can also clone and delete a virtual environment with simple operations on Anaconda Navigator.

Conclusion

If you prefer purely CLI operations, read Conda’s documentation; they write it well. Hieu finds that creating and sharing a Python virtual environment using Anaconda Navigator is the most effective. Once Conda has made this application, we must take advantage of it.

Related articles

Hieu Tran Ngoc Minh

Hieu Tran Ngoc Minh

I am currently working as a Data Analyst; before that, I worked in Digital Marketing. Blogging is a joy, helping me share my knowledge and experiences from life and work. You can donate to me here.

Leave a Comment

Feel free to leave your comment, and we will review and respond as soon as possible. Please use a real email to ensure your comment is approved and to receive notifications when we reply. You can also add an avatar to your email.