Building a python environment for artificial intelligence (Chainer / TensorFlow / CSLAIER)

table of contents

  1. Purpose of the article
  2. My development environment
  3. What to install
  4. Precautions when building the environment
  5. Install Homebrew
  6. Install pyenv ・ About environment variables ・ About .bash_profile ・ About vi command
  7. Install anaconda
  8. Supplement (About virtualenv)

Purpose of the article

Artificial intelligence is getting excited recently! I'm new to programming, but I wanted to use artificial intelligence, so I used python for the first time. It took a lot of time to build the environment on mac, so I summarized the points I did not understand. I hope it helps you build a python environment for artificial intelligence.

Writer's development environment

・ MacBook Pro (Retina, 13inch, Early 2015) · MacOS Sierra (version 10.12.2) ・ Processor (2.7 GHz Intel Core i5) · Memory (8 GB 1867 MHz DDR3) · Graphics (Intel Iris Graphics 6100 1536 MB)

What to install

In this article we will install three things. And for each, why install it? I will explain and post a reference article. ・ Installation of Homebrew ・ Installation of pyenv -Installation of anaconda (which contains python itself)

Precautions when building an environment

First of all, about the points to note when installing python and building the environment. There are two versions of python, the old version 2 series and the new version 3 series (and each of the 2 series and 3 series also has versions such as 2.x and 3.x). Normally, I think it's okay to use the new version 3 series, but that's not the case with python. This is because the 3rd system is gradually being used by many people, but there is no compatibility between the 2nd and 3rd systems, and there are still many libraries that only work with the 2nd system. Therefore, python does not install libraries etc. directly (in the global environment) on your personal computer, but creates and installs virtual environments separately for 2nd and 3rd systems. By the way, what is a virtual environment? A total of specifications and functions of a computer (virtual machine) virtually constructed by software on a computer. I think it's like creating a space in my computer that doesn't interfere with each other and developing there.

If you put the library directly on your computer, you will be wondering whether the version of python you are using is 2 or 3, and which library you have installed supports. I don't understand. Therefore, you should prepare a virtual environment that works with either, install the library separately by version, and manage it.

Homebrew installation

First of all, install a package management system called Homebrew. Homebrew makes it easy to install and remove software with a single command. Regarding Homebrew, the following sites are well organized and helpful. If you're not familiar with Homebrew, take a look.

"Who is homebrew? I investigated how it works." http://qiita.com/omega999/items/6f65217b81ad3fffe7e6

Now it's time to install Homebrew. To install Homebrew

・ An Intel CPU 1 ・ OS X 10.10 or higher 2 -Command Line Tools (CLT) for Xcode: xcode-select --install ・ A Bourne-compatible shell for installation (e.g. bash or zsh) 4 Seems to be needed.

The most common cause of installation errors is not having Command Line Tools installed. The following site is easy to understand about how to install Command Line Tools. If not, please read it.

"Install [mac] Command Line Tools" http://www.programing-style.com/mac/mac-command-line-tools-install/

When you're ready, start installing Homebrew.

homebrew installation command


$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"  

You will be prompted to enter the password on the way. It will take some time.

Check if it was installed.

Confirmation of installation


$ brew doctor

If the installation is not successful, Warning will be displayed, but the following site was helpful.

"Procedures for installing Homebrew on Mac and how to solve Warnings" http://qiita.com/unsoluble_sugar/items/acaffa6d0e28c3c24934

Well, if you can install it successfully, I will update Homebrew just in case.

brew update


$ brew update

You can now keep Homebrew up to date.

Install pyenv

pyenv is a tool for building a virtual environment of python and switching / managing versions. With this, you can easily switch between python 2 series and 3 series. Now let's install pyenv using Homebrew, which we installed earlier.

Install pyenv


$ brew install pyenv

I think that installing pyenv can be done easily with the above command, but after installing it, it was troublesome for me. pyenv has to go through environment variables, not just install it.

In addition to environment variables, you will also need a little knowledge of hidden files (.bash_profile) and vi commands to pass them.

How to set environment variables

About environment variables

Environment variables are a mechanism for storing settings related to application behavior, and are variables that store information required by the system.

"What is PATH? (Mac OS X)" http://qiita.com/soarflat/items/09be6ab9cd91d366bf71

About .bash_profile

Roughly speaking, ".bash_profile" is a file that is read when you start your computer and makes various settings. If you write environment variables here, it will be set so that you can use it. In your home directory (~), type ls –a to see all files, including hidden files one level below (file names with a'.' in front of them). You can see that there is a "bash_profile".

"What is .bash_profile? | An IT terminology dictionary that" I understand "and" I don't understand "but" I understand " http://wa3.i-3-i.info/word13650.html

About vi command

I think it's okay to remember that the vi command is the command used here to change the ".bash_profile" in the terminal.

"What is vi [command]? | An IT terminology dictionary that makes you feel like you understand" but "do not understand" http://wa3.i-3-i.info/word11517.html

Setting environment variables

Now let's set the environment variables. In your home directory, type ls –a and make sure there is a ".bash_profile" in the hierarchy below.

.bash_Confirmation of profile location


$ ls -a

If you have ".bash_profile"

.bash_Enter profile


$ vi .bash_profile

You can only see the state just entered. This is where the vi command comes into play. While in the file

vi command


i

When you enter, you will be in input mode and you can enter new ones (make sure that the keyboard is in hiragana or katakana, so make sure it is in alphabetic characters). If you see --INSERT --at the end of the file, you are in input mode.

This is a newly added environment variable.

Newly added environment variables


export PATH="/usr/local/Cellar/.pyenv/shims:$PATH"
export PATH=${PYENV_ROOT}/bin:$PATH
eval "$(pyenv init -)"

You can enter it anywhere in the file. It's easy to understand, so I wonder if the last one is the best. After inputting, press the "esc key" to exit the input mode. Then save and exit .bash_profile.

Save and.bash_Exit profile


shift + zz

If you change ".bash_profile", be sure to reflect it.

Save changes


$ source .bash_profile

This article is very detailed on how to pass environment variables. "Understanding the setting of environment variables to pass the PATH (Mac OS X)" http://qiita.com/soarflat/items/d5015bec37f8a8254380

Installation of anaconda

Once you have successfully set the environment variables, you can see a list of what you can install with pyenv.

List


pyenv install -l

This time, instead of installing python itself directly, in addition to python itself, install anaconda, which can install packages often used in python at once. It is very convenient because Numpy and Six, which are necessary for installing Chainer (a popular artificial intelligence library in Japan), are already installed in anaconda. It saves you the trouble of installing them separately and makes management easier. anaconda2-x.x.x is for 2 series and anaconda3-x.x.x is for 3 series.

Installation of anaconda2 system (latest is anaconda2-4.2.0 as of January 4, 2017)

Installation of anaconda2 system using pyenv


$ pyenv install anaconda2-4.2.0

Installation of anaconda3 series (latest is anaconda3-4.2.0 as of January 4, 2017)

Installation of anaconda3 system using pyenv


$ pyenv install anaconda3-4.2.0

You can probably install it without any problems. Check the version of python you are currently using with pyenv.

Check version


$ pyenv versions

What is displayed in the terminal


 *system
  anaconda2-4.2.0 (set by /Users/your_name/.pyenv/version)
  anaconda3-4.2.0

The version marked with * is the version of python you are currently using. I think the default is system. Let's switch the version to python2 system.

Version change to 2 system


$ pyenv global anaconda2-4.2.0

Let's check if we could change it.

Check version


$ pyenv versions

What is displayed in the terminal


  system
 *anaconda2-4.2.0 (set by /Users/your_name/.pyenv/version)
  anaconda3-4.2.0

If * attached to system is attached to anaconda2-4.2.0, it is successful. Be sure to update after changing the version.

Update pyenv


$ pyenv rehash

You can check the packages included in anaconda.

Check the package included in anaconda


$ conda list

You can see that Numpy and Six are included. Python is also included. I was able to install python safely.

Supplement

About virtualenv

virtualenv can build different environments with the same python version. pyenv is a python version control, virtualenv is a virtual environment management tool in the same python version. I didn't set virtualenv here, but if you are interested, please take a look.

"Virtual Environment Version Control Summary Python" http://qiita.com/hedgehoCrow/items/0733c63c690450b14dcf

"Building an environment with pyenv and virtualenv" http://qiita.com/Kodaira_/items/feadfef9add468e3a85b

Next time, I would like to install Chainer, an artificial intelligence library. If you have any mistakes, I would appreciate it if you could comment.

Recommended Posts

Building a python environment for artificial intelligence (Chainer / TensorFlow / CSLAIER)
[Mac] Building a virtual environment for Python
Building a Python development environment for AI development
Building a Python virtual environment
Building a Python virtual environment
Building a Python environment for pyenv, pyenv-virtualenv, Anaconda (Miniconda)
Write about building a Python environment for writing Qiita Qiita
Building a Docker working environment for R and Python
Procedure for building a CDK environment on Windows (Python)
Building a Python environment for programming beginners (Mac OS)
Memo for building a machine learning environment using Python
Building a Python environment on Mac
Building a Python environment on Ubuntu
Building a development environment for Android apps-creating Android apps in Python
Building a Hy environment for Lisper who hasn't touched Python
Building a virtual environment with Python 3
[Python] Building a virtual python environment for the pyramid tutorial (summary)
Python3 TensorFlow for Mac environment construction
Building a Docker working environment for R and Python 2: Japanese support
How about Anaconda for building a machine learning environment in Python?
Building a Windows 7 environment for getting started with machine learning with Python
From building a Python environment for inexperienced people to Hello world
[Pyenv] Building a python environment with ubuntu 16.04
Building a Python3 environment with Amazon Linux2
Let's create a virtual environment for Python
Building a Python 3.6 environment with Windows + PowerShell
Building a conda environment for ROS users
Building a virtual environment for Mayavi dedicated to Python 3.6, Anaconda, Spyder users
Selenium + WebDriver (Chrome) + Python | Building environment for scraping
Building a python environment with virtualenv and direnv
Building an environment for executing Python scripts (for mac)
Building an Anaconda environment for Python with pyenv
Building a Python environment with WLS2 + Anaconda + PyCharm
[Python] Web development preparation (building a virtual environment)
Think about building a Python 3 environment in a Mac environment
Commands for creating a python3 environment with virtualenv
Procedure for creating a Python quarantine environment (venv environment)
A memo for creating a python environment by a beginner
Building a Python environment on a Sakura VPS server
Create a Python environment
Python environment for projects
Recommendation of building a portable Python environment with conda
Playing with a user-local artificial intelligence API in Python
Building a training environment for penetration testing using Naumachia
Build a python environment for each directory with pyenv-virtualenv
Building a TensorFlow environment that uses GPU on Windows 10
Building an environment for natural language processing with Python
Building and enabling a python virtual environment, etc. (venv)
How to build a development environment for TensorFlow (1.0.0) (Mac)
conda memorandum: Building a Python environment with supercomputer ITO
The first artificial intelligence. Tensorflow on CentOS7.0. Built separately for python3.5 series and python2.7 series with virtualenv.
Building a Python environment on a Mac and using Jupyter lab
Summary from building Python 3.4. * From source to building a scientific computing environment
[Note] List of basic commands for building python / conda environment
The shortest memo for building a python environment that takes less than 5 minutes to work.
Python environment construction For Mac
Python3 environment construction (for beginners)
Python environment construction and TensorFlow
[For organizing] Python development environment
Build a Python environment offline
Precautions that must be understood when building a PYTHON environment