Skip to content

Instantly share code, notes, and snippets.

@ziritrion
Last active April 21, 2024 19:16
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save ziritrion/8024025672ea92b8bdeb320d6015aa0d to your computer and use it in GitHub Desktop.
Save ziritrion/8024025672ea92b8bdeb320d6015aa0d to your computer and use it in GitHub Desktop.
Cheatsheet for Conda, Pipenv

Conda

  1. Create a virtual environment
    • conda create --name my_env_name python=3.8 or whatever Python version you may need.
  2. List available envs (2 different ways
    • conda env list
    • conda info --envs
  3. Activate virtual env
    • conda activate my_env_name
  4. Deactivate current environment
    • conda deactivate
  5. If pip doesn't work with a fresh conda install:
    • conda install pip
  6. Install project dependencies (listed in requirements.txt file)
    • conda install --file requirements.txt
    • pip install -r requirements.txt
  7. Delete an old environment
    • conda remove --name my_env_name --all
    • conda env remove -n my_env_name
  8. Update conda
    • conda update conda
  9. Update all packages in the current environment
    • conda update --all
  10. Update all packages in another env
    • conda update -n my_env_name --all
  11. List installed packages in current environment
    • conda list
  12. Add conda-forge channel
    • conda config --add channels conda-forge
  13. Check conda channels
    • conda config --show channels
  14. Remove conda-forge channel
    • conda config --remove channels conda-forge
  15. Create an environment file from your current environment.
    • conda env export --from-history > environment.yml
  16. Create a new environment and install dependencies listed in YML file.
    • conda env create -f environment.yml

Pipenv + Pyenv

pyenv is a Python version manager. It allows you to install and manage multiple Python versions.

pipenv is a Python virtualenv management tools. pipenv does not have built-in package search; make sure you search for the packages at PyPI.

Installation

  1. pyenv
    • https://github.com/pyenv/pyenv
    • pyenv can be installed with either Brew or with the automatic installer script.
    • For Windows, there is pyenv-win but I have not tested it.
  2. pipenv

Usage

The environments are based on the folder you're on. There is no need to manually name them, and there is no environment activation to take care of per se.

  1. Install the Python version you need
    • pyenv install 3.11
  2. Create a new virtual environment with pipenv and choose the Python vrsion you want.
    • pipenv install --python 3.11
  3. Install a package (this will modify Pipfile and Pipfile.lock)
    • pipenv install some_package
    • pipenv install some_package=1.0
  4. If a Pipfile.lock file already exists, you can install the packages from it.
    • pipenv sync
  5. Update packages
    • pipenv update > updates all packages
    • pipenv update <package> updates a single package and its sub-dependencies
  6. Access the pipenv shell (necessary for enabling the virtualenv and for your script to find the installed packages).
    • pipenv shell
  7. Exit a pipenv shell.
    • exit
  8. Install all dependencies to the system, avoiding virtualenvs entirely (useful for deployment in containers)
    • pipenv install --system --deploy > Use this in your Dockerfile.

Installing pipenv on Docker Dev Environment

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