Since I did a clean installation of Mac, I will write about building a Python development environment.
Installed using Homebrew.
$ brew install pyenv-virtualenv
After the installation is complete, follow the instructions of homebrew and add the following line to the configuration file of your shell (~ / .bashrc for bash, ~ / .zshrc for zsh).
$ export PYENV_ROOT=/usr/local/var/pyenv
$ if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi
$ if which pyenv-virtualenv-init > /dev/null; then eval "$(pyenv virtualenv-init -)"; fi
After saving, load the configuration file.
$ source ~ / .bashrc # for bash $ source ~ / .zshrc # for zsh
You can see which versions of Python you can install with pyenv install --list
.
This time I will install the latest 3.5.1 at the moment.
$ pyenv install 3.5.1
However, the build failed here. After investigating, it seems that Xcode command line tools should be installed. So install it with the following command.
$ xcode-select --install
When I installed it with pyenv again, it was installed properly. Installation is complete when the following is displayed.
Downloading Python-3.5.1.tgz...
-> https://www.python.org/ftp/python/3.5.1/ Python-3.5.1.tgz
Installing Python-3.5.1...
Installed Python-3.5.1 to /usr/local/var/pyenv/ versions/3.5.1
Check if it is installed just in case.
$ pyenv versions
I got the following output and it seems that I was able to install it properly.
* system (set by /usr/local/var/pyenv/version)
3.5.1
I want to use the 3 series as the main, so I also set it.
$ pyenv global 3.5.1
$ pyenv versions
system
* 3.5.1 (set by /usr/local/var/pyenv/version)
This has changed. If there is a * on the left side, the setting is complete.
(Addition)
Even though I set it to 3.5.1 with pyenv global
, for some reason, when I look at the version with python --version
, it is Python that comes with the Mac standard.
$ python --version
Python 2.7.5
Reloading the shell config file will result in the version set in pyenv.
$ source ~/.zshrc
$ python --version
Python 3.5.1
Probably because of my environment, I'm investigating the cause.
If you comment out ʻif which pyenv> / dev / null; then eval "$ (pyenv init-)"; fidescribed in
~ / .zshrc, the Python version changes even if
source ~ / .zshrc` I don't have it, so I think this area is probably suspicious.
(Additional notes)
solved.
The order of the lines described in ~ / .zshrc
was just changed ...
I was shocked to take time for such a simple thing.
It was like this (the setting for $ PYENV_ROOT
came after ʻeval" $ (pyenv init-) "`).
$ if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi
$ export PYENV_ROOT=/usr/local/var/pyenv
$ if which pyenv-virtualenv-init > /dev/null; then eval "$(pyenv virtualenv-init -)"; fi
Solved by swapping the first and second lines of this.
$ export PYENV_ROOT=/usr/local/var/pyenv
$ if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi
$ if which pyenv-virtualenv-init > /dev/null; then eval "$(pyenv virtualenv-init -)"; fi
I don't think there are many people who make such mistakes, but I wrote it down just in case.
By setting pyenv virtualenv [version] [name]
, you can create a virtual environment named name
with the Python version version
.
$ pyenv virtualenv 3.5.1 test_351
You have now created a virtual environment named test_351
.
You can check it with pyenv versions
.
$ pyenv versions
system
* 3.5.1 (set by /usr/local/var/pyenv/version)
3.5.1/envs/test_351
test_351
After that, go to the folder used for development and set the virtual environment created earlier.
$ cd dev
$ pyenv local test_351
By doing this, from now on, when you go to the dev
folder, you will automatically be in the environment of test_351
.
If you want to delete the virtual environment, see below.
$ pyenv uninstall [name]
It seems that it will be more convenient if you use a wheel, but I will do it again this time.
Recommended Posts