How to install and use pyenv, what to do if you can't switch python versions

Recently I had the opportunity to use python and tried to prepare the development environment, but it took a lot of time when I introduced pyenv to build a version control environment. Here, I will introduce how to install pyenv, the basics of how to use it, and how to deal with errors that I personally encountered. Also, please do not hesitate to point out any mistakes.

Environment used

The environment used is OS: macOS Sierra (version 10.12.2). There is also a place to use vim on the way. Shell: bash

What is pyenv

It's a tool that makes it easy to switch between python versions. It is convenient for switching between 2 series and 3 series. You can also switch the python version for each directory.

Install pyenv

First, let's install pyenv. If you bring it from git, you can use the above command, and if you can use brew, you can use the following command.

$ git clone git://github.com/yyuu/pyenv.git ~/.pyenv
$ brew install pyenv

Edit bash_profile

Then edit bash_profile to get it through the path of pyenv.

$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(pyenv init -)"' >> ~/.bash_profile

Or you can use vim to write directly to bash_profile.

$ vim ~/.bash_profile
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"

Finally apply the save of bash_profile

$ source ~/.bash_profile

Install python with pyenv

Let's install python with two different versions to experience the version switching. If you want to check if it can be installed, you can check the list by using the third command.

$ pyenv install 2.7.10
$ pyenv install 3.5.0
$ pyenv install --list  

Try switching versions with pyenv

Try switching versions. If it is local, it will be reflected in the current directory, and if it is global, it will be reflected in the whole.

$ pyenv local 2.7.10
$ pyenv global 3.5.0

Check the python version to see if the changes are reflected here.

$ python --version
Python 2.7.10

There is no problem if the changes are reflected in this way. If it is not reflected, please try the article below.

What to do if the python version in pyenv is different from python -v

For example, I should have switched the version to 2.7.10 with pyenv, but when I checked the python version, it was 3.5.0 ... There may be something like that.

$ pyenv versions
  system
* 2.7.10 
  3.5.0
$ python --version                                                                                             
Python 3.5.0

In that case, try displaying the path with the which command.

$ which python
/usr/bin

If it looks like this, it's referencing the python that comes with your PC by default. If you want to change the path to refer to pyenv here, edit it as follows.

$ vim /etc/paths
/usr/local/bin
/usr/bin
/bin
/usr/local/sbin
/usr/sbin
/sbin

The path will now prioritize / usr / local over usr / bin. If you enter which command again for confirmation

$ which python
/usr/local/bin/pyenv

It should look like this. Finally, try again to see if the python version of pyenv matches the python version.

Reference links

This is a link that I used as a reference. If you want to know more details, please use it. -Summary of pyenv usage -Build a Python environment on Mac using pyenv -Can't open file for writing by a user who does not have write permission

Recommended Posts

How to install and use pyenv, what to do if you can't switch python versions
What to do if you can't install pyaudio with pip #Python
What to do if you can't use scikit grid search in Python
What to do if you can't pip install mysqlclient
Install pyenv on MacBook Air and switch python to use
What to do if you can't use WiFi on Linux
What to do if you can't use the trash in Lubuntu 18.04.
What to do if you can't pipenv shell
What to do if ipython and python start up with different versions
What to do if you can't install with pip in babun environment
mac OS X 10.15.x pyenv Python If you can't install
What is pip and how do you use it?
What to do when you can't bind CaboCha to Python
What to do if you get an error when installing python with pyenv
What to do if you get an OpenSSL error when installing Python 2 with pyenv
How to install and use Tesseract-OCR
What to do if you installed pyenv on Mac OS using Homebrew but the python version doesn't switch
What to do if you can't sort files with subscripts
How to install and use Graphviz
What to do if you can't log in as root
What to do if Python does not switch from the System version in pyenv
What to do if you can't hit the arrow keys in the Python interactive console
What to do if you run python in IntelliJ and end with an error
What to do if you get Swagger-codegen in python and Import Error: No module named
What to do if you get an Undefined error when trying to use pip with pyenv
What to do if you get a minus zero in Python
How to use functions in separate files Perl and Python versions
What to do if you get a UnicodeDecodeError with pip install
Indispensable if you use Python! How to use Numpy to speed up operations!
What to do if you can't build your project with Maven
python: How to use locals () and globals ()
How to switch python versions in cloud9
How to use Python zip and enumerate
How to use is and == in Python
What to do if you couldn't send an email to Yahoo with Python.
What to do if pyenv install does not proceed with an error
How to use tkinter with python in pyenv
[Python] How to use hash function and tuple.
How to use Ruby's PyCall to enable pyenv Python
How to install Cascade detector and how to use it
How to install Python
How to install python
What to do if you get "Python not configured." Using PyDev in Eclipse
How to use Pyenv
If you try to install Python2 pip after installing Python3 pip and it is rejected
What to do if pip install fails in Xcode 5.1
[Python] [Django] How to use ChoiceField and how to add options
No module named What to do if you get'libs.resources'
ModuleNotFoundError: No module What to do if you get'tensorflow.contrib'
What to do if pip install fails to install dependent libraries
[Python] How do you use lambda expressions? ?? [Scribbles] [Continued-1]
What to do if pyenv is not enabled (zsh)
What to do if you get the error RuntimeError: Python is not installed as a framework when trying to use matplitlib and pylab in Python 3.3
What to do if the Microsoft Store opens even if you run python on Windows
What to do if you get `No kernel for language python found` in Hydrogen
What to do if you get an error when importing matplotlib in Python (Mac)
What to do if you don't want to use Japanese column names when using ortoolpy.logistics_network
What to do if you get the error "Error: opencv3: Does not support building both Python 2 and 3 wrappers" when installing openCV 3
[2020.8 latest] How to install Python
How to install Python [Windows]
[Python] How to use list 1