Manage Python Environments
Pip
Install Packages With pip Using Mirrors
For global set up, create file ~/.pip/pip.conf
with the following content.
Create folder ~/.pip/
if it does not exist.
Conda
Install Conda
Check out conda-forge installation instructions.
Check out Miniconda installation instructions.
Check out Mamba installation instructions.
Create a Conda Environment
The follow commands create an conda environment with name <env-name>
and python of version 3.9
.
Install Packages via conda-forge
c
stands for channel.
Pyenv
Install Pyenv
-
Update System Packages
-
(Optional): Install Dependencies
Install optional dependencies with
or
-
Install pyenv
-
Set Up Environment Variables
-
Don’t forget to restart your shell for the changes to take effect.
Install Python With Pyenv
Check available versions with
Install specific version with
Install the latest version of 3.11 with
Set global version with
or just assign to current shell with
Uninstall Python version with
Check all the Python versions installed by PyEnv, including virtual environments
Set Up Virtual Environment
Install pyenv-virtualenv:
-
Install
pyenv-virtualenv
-
Update shell configuration:
-
Restart your shell:
Create a new virtual environment:
Activate the virtual environment:
Deactivate the virtual environment:
Check available virtual environments:
Remove a virtual environment:
Python Package Management with Poetry
Install Poetry and Add to PATH
Additional Setup
I prefer to use virtual environment under the current project directory.
use poetry config --list
to check current settings.
Create New Project
Install Dependencies
Two files, pyproject.toml
and poetry.lock
, are created and updated accordingly by now. When you use poetry add
command, Poetry will automatically do the following three things in this order:
- update
pyproject.toml
- update
poetry.lock
according topyproject.toml
- update virtual environment according to
poetry.lock
read more