This post is aimed at Python users who have encountered situations where their Python environment is not suitable for running projects sourced from others on GitHub or elsewhere. However, this issue may not only be caused by package dependencies but also by the version of Python installed on their machines. One handy solution is to install multi-Python.
Additionally, consider a scenario where you are installing a new library to your virtual environment. The library may have additional dependencies, and if you later decide to remove the library using pip, its dependencies may not be removed, which can cause problems in your environment.
“To start resolving this issue, let’s begin with an introduction to Pyenv.
Pyenv is a Python version management tool that allows you to install and manage multiple Python versions, as well as set a default Python version or use a specific version for a particular project. The installation process for pyenv can vary depending on your operating system.
MacOS can install via homebrew (details)
brew update
brew install pyenv
// This lib is required while installing python via pyenv
brew install xz
Windows can install via PowerShell, run as admin if it needs (details)
Invoke-WebRequest -UseBasicParsing -Uri "https://raw.githubusercontent.com/pyenv-win/pyenv-win/master/pyenv-win/install-pyenv-win.ps1" -OutFile "./install-pyenv-win.ps1"; &"./install-pyenv-win.ps1"
Ubuntu can install via apt (reference)
# Update your apt first
apt-get update
# Install dependencies
apt-get install 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 python3-openssl git
# If you encounter some errors with python-openssl like "Unable to locate package python3-openssl", please install python-openssl via apt install python3-openssl
# Clone pyenv from github
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
# Export path
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bashrc
# Reload ~/.bashrc
source ~/.bashrc
After the installation, you can just type pyenv to see if it works…
Since the helper already listed out all command you can use, here I would just mention some commands that I usually need.
Check available Python versions
# List out all available Python versions
pyenv install -l
Install Python with a specific version
# Indicates what Python version you're going to install
# Depends on your OS, -v may need or may not need
pyenv install -v 3.9.0
pyenv install 3.9.0
# Once the installation is done, you can see what version has been installed on your machine
pyenv versions
Set Python version for a specific scope
# You can set Python version to global, local, or shell
# The priority is shell > local > global which means, if you have set Python 3.9.15 at your current path (ex: ~/Desktop) via pyenv local 3.9.15 and set Python 3.8.15 via pyenv global 3.8.15. Then if you execute Python command at ~/Desktop, it should use 3.9.15; otherwise, it should use 3.8.15
# You can check what Python version you're using via pyenv versions.
pyenv global 3.9.0
pyenv local 3.9.0
pyenv shell 3.9.0
Uninstall a specific Python version
pyenv uninstall 3.8.0
Now you can see manage your Python environment can be made simple with tools like Pyenv.
搶先發佈留言