Installation and use of pyenv and problem solving

It is easy to use pyenvand manage different versions of Python, and the environment of each version is completely independent without interfering with each other. It is highly recommended to combine the shell under Linux. Here are some steps to install and use under the Ubuntu system, and also record some solutions to some more complicated problems using Pyenv.pyenv-virtualenv

Install

Dependency installation

In order to avoid some unnecessary troubles, it is recommended to install some dependencies in advance:

sudo apt install -y git make wget curl build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev libncurses5-dev libncursesw5-dev llvm 

install pyenv

  • Clone the project to the current user directory
git clone https://github.com/pyenv/pyenv.git ~/.pyenv

Note: If you use zsh, ~/.bash_profilechange the following to~/.zshrc

  • Add environment environment variables
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
  • add pyenv initto the shell
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
  • restart shell
exec "$SHELL"
  • View the installed version of pyenv, you can view the version of Python installed on the machine
pyenv verson

Install pyenv-virtualenv

  • The installation path of the clone plugin to pyenv
git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
  • added pyenv virtualenv-initto the shell
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile

Note: If you use zsh, ~/.bash_profilechange the following to~/.zshrc

  • restart shell
exec "$SHELL"

use

  • view current version
pyenv version
  • view all editions
pyenv versions
  • View all available versions
pyenv install --list
  • Install the specified version
pyenv install 3.6.5
  • Rehash after installing the new version
pyenv rehash
  • delete specified version
pyenv uninstall 3.6.5
  • Specify the global version
pyenv global 3.6.5
  • Create a virtual environment of version 3.6.5, named v365env
pyenv virtualenv 3.6.5 v365env
  • Activate the virtual environment
pyenv activate v365env
  • Close the virtual environment
pyenv deactivate v365env
  • If you specify a partial version, it will be created in the current directory .python-version. If it is a git warehouse, please remember to add this file to it .gitignore. The next time you enter the current directory, the virtual environment will be automatically activated.
pyenv local v365env

question

python3.6 uses tkinter to prompt No module named ‘_tkinter’the problem:

import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named ‘_tkinter’

The solution is as follows:

sudo apt update
sudo apt install python3-tk  # (如果是python2.x,安装sudo apt-get install python-tk即可)
sudo apt install tk-dev

After the installation is complete, reinstall Python:

pyenv uninstall 3.6.6  # 卸载原来安装的版本
pyenv install 3.6.6  # 重新安装

Guess you like

Origin blog.csdn.net/MrTeacher/article/details/118077167