Python environment construction on Mac (pyenv, virtualenv, anaconda, ipython notebook)

Introduction

I will summarize the environment construction of python for the future.

--Version control (pyenv) --Environment management (virtualenv) --Also use anaconda (anaconda: useful python package) --Also touch on iPython notebook (development must item if you don't know)

basic configuration

Install pyenv virtualenv

First, install pyenv and virtualenv with homebrew. (Homebrew is assumed to be installed.)

You can manage the version with pyenv. You can manage each environment such as development environment with virtualenv.

$ brew install pyenv
$ brew install pyenv-virtualenv

Pass through

Then pass the path. Describe the following in .bash_profile. Then, it will be loaded automatically when the terminal is started. (If the login shell is not bash, please take appropriate action)

~/.bash_profile


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

By passing the path, you will be able to execute the command as follows.

$ pyenv

Load .bash_profile once.

$ source ~/.bash_profile

Install anaconda with pyenv

Installation of anaconda

Now, let's install python.

$ pyenv install --list

This will give you a list of installable python versions. You can install any version of python from this. Here, we will install anaconda. Anaconda is very convenient because it sets up and prepares Python itself and frequently used packages together. This is easier than installing python alone.

$ pyenv install anaconda3-2.4.1

This command takes a long time to complete. This will install anaconda with various packages installed on Python 3.

Switch the version used

Check the version currently in use.

$ pyenv versions

If you have ʻanaconda3-2.4.1as below, it's okay. The version with* is the version you are currently using, so switch to ʻanaconda3-2.4.1.

* system
anaconda3-2.4.1 (set by /Users/your_name/.pyenv/version)
$ pyenv global anaconda3-2.4.1
$ pyenv versions
system
* anaconda3-2.4.1 (set by /Users/your_name/.pyenv/version)

Installation of other versions

Now that you can use python, let's also install python2 anaconda.

$ pyenv install anaconda-2.4.0
system
anaconda-2.4.0
* anaconda3-2.4.1 (set by /Users/your_name/.pyenv/version)

After that, you can switch at any time as follows. Since there are situations where it is necessary to use python2 and python3 properly, I think it is better to prepare both. You may have felt the strength of pyenv, which allows you to easily switch versions.

$ pyenv global anaconda-2.4.0

Try using ipython notebook

By installing anaconda, you can use your favorite version of python. Let's briefly touch on ʻipython notebook`, which you must know if you develop with python.

After installing anaconda, the environment is already in place.

$ ipython notebook

When you type, the browser will be launched.

You can create a new file from "New"> "Python3" in the upper right corner of your browser.

スクリーンショット_2016-08-13_16_56_31.png

When the file opens, you can write python code and press "Shift + enter" to execute the code in the cell. You can easily draw a graph with the following feeling.

スクリーンショット 2016-08-13 17.02.57.png

It's a very powerful tool in development, so it's a good idea to have it available.

I posted a post about data analysis before, but this is all done on the ipython notebook. Practical exercise of data analysis with Python ~ 2016 New Coder Survey Edition ~

Environmental management with virtual-env

When developing a team, it is necessary to unify the development environment with the members. In such a case, I think it is better to set up for each environment.

Creating virtual-env

First, install a common version of python. (Here 3.5 series)

$ pyenv install 3.5.1
$ pyenv virtualenv 3.5.1 hoge

This will create an environment named "hoge" in 3.5.1. Then change to the directory that specifies the environment. Then specify the version in it.

$ pyenv local hoge

By doing this, the environment of "hoge" is applied in this directory.

This completes the environment settings.

Note that unlike anaconda, it's pure python, so you need to install the required packages.

Package installation and management

Let's install the necessary packages as follows. The installation of numpy below is an example.

$ pip install numpy

Now, in order to unify the environment with others, you also need to synchronize the version of the installed package. Now let's output your own environment.

$ pip freeze > requirements.txt

The file name on the right side of pip freeze is the output file name. Generally, it seems to be output to requirements.txt, so I think it is better to use this name.

For example, I installed only numpy and output it to requirements.txt.

$ pip install numpy
$ pip freeze > requirements.txt
$ cat requirements.txt
numpy==1.11.1

Others install the package from this file.

$ pip install -r requirements.txt

This will install the packages described in the file. I often see it in the README of the github repository where I install the package.

Remove virtual-env

Finally, there is a way to remove this virtual environment.

$ pyenv uninstall hoge

You can now delete the created virtual environment.

Recommended Posts

Python environment construction on Mac (pyenv, virtualenv, anaconda, ipython notebook)
Python3 TensorFlow environment construction (Mac and pyenv virtualenv)
Anaconda python environment construction on Windows 10
Anaconda environment construction on Mac (2018 version)
Python environment construction memo on Mac
Python environment construction (pyenv, anaconda, tensorflow)
Environment construction of python3.8 on mac
Create a Python (pyenv / virtualenv) development environment on Mac (Homebrew)
Build Python environment with Anaconda on Mac
Pyenv + virtualenv on Mac
Mac environment construction Python
Try to build python and anaconda environment on Mac (by pyenv, conda)
Python + Anaconda + Pycharm environment construction
Anaconda environment construction on CentOS7
Python environment construction For Mac
Anaconda3 python environment construction procedure
[Python] Anaconda, pyenv, virtualenv, .bash_profile
Data analysis environment construction with Python (IPython notebook + Pandas)
Build a Python environment on your Mac using pyenv
[Python] Django environment construction (pyenv + pyenv-virtualenv + Anaconda) for macOS
Install Python environment on local PC (pyenv, venv on Mac)
Building a Python environment on Mac
Python environment construction memo on Windows 10
[Python] Anaconda environment construction (installation, startup, virtual environment, package management) Mac environment
Create a Python environment on Mac (2017/4)
I checked Mac Python environment construction
Install Python 3.7 Anaconda on MAC, but Python 2
Python development environment construction on macOS
Python environment construction (pyenv + poetry + pipx)
Python3 TensorFlow for Mac environment construction
Python3.6 environment construction (using Win environment Anaconda)
OpenCV3 & Python3 environment construction on Ubuntu
VScode environment construction (on Mac) & graph display in Python (@browser)
How to rebuild python environment from pyenv on Mac environment (El Capitan)
Build a Python environment on your Mac with Anaconda and PyCharm
Install Anaconda on Mac and upload Jupyter (IPython) notebook to Anaconda Cloud
[Ubuntu 18.04] Python environment construction with pyenv + pipenv
[Python] Building an environment with Anaconda [Mac]
Python (anaconda) development environment construction procedure (SpringToolsSuites) _2020.4
Create a python environment on your Mac
Put Anaconda on your Mac using Pyenv
Notes on building Python and pyenv on Mac
Python environment construction
Environment construction (python)
pyenv + virtualenv + Anaconda
python on mac
python environment construction
Python --Environment construction
pyenv + anaconda + python3
Python environment construction
python environment construction
Python virtual environment construction (2017 version) pyenv and pyenv-virtualenv and virtualenv and virtualenv wrapper and pyvenv and venv
Notes on setting pyenv and python environment using Homebrew on Mac OS Marvericks
Create a Python3 environment with pyenv on Mac and display a NetworkX graph
[Note] Python environment construction on rental server "CORESERVER"
Put Python's numerical calculation environment Anaconda on mac (2)
Put Python's numerical calculation environment Anaconda on mac
Build python environment with pyenv on EC2 (ubuntu)
Build Python3.5 + matplotlib environment on Ubuntu 12 using Anaconda
Python explosive environment construction starting from zero (Mac)
Python environment construction (Anaconda + VSCode) @ Windows10 [January 2020 version]