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:
- Mac: https://github.com/pyenv/pyenv#installation
- Linux: https://github.com/pyenv/pyenv-installer
- Windwos: https://github.com/pyenv-win/pyenv-win
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
virtualenvwrapper | conda | |
---|---|---|
Installation | pip install virtualenv virtualenvwrapper | 1 |
Env List | lsvirtualenv | conda env list |
Env Create | mkvirtualenv env | conda create -n env |
Env Activate | workon env | conda activate env |
Env Deactivate | deactivate | conda deactivate |
Env Remove | rmvirtualenv env | conda env remove -n env |
Env Duplicate | cpvirtualenv orig new | conda 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
venv | virtualenv | conda | pipenv | poetry | |
---|---|---|---|---|---|
Installation | - | pip install virtualenv | 1 | 2 | 3 |
Env Create | python -m venv env | virtualenv env | conda create -p env | pipenv --python python-version | poetry init |
Env Activate | source env /bin/activate | source env /bin/activate | conda activate ./env | pipenv shell | poerty shell |
Env Deactivate | deactivate | deactivate | conda deactivate | ++ctrl+d++ | ++ctrl+d++ |
Env Remove | Delete env folder | Delete env folder | conda env remove -p env | pipenv --rm | 4 |
Env Run | - | - | - | pipenv run python main.py | poetry run python main.py |
Manage python packages Link to heading
pip | conda | pipenv | poetry | |
---|---|---|---|---|
Installation | 5 | 1 | 2 | 3 |
Package Install | pip install pkg | conda install pkg | pipenv install pkg | poetry add pkg |
Package Update | pip install --upgrade pkg | conda update pkg | pipenv update pkg | poetry update pkg |
Package Remove | pip uninstall pkg | conda remove pkg | pipenv uninstall pkg | poetry remove pkg |
Env Export | pip freeze > requirements.txt | conda env export > environment.yml | Pipfile | pyproject.toml |
Env Deploy | pip install -r requirements.txt | conda env create -f environment.yml | pipenv install | poetry install |
Import from pip | - | 6 | pipenv install -r requirements.txt | 7 |
Export to pip | - | - | pipenv lock -r > requirements.txt | poetry export -f requirements.txt -o requirements.txt |
pip Link to heading
- Install packages from PyPI.
- Won't remove dependencies when removing explicitly installed packages.
(Solution:
pip install python3-pip-autoremove
,pip3-autoremove <pkg>
) - Visualize dependencies in
requirements.txt
:pip install pipdeptree
pipdeptree -f > requirements.txt
- Mimic separating development env and production env.
conda Link to heading
- Install packages from Anaconda, which has fewer packages than PyPI.
- Can use pip/poetry inside conda env.
(Set
poetry config settings.virtualenvs.create false
when using poetry inside conda env) - Support nested env activation:
conda activate --stack <env>
- For exporting packages which is explicitly installed with conda:
conda env export --from-history
pipenv Link to heading
- Install packages from PyPI.
- Support separating development env and production env.
- Package Install:
pipenv install <pkg> --dev
- Env Deploy:
pipenv install --dev
- Package Install:
- Support
.env
file. - Generating
Pipfile.lock
is time-consuming.
poetry Link to heading
- Install packages from PyPI.
- Support separating development env and production env.
- Package Install:
poetry add <pkg> --dev
- Package Remove:
poetry remove <pkg> --dev
- Env Deploy:
poetry install --no-dev
- Package Install:
- It is hard to type poetry fast. 😄
Reference Link to heading
- https://docs.conda.io/projects/conda/en/latest/index.html
- https://github.com/pyenv/pyenv
- https://virtualenvwrapper.readthedocs.io/en/latest/
- https://virtualenv.pypa.io/en/latest/
- https://pip.pypa.io/en/stable/getting-started/
- https://pipenv.pypa.io/en/latest/
- https://python-poetry.org/
- https://stackoverflow.com/questions/17803829/how-to-customize-a-requirements-txt-for-multiple-environments
- https://stackoverflow.com/questions/47311847/separating-development-and-production-dependencies-with-virtualenv
- https://stackoverflow.com/questions/35802939/install-only-available-packages-using-conda-install-yes-file-requirements-t
- https://stackoverflow.com/questions/62764148/how-to-import-requirements-txt-from-an-existing-project-using-poetry
pip install pipenv
orcurl https://raw.githubusercontent.com/pypa/pipenv/master/get-pipenv.py | python
↩︎ ↩︎curl -sSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | python
↩︎ ↩︎poetry env list --full-path
,poetry env remove <env>
↩︎Use system package manager (e.g.
apt
,yum
) orcurl -sSL https://bootstrap.pypa.io/get-pip.py | python
↩︎while read requirement; do conda install --yes $requirement || pip install $requirement; done < requirements.txt
↩︎cat requirements.txt | xargs poetry add
↩︎