Skip to content

Instantly share code, notes, and snippets.

@discdiver
Last active May 1, 2024 14:25
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

Set up and use a Python virtual environment

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

Download and install Miniconda

Follow the instructions here for your operating system: https://docs.conda.io/en/latest/miniconda.html.

You may be prompted with “Do you wish the installer to initialize Anaconda3 by running conda init?” We recommend “yes”. The default conda virtual environment is named base.

You will know you are in a virtual environment because your terminal prompt will have the name of the environment, for example, (base), in parentheses.

The default environment is the base environment. If you are starting out, you can just use that environment. To isolate dependencies for different projects or to use different software versions, you will want to create and use multiple virtual environments.

NOTE: You do not want to have more than a single virtual environment active at the same time - otherwise it can be tricky to know which versions of packages are being installed and used at runtime.

Deactivate current active environment

conda deactivate Deactivate the current environment.

Create a new virtual environment

conda create -n myenv python=3.12 Create a new conda environment named myenv with the latest version of Python from the main conda package channel with Python 3.12 installed. The environment will have very few packages installed.

Activate environment

conda activate myenv Activate the myenv virtual environment. Unlike the venv/virtualenv virtual environment managers, wherever you navigate in your terminal you will stay in your activated virtual environment.

Manage packages in active environment

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

pip install -U pandas Install or update the pandas package from PyPI - the Python package manager. -U specifies to update the specified package and all dependent packages. PyPI often has packages that are not on conda or conda-forge. Packages from conda and PyPI packages generally play together nicely.

pip 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 creation and management with venv

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

venv comes installed with Python.

If you need to install Python 3.12, download it from the python.org

Commands (assuming you have Python 3.12 installed):

python3.12 -m venv my_env Create my_env with Python 3.12 installed.

source my_env/bin/activate Activate my_env

pip install pandas Install pandas 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