Conda commands (create virtual environments for Python with conda) - Deep Learning Garden (2023)

This post contains some commonly used conda commands.

(I use both Mac and Linux operating systems, so the commands here assume you're using Mac and Linux operating systems. If you're using Windows, most conda commands should be the same, but some commands may differ slightly For example the I'm aware that the command to enable and disable the conda environment is slightly different, you don't need to add "source" to the command like on Mac and Linux OS).

(You must have Miniconda or Anaconda installed on your computer to use Conda. Give it a tryHereto install Miniconda on Mac, andHereto install Miniconda on CentOS7/RedHat7. Check overHereif you want to know the difference between Miniconda and Anaconda.)

The first part of this post introduces general conda commands and the second part of this post provides some package installations (e.g. OpenCV, scikit-image, jupyter notebook, etc.) using conda.

Index

general conda commands

Check if Conda is installed / check Conda version info

In your terminal, enter the following command

$ conda -V # If you see something like below, it means conda is installed and provides information about its version. conda 4.5.11

Make sure Conda is up to date

Enter in your terminal

$ conda update conda#Update any packages if necessary by typing the followingjkeep going.

Create a virtual environment for your project with Conda

With Conda we can create a virtual environment for different Python versions.

To see a list of Python versions available in the conda repository, enter the following command with a regular expression and presstap.

$ conda search "^python$"# You should see a list of Python versions including python2.X and python3.X

Now let's create a virtual environment using Conda

(Video) Master the basics of Conda environments in Python

Use the following command to create a virtual environment for a Python version you specify. Replace x.x with the Python version you want to use.

$ conda create -nseuenvname Python=x.x#For example, the following command creates a Python 3.6 Conda virtual environment named "conda-venv3_py36".$ conda create -nconda-venv3_py36python=3.6# The following command creates a Python 2.7 conda virtual environment named "conda-venv_py27".$ conda create -nconda-venv_py27python=2.7

Pressjkeep going. This will install the Python version (and any associated Anaconda package libraries if you installed Conda via Anaconda) to path_to_your_anaconda_location/anaconda/envs/yourenvname or to path_to_your_miniconda_location/miniconda/envs/yourenvname.

Activate your virtual environment

Once a virtual environment has been created with Conda, if we need to use the virtual environment, before we use it, we must first activate it.

To activate or switch to your virtual environment, simply type the following, where yourenvname is the name you gave your environment when you created it.

$ Activate fontseuenvname

Enabling a conda environment will change the PATH and shell variables to point to the specific isolated python you created. Note that thecommand promptchanges to indicate which conv environment you are currently connected to(YourVName).

If you don't remember your virtual environment or don't want to enter it, you can use the following command to display a list of all your environments.

$ conda info -e

Install (additional) Python packages in a virtual environment

To install packages only in your virtual environment (not system-wide), enter the following command

$ conda install -nseuenvname Paketnamen#seuenvnameis the name of your environment andPackage-name is the name of the package you want to install. #Note that if you type "-nseuenvname' installs the package in the root Python installation.

Or you can just activate first and in the virtual environment you want to install use the packages [see above command in (4)] and then use the following command

Install $condaPaketnamen

(To install some specific packages, see Section 2 at the bottom of this post.)

Deactivate your virtual environment.

Every time we finish working on your virtual environment created with Conda, we have to deactivate the virtual environment to finish it.

To end a session in the current virtual environment, enter the following command.

$Thosedeactivate#Note that we don't need to specify the environment name - whatever is currently active will be deactivated and the PATH and shell variables will go back to normal.

Delete a virtual environment

If we no longer need a virtual environment created by conda, we can easily remove it with the following command.

(Video) How to Create Virtual Environment in Anaconda | Python

remove conda -nseuenvname-at#seuenvnameis the name of the environment you want to delete. # (You can use $ conda env remove -nmyv.)

To verify that the environment has been removed, run the following in the terminal window or from a conda prompt

$cond Information-e

The displayed list of environments should not show the remote environment.

Cloning a Conda Virtual Environment

To create an exact copy of an environment by making a clone of it, use the following command:

$cond create --Name Microlon --Clone myv# NOTE: replaceMicrolonwith the name of the new environment. Substitutemyvwith the name of the existing environment you want to copy.# Below is an example of cloningpy35and naming the new copy aspy35-2$ conda create --clonepy35--Namepy35-2 

To verify that the copy was made:

In the mine Information -e

In the list of environments that is displayed, you can see the source environment and the new copy.

How to view a list of your environments

To see a list of all your virtual conda environments, run in your terminal or one of your virtual conda environmentsone of the followingCommands:

(Video) How to Create and Activate Virtual Environments in Anaconda (Conda Create)

$cond Information-eOR$ conda info--lista envs OR$ conda env

You will see a list of all your conda environments and the active environment will be shown with *.

Displaying a list of packages in a conda environment

To view a list of all packages installed in a specific environment,

  • If the environment is not enabled, run the following from the terminal window or a conda prompt:
$ Conda-List -nmyv
  • With the environment enabled, run the following from the terminal window or a conda prompt:
$ conda-lists

To see if a specific package is installed in a conda environment,

  • If the environment is not enabled, run the following from the terminal window or a conda prompt:
$ Conda-List -nmyv Paketnamen# For example, the following command lists the opencv versions installed in the conda environment you specified$ Conda-List -nmyenv opencv
  • With the environment enabled, run the following from the terminal window or a conda prompt:
$ conda-listsPaketnamen# For example, the following command lists the opencv versions installed in the current active conda environment you are in$ conda-listsopencv

Package installation with conda

Before installing packages with conda, make sure you first create a conda virtual environment [see the command in (3) in section 1 above in this post] and then activate and in the environment where you want to install the packages want to install [see the command for ( 4) in section 1 above in this post].

To installUntil now

$ conda install numpy

To installMatplotlibGenericName

Install $ conda matplotlib

To installStand

$ conda installs Keras

This should also install Tensorflow

To installh5py

$ conda install h5py

To installCaderno Jupyter

$ conda installs jupyter

To installIPythonName

$ conda installs ipython

Install OpenCV3 (https://opencv.org/)

$ conda install -c conda-forge opencv

The above command installs (by default) the latest version of opencv available in the conda repository. If you want to specify which version of openCV to install, you can first use the following command to check the available OpenCV versions.

$ conda search "^openCV$"  # You should see a list of openCV versions.

Then you can use the following command to install the OpenCV version you want to install,

$ conda install -c conda-forge opencv=x.x# For example, the following command will install openCV 3..4.1 instead of the current latest version 3.4.2. Note that when opencv=3.4 it installs openCV3.4.2 (the latest version of the 3.4 series), it's opencv==3.4.1, not opencv=3.4.$ conda install -c conda-forge opencv==3.4.1

To installScikit Image

$ conda install -c conda-forge scikit-image

To installDjango

Use the following command to see what version of Django is available in your conda environment.

$ conda search "^django$"

Use the following command to install the specific version of Django you want installed in your conda environment.

$ conda install -c conda-forge django==1.11.8

Use the following to test if Django was successfully installed in your conda environment.

(Video) Anaconda Tutorial 2019 - Python Virtual Environment Manager

$ Python
Python 3.6.7 |Anaconda, Inc.| (Standard, October 23, 2018, 19:16:44)
[GCC 7.3.0] no Linux
Type Help, Copyright, Credits, or License for more information.
>>>Import Django
>>>print(django.__version__)
1.11.8
>>>

Install non-conda packages

If a package is not available on conda or Anaconda.org, you can find and install the package using conda-forge or another package manager like pip.

Pip packages do not have all the features of conda packages and we recommend trying to install a package with conda first. If the package is not available via conda, try to find and install it withConda Forge.

If you still can't install the package, try pip. Differences between the pip and conda packages cause certain unavoidable compatibility issues, but conda works hard to be as compatible with pip as possible.

Remove packages with conda

  • To remove a package like SciPy in an environment like myenv:
    conda remove -n myenv scipy
  • To remove a package like SciPy in the current environment:
    conda removes scipy
  • To remove multiple packages at once, e.g. B. SciPy and cURL:
    conda entfernen scipy curl
  • To confirm a package has been removed:
    In the mine Listthe package name
    (Video) The Complete Guide to Python Virtual Environments!

references

FAQs

How to create virtual environment in Python using conda? ›

Let's go through the steps of creating a virtual environment using conda interface:
  1. Step 1: Check if conda is installed in your path. ...
  2. Step 2: Update the conda environment. ...
  3. Step 3: Set up the virtual environment. ...
  4. Step 4: Activating the virtual environment. ...
  5. Step 5: Installation of required packages to the virtual environment.
Apr 18, 2022

What is the command to create virtual environment in anaconda? ›

Use the terminal or an Anaconda Prompt for the following steps:
  1. Create the environment from the environment.yml file: conda env create -f environment. yml. ...
  2. Activate the new environment: conda activate myenv.
  3. Verify that the new environment was installed correctly: conda env list.

How do I create a virtual environment in Python? ›

Creation of virtual environments is done by executing the command venv :
  1. python3 -m venv /path/to/new/virtual/environment.
  2. c:\>c:\Python35\python -m venv c:\path\to\myenv.
  3. c:\>python -m venv c:\path\to\myenv.

How do you create a new conda environment with a specific version of Python? ›

Conda Usage
  1. Before working with Conda, it's always good practice to ensure that the latest versions of Conda and Anaconda are installed. ...
  2. To create a new Conda Python environment named <env_name> and install python 3.8, open an Anaconda Prompt or terminal and enter: $ conda create --name <env_name> python=3.8.
Jul 12, 2022

How do I create a 3.8 Virtualenv in Python? ›

If you are working with Python 3.8 or later, you can create a virtual environment shell by doing the following:
  1. cd into the directory where you would like to create your project.
  2. Enter python -m venv <project_name>
Aug 5, 2022

How do I start a conda environment? ›

To activate your Conda environment, type source activate <yourenvironmentname> . Note that conda activate will not work on Discovery with this version. To install a specific package, type conda install -n <yourenvironmentname> [package] . To deactivate the current, active Conda environment, type conda deactivate .

How do I run a Python command from the virtual environment? ›

To use the virtual environment you created to run Python scripts, simply invoke Python from the command line in the context where you activated it. For instance, to run a script, just run python myscript.py .

What is the use of creating virtual environment in Python? ›

Python virtual environments give you the ability to isolate your Python development projects from your system installed Python and other Python environments. This gives you full control of your project and makes it easily reproducible.

What is a conda virtual environment? ›

A conda environment is a directory that contains a specific collection of conda packages that you have installed. For example, you may have one environment with NumPy 1.7 and its dependencies, and another environment with NumPy 1.6 for legacy testing.

How do you create a virtual environment in Python VS code? ›

From within VS Code, you can create local environments, using virtual environments or Anaconda, by opening the Command Palette (Ctrl+Shift+P), start typing the Python: Create Environment command to search, and then select the command. The command presents a list of environment types: Venv or Conda.

How do I create a virtual environment in Python offline? ›

All the tasks are done in the command window in Windows 10, python 3.7.
  1. Install Python Packages (Offline)
  2. Create Virtual Environment.
  3. Edit Default Setting of Jupyter Notebook.
  4. Install Jupyter Notebook Extension (Offline)
  5. Set Up GPU for Tensorflow.

How do I start a virtual environment in Python 3? ›

  1. Install python3.x (if not present) for your platform.
  2. Install python3.x-devel for your platform.
  3. Create virtual environment in python 3.x (for example $ python3.6 -m venv virenv_test_p3/ )
  4. Activate the testenvironment for python 3.x (for example source virenv_test_p3/bin/activate)

What is conda command in Python? ›

The conda command is the primary interface for managing installations of various packages. It can: Query and search the Anaconda package index and current Anaconda installation. Create new conda environments. Install and update packages into existing conda environments.

How do you create a local environment in Python? ›

Open a terminal and run the following command:
  1. python --version. It should say something like the following:
  2. python --version Python 3.9.1. Find the instructions below to install virtualenv for the version of Python reported by your terminal. ...
  3. easy_install virtualenv. ...
  4. easy_install-2.7 virtualenv. ...
  5. pip install virtualenv.

How to install Python in conda environment? ›

The Earth Engine Python API can be installed to a local machine via conda, a Python package and environment manager. Conda is bundled with Anaconda and Miniconda Python distributions.
...
Install Miniconda
  1. Download the 64-bit Python 3 Miniconda installer to your Home directory.
  2. Run the installer.
  3. Delete the installer.
Dec 5, 2022

How do I create a virtual environment in python bash? ›

Comments
  1. Use Git-Bash as shell for terminal on windows.
  2. Create a python virtual environment ( python -m venv venv )
  3. Activate the virtual environment ( source venv/Scripts/activate )
  4. Try to perform any command (e.g. ls )
May 4, 2020

Does python 3.10 have venv? ›

Creating a Python Virtual Environment

virtualenv supports older Python versions and needs to be installed using the pip command. In contrast, venv is only used with Python 3.3 or higher and is included in the Python standard library, requiring no installation.

Should I use venv or virtualenv? ›

Traditionally virtualenv has been the library used to create virtual environments for python. However , starting python 3.3 , module venv has been added to python standard library and can be used as a drop-in replacement for virtualenv. If older version of python is being used, then virtualenv is the way to go.

How do you use conda environment command line? ›

Use the terminal or an Anaconda Prompt for the following steps:
  1. Create the environment from the environment.yml file: conda env create -f environment. yml. ...
  2. Activate the new environment: conda activate myenv.
  3. Verify that the new environment was installed correctly: conda env list.

How use conda command line? ›

Your answer
  1. Open Anaconda Prompt:
  2. Check Conda Installed Location. where conda.
  3. Open Advanced System Settings.
  4. Click on Environment Variables.
  5. Edit Path.
  6. Add New Path. ...
  7. Open Command Prompt and Check Versions.
  8. After 7th step type conda install anaconda-navigator in cmd then press y.
Feb 11, 2022

How do I get to conda in command prompt? ›

Go with the mouse to the Windows Icon (lower left) and start typing "Anaconda". There should show up some matching entries. Select "Anaconda Prompt". A new command window, named "Anaconda Prompt" will open.

How do I run a Python script from the command line with example? ›

To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World! If everything works okay, after you press Enter , you'll see the phrase Hello World!

How to activate virtual environment in cmd? ›

To activate virtualenv on Windows, first, install the pip. For this purpose, you can download and execute the latest Python installer. Next, install and create virtualenv on Windows using the pip package manager. Then, activate it using the “venvironment\Scripts\activate” command.

How do I know if Python virtual environment is activated? ›

Note: Before installing a package, look for the name of your virtual environment within parentheses just before your command prompt. In the example above, the name of the environment is venv . If the name shows up, then you know that your virtual environment is active, and you can install your external dependencies.

What is an example of a virtual environment? ›

Email, chat, and web-based document sharing applications are all examples of virtual environments. Simply put, it is a networked common operating space.

How do I list all virtual environments in conda? ›

List all existing environments. We can use conda env list to list all existing Python environments. The * will point to the current active environment. base is always the default active environment when you open your terminal.

Why use a conda environment? ›

1️⃣ It makes it easy to tell if a project utilizes an isolated environment by including the environment as a sub-directory. 2️⃣ It allows you to use the same name for all of your environments (I use “conda-env”), meaning you can activate each with the same command.

How do I create a virtual environment with Python requirements txt? ›

How to set up a virtual environment
  1. Install the virtualenv tool.
  2. Initialize your environment.
  3. Install dependencies.
  4. Create the requirements. txt to remember the dependencies.
  5. Check code and the requirements. txt file into code repository.
Jun 21, 2017

Which Python module helps you create virtual environments? ›

The module used to create and manage virtual environments is called venv . venv will usually install the most recent version of Python that you have available. If you have multiple versions of Python on your system, you can select a specific Python version by running python3 or whichever version you want.

How do I create a virtual environment and activate it? ›

Create a Virtual Environment using “virtualenv”
  1. Install the virtualenv. ...
  2. Create a virtual environment. ...
  3. Create an environment with a specific version of Python. ...
  4. Activate the virtual environment. ...
  5. Deactivate the virtual environment. ...
  6. Check which Environment you are in. ...
  7. Remove an environment.
Mar 22, 2020

How to install venv using pip? ›

How to install virtualenv:
  1. Install pip first. sudo apt-get install python3-pip.
  2. Then install virtualenv using pip3. ...
  3. Now create a virtual environment. ...
  4. You can also use a Python interpreter of your choice. ...
  5. Active your virtual environment: ...
  6. Using fish shell: ...
  7. To deactivate: ...
  8. Create virtualenv using Python3.

What is the best Python virtual environment? ›

Two most popular virtual environment libraries for Python are venv and virtualenv. The difference between these two are negligible. However, there is one big difference and that is venv is a standard library that does not need to be installed while virtualenv needs to be installed with pip.

Where are conda commands used? ›

Conda is a powerful package manager and environment manager that you use with command line commands at the Anaconda Prompt for Windows, or in a terminal window for macOS or Linux.

What is Anaconda command prompt? ›

Anaconda Prompt or Terminal

Anaconda Prompt is a command line interface with Anaconda Distribution. Terminal is a command line interface that comes with macOS and Linux.

How do I set an environment variable in Python? ›

To permanently modify the default environment variables, click Start and search for 'edit environment variables', or open System properties, Advanced system settings and click the Environment Variables button. In this dialog, you can add or modify User and System variables.

How do I create a virtual environment in Anaconda Windows? ›

Create a Virtual Environment using Conda
  1. Step 1: Open Anaconda prompt. ...
  2. Step 2: Check Conda is installed in your path. ...
  3. Step 3: Check if Conda is up to date. ...
  4. Step 4: Create a Virtual Environment. ...
  5. Step 5: Activate the environment. ...
  6. Step 6: Deactivate the above environment once your work is done.

How do you create a virtual environment in Anaconda Jupyter notebook? ›

Let's see how to do that:
  1. Step 1: Create a virtual environment. ...
  2. Step 2: Activate the virtual environment. ...
  3. Step 3: Install jupyter kernel for the virtual environment using the following command: ...
  4. Step 4: Select the installed kernel when you want to use jupyter notebook in this virtual environment.
Sep 14, 2021

How do I get to the virtual environment in CMD? ›

How to Activate virtualenv on Windows?
  1. Step 1: Open Command Prompt. Firstly, open the Command Prompt with the help of the Run box. ...
  2. Step 2: Install virtualenv. ...
  3. Step 3: Make and Open Project Directory. ...
  4. Step 4: Create virtualenv. ...
  5. Step 5: Activate virtualenv.

How to create environment in cmd? ›

Open the Windows Command Prompt enter into your Desktop folder with the command cd desktop . You can find your device's command line interface (CLI) by searching in your applications.> Type py -m venv env to create a virtual environment named env . When the environment is created, the prompt will reappear.

Videos

1. How to create a Python virtual environment for a new project
(IDG TECHtalk)
2. Create Your Own Conda Environment
(Oracle Learning)
3. Anaconda Beginners Guide for Linux and Windows - Python Working Environments Tutorial
(Python Simplified)
4. Python Tutorial: VENV (Mac & Linux) - How to Use Virtual Environments with the Built-In venv Module
(Corey Schafer)
5. Python Tutorial: VENV (Windows) - How to Use Virtual Environments with the Built-In venv Module
(Corey Schafer)
6. Set Up Python Virtual Environment In Visual Studio Code (VS Code)
(Jie Jenn)
Top Articles
Latest Posts
Article information

Author: Rev. Leonie Wyman

Last Updated: 14/06/2023

Views: 5833

Rating: 4.9 / 5 (79 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Rev. Leonie Wyman

Birthday: 1993-07-01

Address: Suite 763 6272 Lang Bypass, New Xochitlport, VT 72704-3308

Phone: +22014484519944

Job: Banking Officer

Hobby: Sailing, Gaming, Basketball, Calligraphy, Mycology, Astronomy, Juggling

Introduction: My name is Rev. Leonie Wyman, I am a colorful, tasty, splendid, fair, witty, gorgeous, splendid person who loves writing and wants to share my knowledge and understanding with you.