在MacOS使用轻量级工具Pyenv和Pipenv切换多版本Python
Setup Pyenv
# Install Pyenv
brew update && brew install pyenv
# Install Python Interpreter with specific version e.g. 3.9.15
pyenv install 3.9.15
# Navigate to project directory and choose Python Interpreter
pyenv local 3.9.15
# Setup shell environment (For zsh)
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
Setup Pipenv
# Install pipenv
pip install pipenv
# Initialize pipenv
pipenv install
pipenv
will create a virtual environment if no existed one found.
By default, the Pipenv create the virtual environment in a centralized location like:
~/.local/share/virtualenvs/${PROJ_NAME}-{HASHED_PROJ_FULL_PATH}
unless environment variable PIPENV_VENV_IN_PROJECT
is set to 1
:
export PIPENV_VENV_IN_PROJECT=1
This command will cause Pipenv to create the virtual environment inside your project directory, typically as a .venv
folder.
There are some useful command:
# show venvs for current project:
pipenv --venv
# activate venv:
pipenv shell
# deactivate venv:
exit
# show dependencies:
pipenv graph
# remove venv:
pipenv --rm