Note
For Windows user / machine learning / newbie: Recommend to use conda + pip

Manage Python interpreter version Link to heading

conda Link to heading

Installation: https://docs.conda.io/en/latest/miniconda.html Specify the Python version when creating the environment: conda create -n env python=3.8

pyenv Link to heading

Installation:

Install a new version: pyenv install -v 3.8.0 Set the global version: pyenv global 3.8.0 Set the local version: pyenv local 3.8.0 List versions: pyenv versions

Manage Python virtual environment Link to heading

Global Link to heading

virtualenvwrapperconda
Installationpip install virtualenv virtualenvwrapper1
Env Listlsvirtualenvconda env list
Env Createmkvirtualenv envconda create -n env
Env Activateworkon envconda activate env
Env Deactivatedeactivateconda deactivate
Env Removermvirtualenv envconda env remove -n env
Env Duplicatecpvirtualenv orig newconda create -n new --clone orig

Virtualenvwrapper setup Link to heading

# In .bashrc/.profile/.zshrc etc.
export WORKON_HOME=<path-to-env-stored>
export VIRTUALENVWRAPPER_PYTHON= $(which python3)
source /usr/local/bin/virtualenvwrapper.sh

Local Link to heading

venvvirtualenvcondapipenvpoetry
Installation-pip install virtualenv123
Env Createpython -m venv envvirtualenv envconda create -p envpipenv --python python-versionpoetry init
Env Activatesource env/bin/activatesource env/bin/activateconda activate ./envpipenv shellpoerty shell
Env Deactivatedeactivatedeactivateconda deactivate++ctrl+d++++ctrl+d++
Env RemoveDelete env folderDelete env folderconda env remove -p envpipenv --rm4
Env Run---pipenv run python main.pypoetry run python main.py

Manage python packages Link to heading

pipcondapipenvpoetry
Installation5123
Package Installpip install pkgconda install pkgpipenv install pkgpoetry add pkg
Package Updatepip install --upgrade pkgconda update pkgpipenv update pkgpoetry update pkg
Package Removepip uninstall pkgconda remove pkgpipenv uninstall pkgpoetry remove pkg
Env Exportpip freeze > requirements.txtconda env export > environment.ymlPipfilepyproject.toml
Env Deploypip install -r requirements.txtconda env create -f environment.ymlpipenv installpoetry install
Import from pip-6pipenv install -r requirements.txt7
Export to pip--pipenv lock -r > requirements.txtpoetry export -f requirements.txt -o requirements.txt

pip Link to heading

  1. Install packages from PyPI.
  2. Won't remove dependencies when removing explicitly installed packages. (Solution: pip install python3-pip-autoremove, pip3-autoremove <pkg>)
  3. Visualize dependencies in requirements.txt:
    1. pip install pipdeptree
    2. pipdeptree -f > requirements.txt
  4. Mimic separating development env and production env.
    1. Without any tools
    2. Using pip-tools

conda Link to heading

  1. Install packages from Anaconda, which has fewer packages than PyPI.
  2. Can use pip/poetry inside conda env. (Set poetry config settings.virtualenvs.create false when using poetry inside conda env)
  3. Support nested env activation: conda activate --stack <env>
  4. For exporting packages which is explicitly installed with conda: conda env export --from-history

pipenv Link to heading

  1. Install packages from PyPI.
  2. Support separating development env and production env.
    1. Package Install: pipenv install <pkg> --dev
    2. Env Deploy: pipenv install --dev
  3. Support .env file.
  4. Generating Pipfile.lock is time-consuming.

poetry Link to heading

  1. Install packages from PyPI.
  2. Support separating development env and production env.
    1. Package Install: poetry add <pkg> --dev
    2. Package Remove: poetry remove <pkg> --dev
    3. Env Deploy: poetry install --no-dev
  3. It is hard to type poetry fast. 😄

Reference Link to heading


  1. https://docs.conda.io/en/latest/miniconda.html ↩︎ ↩︎ ↩︎

  2. pip install pipenv or curl https://raw.githubusercontent.com/pypa/pipenv/master/get-pipenv.py | python ↩︎ ↩︎

  3. curl -sSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | python ↩︎ ↩︎

  4. poetry env list --full-path, poetry env remove <env> ↩︎

  5. Use system package manager (e.g. apt, yum) or curl -sSL https://bootstrap.pypa.io/get-pip.py | python ↩︎

  6. while read requirement; do conda install --yes $requirement || pip install $requirement; done < requirements.txt ↩︎

  7. cat requirements.txt | xargs poetry add ↩︎