Building a Python environment on Mac

Since I did a clean installation of Mac, I will write about building a Python development environment.

Installation of pyenv and virtualenv

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

Install Python3

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.

Creating a virtual environment with virtualenv

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]

Finally

It seems that it will be more convenient if you use a wheel, but I will do it again this time.

Recommended Posts

Building a Python environment on Mac
Building a Python environment on Ubuntu
Create a Python environment on Mac (2017/4)
Create a python environment on your Mac
[Mac] Building a virtual environment for Python
Building a Python environment on a Mac and using Jupyter lab
Building a Python virtual environment
Building a Python virtual environment
Build a Python environment on Mac (Mountain Lion)
Build a Python development environment on your Mac
Think about building a Python 3 environment in a Mac environment
Building a Python environment on a Sakura VPS server
Error when building mac python environment
Build a machine learning Python environment on Mac OS
Python environment construction memo on Mac
Building a virtual environment with Python 3
Build a Python development environment on Mac OS X
python on mac
Build a Python environment on your Mac using pyenv
Create a python environment on centos
Procedure for building a CDK environment on Windows (Python)
Environment construction of python3.8 on mac
Building a Python environment for programming beginners (Mac OS)
Build a python3 environment on CentOS7
Create a Python (pyenv / virtualenv) development environment on Mac (Homebrew)
Build a python data analysis environment on Mac (El Capitan)
Create a virtual environment for python on mac [Very easy]
Until building a Python development environment using pyenv on Ubuntu 20.04
[Pyenv] Building a python environment with ubuntu 16.04
Django Crispy Tutorial (Environment Building on Mac)
[Python] Building an environment with Anaconda [Mac]
Building a Python3 environment with Amazon Linux2
Build a python environment on MacOS (Catallina)
Building a LaTeX environment on Chrome OS
Building a Python 3.6 environment with Windows + PowerShell
Creating a python virtual environment on Windows
Notes on building Python and pyenv on Mac
I installed Kivy on a Mac environment
Build Python environment with Anaconda on Mac
Build a Python + OpenCV environment on Cloud9
Building a Python development environment for AI development
Install Python on Mac
Install Python 3 on Mac
Create a Python environment
Install Python 3.4 on Mac
Mac environment construction Python
Build a Python environment on your Mac with Anaconda and PyCharm
Everything from building a Python environment to running it on Windows
Building a python environment with virtualenv and direnv
Simply build a Python 3 execution environment on Windows
Building an environment for executing Python scripts (for mac)
Build a python environment with ansible on centos6
Building a Python environment with WLS2 + Anaconda + PyCharm
Building an environment for matplotlib + cartopy on Mac
[Python] Web development preparation (building a virtual environment)
[Venv] Create a python virtual environment on Ubuntu
Set up a Python development environment on Marvericks
Create a Python execution environment on IBM i
Create a Python virtual development environment on Windows
Build a Python development environment on Raspberry Pi
Things to watch out for when creating a Python environment on a Mac