Managing Python Versions and Virtual Environments using pyenv

WBOY
發布: 2024-08-05 21:23:42
原創
595 人瀏覽過

Managing Python Versions and Virtual Environments using pyenv

The pyenv package is a very useful tool for managing multiple versions of Python, without too much hassle. It also comes with various plugins for streamlining the development experience, including pyenv-virtualenv, which provides features for managing virtual environments and conda environments.

Unfortunately, pyenv is not supported on Windows. However, we recommend using the pyenv-win fork for Windows users.

Installing pyenv

1. Install required Python build dependencies

Mac OS X

brew install openssl readline sqlite3 xz zlib
登入後複製

Ubuntu/Debian/Mint

sudo apt-get update; sudo apt-get install 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
登入後複製

Alpine

apk add --no-cache git bash build-base libffi-dev openssl-dev bzip2-dev zlib-dev readline-dev sqlite-dev
登入後複製

2. Installing pyenv

The fastest way to install pyenv and some of popular plugins is to use the pyenv-installer:

curl https://pyenv.run | bash
登入後複製

Next up, restart your shell so the path changes take effect:

exec $SHELL
登入後複製

3. Updating pyenv

Updating pyenv is as simple as:

pyenv update
登入後複製

4. Switching between Python versions

  • pyenv local 3.3.3 - Sets Python 3.3.3 in the local shell.
  • pyenv global 2.7.3 - Sets Python 2.7.3 globally, in all shells.

5. Managing virtual environments and Python versions

  • pyenv virtualenv 3.3.3 virtual-env-name - Creates a virtual environment called virtual-env-name that uses Python 3.3.3
  • pyenv virtualenvs - Shows the created virtual environments.
  • pyenv activate virtual-env-name - Activates the virtual environment with the virtual-env-name name.
  • pyenv deactivate - Deactivates the currently activated virtual environment.

Uninstalling pyenv

The pyenv executable is installed in $PYENV_ROOT, which defaults to ~/.pyenv. To uninstall it, just simply remove it:

rm -fr ~/.pyenv
登入後複製

Then clean up your .bashrc file, by removing the following lines from it:

export PATH="$HOME/.pyenv/bin:$PATH" eval "$(pyenv init --path)" eval "$(pyenv virtualenv-init -)"
登入後複製

Conclusion

In this article we've learned how simple and easy it is to use pyenv and its related plugins for managing multiple versions of Python and virtual environments from a single machine.

This article was originally published here.

以上是Managing Python Versions and Virtual Environments using pyenv的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!