Skip to content

Instantly share code, notes, and snippets.

@discdiver
Last active December 8, 2023 04:12
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 14 You must be signed in to fork a gist
  • Save discdiver/0bb3bf96f02c182f96d45278f9564551 to your computer and use it in GitHub Desktop.
Save discdiver/0bb3bf96f02c182f96d45278f9564551 to your computer and use it in GitHub Desktop.
Common conda and venv commands

Common conda commands

Use conda commands to create and manage a virtual environment and the Python packages inside it.

Download the Anaconda or Miniconda. Anaconda and miniconda come with Python and pip installed. Anaconda installs many packages and tools that are common for data science. Miniconda is far smaller.

Create environment

conda create -n myenv pandas jupyterlab Create a new conda environment named myenv with the latest version of Python on the main conda channel and have pandas and jupyterlab installed into it.

conda create -n myenv python=3.9 Create a new conda environment named myenv with the Python version specified.

Activate and deactivate environment

conda activate myenv Activate the myenv virtual environment. You can tell you are in a virtual environment because your command line prompt will start with the name of your virtual environment in parentheses. Conda can be setup to activate an environment whenever you open your terminal. Wherever you go on your machine you will stay in your activated virtual environment.

conda deactivate Deactivate the current environment.

Manage packages in active environment

conda list List installed packages and versions in the active environment.

conda install pandas Download and install the pandas package from the main conda channel.

conda install -c conda-forge pandas Download and install the pandas package from the main conda-forge channel. This is useful when a package doesn't exist on the main channel or the version on the main channel isn't as new as you would like.

conda update pandas Download and install the latest pandas package from the main conda channel.

pip install -U pandas Install or update the pandas package from PyPI - the Python package manager. -U specifies to update all dependent packages. PyPI often has packages that are not on conda or conda-forge. Conda and PyPI packages generally play nicely. Make sure that if you are installing a version of a package from PyPI you don't already have the same package installed from conda. If you do have it installed, then uninstall the conda version first.

conda uninstall pandas Uninstall pandas.

Manage environments

conda env list List conda environments.

conda env remove --name myenv Remove the myenv environment.

@discdiver
Copy link
Author

discdiver commented Jan 11, 2020

Alternative virtual environment: use venv and pip

Alternatively, you can create virtual environments and install packages with venv and pip.

You have venv automatically if you have a recent version of Python installed.

Use the following commands (assuming you have python3.8 installed):

python3.8 -m venv my_env Create my_env with Python 3.8 installed.

source my_env/bin/activate Activate my_env

pip install jupyterlab Install jupyterlab into active environment

deactivate Deactivate current environment

If you want to install a bunch of packages, you can list them in a file. requirements.txt is the common name. You can specify the versions if you want.

pip install -r requirements.txt Install the list of packages in requirements.txt.

To avoid confusion, don't use venv inside an active conda environment. Deactivate the conda environment first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment