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
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
Pressj
keep 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.
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: replaceMicrolon
with the name of the new environment. Substitutemyv
with 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:
$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.
$ 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? ›
- Step 1: Check if conda is installed in your path. ...
- Step 2: Update the conda environment. ...
- Step 3: Set up the virtual environment. ...
- Step 4: Activating the virtual environment. ...
- Step 5: Installation of required packages to the virtual environment.
- Create the environment from the environment.yml file: conda env create -f environment. yml. ...
- Activate the new environment: conda activate myenv.
- Verify that the new environment was installed correctly: conda env list.
- python3 -m venv /path/to/new/virtual/environment.
- c:\>c:\Python35\python -m venv c:\path\to\myenv.
- c:\>python -m venv c:\path\to\myenv.
- Before working with Conda, it's always good practice to ensure that the latest versions of Conda and Anaconda are installed. ...
- 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.
- cd into the directory where you would like to create your project.
- Enter python -m venv <project_name>
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? ›
- Install Python Packages (Offline)
- Create Virtual Environment.
- Edit Default Setting of Jupyter Notebook.
- Install Jupyter Notebook Extension (Offline)
- Set Up GPU for Tensorflow.
- Install python3.x (if not present) for your platform.
- Install python3.x-devel for your platform.
- Create virtual environment in python 3.x (for example $ python3.6 -m venv virenv_test_p3/ )
- Activate the testenvironment for python 3.x (for example source virenv_test_p3/bin/activate)
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? ›- python --version. It should say something like the following:
- python --version Python 3.9.1. Find the instructions below to install virtualenv for the version of Python reported by your terminal. ...
- easy_install virtualenv. ...
- easy_install-2.7 virtualenv. ...
- pip install virtualenv.
...
Install Miniconda
- Download the 64-bit Python 3 Miniconda installer to your Home directory.
- Run the installer.
- Delete the installer.
- Use Git-Bash as shell for terminal on windows.
- Create a python virtual environment ( python -m venv venv )
- Activate the virtual environment ( source venv/Scripts/activate )
- Try to perform any command (e.g. ls )
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.
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? ›- Create the environment from the environment.yml file: conda env create -f environment. yml. ...
- Activate the new environment: conda activate myenv.
- Verify that the new environment was installed correctly: conda env list.
- Open Anaconda Prompt:
- Check Conda Installed Location. where conda.
- Open Advanced System Settings.
- Click on Environment Variables.
- Edit Path.
- Add New Path. ...
- Open Command Prompt and Check Versions.
- After 7th step type conda install anaconda-navigator in cmd then press y.
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? ›- Install the virtualenv tool.
- Initialize your environment.
- Install dependencies.
- Create the requirements. txt to remember the dependencies.
- Check code and the requirements. txt file into code repository.
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? ›- Install the virtualenv. ...
- Create a virtual environment. ...
- Create an environment with a specific version of Python. ...
- Activate the virtual environment. ...
- Deactivate the virtual environment. ...
- Check which Environment you are in. ...
- Remove an environment.
How to install venv using pip? ›
- Install pip first. sudo apt-get install python3-pip.
- Then install virtualenv using pip3. ...
- Now create a virtual environment. ...
- You can also use a Python interpreter of your choice. ...
- Active your virtual environment: ...
- Using fish shell: ...
- To deactivate: ...
- Create virtualenv using Python3.
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.
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? ›- Step 1: Open Anaconda prompt. ...
- Step 2: Check Conda is installed in your path. ...
- Step 3: Check if Conda is up to date. ...
- Step 4: Create a Virtual Environment. ...
- Step 5: Activate the environment. ...
- Step 6: Deactivate the above environment once your work is done.
- Step 1: Create a virtual environment. ...
- Step 2: Activate the virtual environment. ...
- Step 3: Install jupyter kernel for the virtual environment using the following command: ...
- Step 4: Select the installed kernel when you want to use jupyter notebook in this virtual environment.
- Step 1: Open Command Prompt. Firstly, open the Command Prompt with the help of the Run box. ...
- Step 2: Install virtualenv. ...
- Step 3: Make and Open Project Directory. ...
- Step 4: Create virtualenv. ...
- Step 5: Activate virtualenv.
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.