Manage Python Environments
Pip
Install Packages With pip Using Mirrors
pip install <package> -i https://pypi.tuna.tsinghua.edu.cn/simple
# pip install <package> -i http://mirrors.aliyun.com/pypi/simple --truste-host mirrors.aliyun.com[global]
index-url=https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host=https://pypi.tuna.tsinghua.edu.cnCreate folder ~/.pip/ if it does not exist.
$ pip config list
global.index-url='https://pypi.tuna.tsinghua.edu.cn/simple'
install.trusted-host='https://pypi.tuna.tsinghua.edu.cn'[!tip] Choose from these mirrors
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.
CONDA_SUBDIR=osx-64 conda create -n <env-name> python=3.9
conda activate myenv_x86
conda config --env --set subdir osx-64Install Packages via conda-forge
c stands for channel.
conda install <package> -c conda-forgeconda config --show [channels]
conda config --remove channels <channel>
conda config --add channels <channel>
conda config --set show_channel_urls yesPyenv
Install Pyenv
[!tip] Manage multiple versions of Python with pyenv.
-
Update System Packages
sudo apt update -
(Optional): Install Dependencies
Install optional dependencies with
sudo apt install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-devor
sudo apt install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl git -
Install pyenv
curl https://pyenv.run | bash -
Set Up Environment Variables
# Load pyenv automatically by appending # the following to # ~/.bash_profile if it exists, otherwise ~/.profile (for login shells) # and ~/.bashrc (for interactive shells) : echo -e 'export PYENV_ROOT="$HOME/.pyenv"\n[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc echo -e 'eval "$(pyenv init -)"' >> ~/.bashrc # Restart your shell for the changes to take effect. # Load pyenv-virtualenv automatically by adding # the following to ~/.bashrc: echo -e 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc -
Don't forget to restart your shell for the changes to take effect.
source ~/.bashrc # or source ~/.zshrc
Install Python With Pyenv
Check available versions with
pyenv install --listInstall specific version with
pyenv install 3.11.10Install the latest version of 3.11 with
pyenv install "$(pyenv install --list | tr -d ' ' | grep --extended-regexp '^3.11.[0-9]+' | tail -1)"Set global version with
pyenv global 3.11.10or just assign to current shell with
pyenv shell 3.11.10Uninstall Python version with
pyenv uninstall 3.11.10Check all the Python versions installed by PyEnv, including virtual environments
pyenv versionsSet Up Virtual Environment
Install pyenv-virtualenv:
```bash
git clone https://github.com/pyenv/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
```2. Update shell configuration:
```bash
eval "$(pyenv virtualenv-init -)"
```3. Restart your shell:
```bash
exec $SHELL
```Create a new virtual environment:
# pyenv virtualenv <python_version> <environment_name>
pyenv virtualenv 3.11.10 myenvActivate the virtual environment:
# pyenv activate <environment_name>
pyenv activate myenvDeactivate the virtual environment:
pyenv deactivateCheck available virtual environments:
pyenv virtualenvsRemove a virtual environment:
pyenv virtualenv-delete <environment_name>Python Package Management with Poetry
Install Poetry and Add to PATH
curl -sSL https://install.python-poetry.org | python3 -
echo 'export PATH=$PATH:$HOME/.local/bin' >> ~/.bashrc
source ~/.bashrcAdditional Setup
I prefer to use virtual environment under the current project directory.
poetry config virtualenvs.in-project trueuse poetry config --list to check current settings.
Create New Project
cd my-project
poetry initpoetry env use python
# or
# poetry env use /Users/caijiaqi/.pyenv/versions/3.10.14/bin/python[!tip] Note that the python version is determined by
pythonso choose the desired one.
Install Dependencies
poetry add <package>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.lockaccording topyproject.toml - update virtual environment according to
poetry.lock
[!caution] Do not manually edit poetry.lock and pyproject.toml. If you manually edited pyproject.toml, run
poetry lockto update poetry.lock file.
[!readme] Readme