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.
The environment used is OS: macOS Sierra (version 10.12.2). There is also a place to use vim on the way. Shell: bash
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.
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
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
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. 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.
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.
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