Skip to content

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

Conda

Install Conda

Check out conda-forge 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-64

Install Packages via conda-forge

c stands for channel.

conda install <package> -c conda-forge
conda config --show [channels]
conda config --remove channels <channel>
conda config --add channels <channel>
conda config --set show_channel_urls yes

Pyenv

Install Pyenv

  1. Update System Packages

    sudo apt update
  2. (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-dev

    or

    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

    according to OpenAI’s gpt-4o-2024-05-13.

  3. Install pyenv

    curl https://pyenv.run | bash
  4. 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
  5. 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 --list

Install specific version with

pyenv install 3.11.10

Install 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.10

or just assign to current shell with

pyenv shell 3.11.10

Uninstall Python version with

pyenv uninstall 3.11.10

Check all the Python versions installed by PyEnv, including virtual environments

pyenv versions

Set Up Virtual Environment

Install pyenv-virtualenv:

  1. Install pyenv-virtualenv

    git clone https://github.com/pyenv/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
  2. Update shell configuration:

    eval "$(pyenv virtualenv-init -)"
  3. Restart your shell:

    exec $SHELL

Create a new virtual environment:

# pyenv virtualenv <python_version> <environment_name>
pyenv virtualenv 3.11.10 myenv

Activate the virtual environment:

# pyenv activate <environment_name>
pyenv activate myenv

Deactivate the virtual environment:

pyenv deactivate

Check available virtual environments:

pyenv virtualenvs

Remove 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 ~/.bashrc

Additional Setup

I prefer to use virtual environment under the current project directory.

poetry config virtualenvs.in-project true

use poetry config --list to check current settings.

Create New Project

cd my-project
poetry init
poetry env use python
# or
# poetry env use /Users/caijiaqi/.pyenv/versions/3.10.14/bin/python

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:

  1. update pyproject.toml
  2. update poetry.lock according to pyproject.toml
  3. update virtual environment according to poetry.lock

read more