LoginSignup
5
6

More than 5 years have passed since last update.

Python環境構築(pyenv, anaconda, tensorflow)

Last updated at Posted at 2017-02-19

pyenv

$ brew install pyenv
$ vi ~/.bash_profile
# python
PYENV_ROOT=~/.pyenv
export PATH=$PATH:$PYENV_ROOT/bin
eval "$(pyenv init -)"
$ source ~/.bash_profile
$ python -V
Python 2.7.13
$ pyenv version
system (set by /Users/takahiro/.pyenv/version)
$ pyenv help
Usage: pyenv <command> [<args>]

Some useful pyenv commands are:
   commands    List all available pyenv commands
   local       Set or show the local application-specific Python version
   global      Set or show the global Python version
   shell       Set or show the shell-specific Python version
   install     Install a Python version using python-build
   uninstall   Uninstall a specific Python version
   rehash      Rehash pyenv shims (run this after installing executables)
   version     Show the current Python version and its origin
   versions    List all Python versions available to pyenv
   which       Display the full path to an executable
   whence      List all Python versions that contain the given executable

anaconda

データサイエンスで使われる、たいていのライブラリがあらかじめ含まれているとのこと

$ pyenv install -l | grep anaconda3
  anaconda3-2.0.0
  anaconda3-2.0.1
  anaconda3-2.1.0
  anaconda3-2.2.0
  anaconda3-2.3.0
  anaconda3-2.4.0
  anaconda3-2.4.1
  anaconda3-2.5.0
  anaconda3-4.0.0
  anaconda3-4.1.0
  anaconda3-4.1.1
  anaconda3-4.2.0
$ pyenv install anaconda3-4.2.0
$ pyenv versions
* system (set by /Users/takahiro/.pyenv/version)
  anaconda3-4.2.0
$ pyenv shell anaconda3-4.2.0
$ pyenv version
anaconda3-4.2.0 (set by PYENV_VERSION environment variable)
$ python -V
Python 3.5.2 :: Anaconda 4.2.0 (x86_64)
$ pyenv global anaconda3-4.2.0

condaコマンド

$ conda -h
usage: conda [-h] [-V] command ...

conda is a tool for managing and deploying applications, environments and packages.

Options:

positional arguments:
  command
    info         Display information about current conda install.
    help         Displays a list of available conda commands and their help
                 strings.
    list         List linked packages in a conda environment.
    search       Search for packages and display their information. The input
                 is a Python regular expression. To perform a search with a
                 search string that starts with a -, separate the search from
                 the options with --, like 'conda search -- -h'. A * in the
                 results means that package is installed in the current
                 environment. A . means that package is not installed but is
                 cached in the pkgs directory.
    create       Create a new conda environment from a list of specified
                 packages.
    install      Installs a list of packages into a specified conda
                 environment.
    update       Updates conda packages to the latest compatible version. This
                 command accepts a list of package names and updates them to
                 the latest versions that are compatible with all other
                 packages in the environment. Conda attempts to install the
                 newest versions of the requested packages. To accomplish
                 this, it may update some packages that are already installed,
                 or install additional packages. To prevent existing packages
                 from updating, use the --no-update-deps option. This may
                 force conda to install older versions of the requested
                 packages, and it does not prevent additional dependency
                 packages from being installed. If you wish to skip dependency
                 checking altogether, use the '--force' option. This may
                 result in an environment with incompatible packages, so this
                 option must be used with great caution.
    upgrade      Alias for conda update. See conda update --help.
    remove       Remove a list of packages from a specified conda environment.
    uninstall    Alias for conda remove. See conda remove --help.
    config       Modify configuration values in .condarc. This is modeled
                 after the git config command. Writes to the user .condarc
                 file (/Users/takahiro/.condarc) by default.
    clean        Remove unused packages and caches.
    package      Low-level conda package utility. (EXPERIMENTAL)
$ conda info
Current conda install:

               platform : osx-64
          conda version : 4.2.9
       conda is private : False
      conda-env version : 4.2.9
    conda-build version : 2.0.2
         python version : 3.5.2.final.0
       requests version : 2.11.1
       root environment : /Users/takahiro/.pyenv/versions/anaconda3-4.2.0  (writable)
    default environment : /Users/takahiro/.pyenv/versions/anaconda3-4.2.0
       envs directories : /Users/takahiro/.pyenv/versions/anaconda3-4.2.0/envs
          package cache : /Users/takahiro/.pyenv/versions/anaconda3-4.2.0/pkgs
           channel URLs : https://repo.continuum.io/pkgs/free/osx-64/
                          https://repo.continuum.io/pkgs/free/noarch/
                          https://repo.continuum.io/pkgs/pro/osx-64/
                          https://repo.continuum.io/pkgs/pro/noarch/
            config file : None
           offline mode : False
$ conda list | grep pandas
pandas                    0.18.1              np111py35_0

Tensorflowインストール

$ conda list | grep tensorflow
$ conda install -c conda-forge tensorflow
$ python
Python 3.5.2 |Anaconda 4.2.0 (x86_64)| (default, Jul  2 2016, 17:52:12)
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> import tensorflow as tf
>>> node1 = tf.constant(3.0, tf.float32)
>>> node2 = tf.constant(4.0) # also tf.float32 implicitly
>>> print(node1, node2)
Tensor("Const:0", shape=(), dtype=float32) Tensor("Const_1:0", shape=(), dtype=float32)

>>> sess = tf.Session()
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
>>> print(sess.run([node1, node2]))
[3.0, 4.0]

作業フォルダごとに環境を変える

pyenvで別バージョンをインストールして、 pyenv localを使う方法

  • python2インストール
$ pyenv install -l | grep anaconda2
  anaconda2-2.4.0
  anaconda2-2.4.1
  anaconda2-2.5.0
  anaconda2-4.0.0
  anaconda2-4.1.0
  anaconda2-4.1.1
  anaconda2-4.2.0
$ pyenv install anaconda2-4.2.0
$ pyenv versions
  system
  anaconda2-4.2.0
* anaconda3-4.2.0 (set by PYENV_VERSION environment variable)
  • 作業フォルダ
$ mkdir py2 
$ cd py2
$ pyenv local anaconda2-4.2.0
$ pyenv version
anaconda2-4.2.0 (set by /Users/takahiro/workspace/py2/.python-version)
$ python -V
Python 2.7.12 :: Anaconda 4.2.0 (x86_64)

condaの仮想環境を作って、pyenv localを使う方法

  • 仮想環境作成
$ conda create -n tensorflow
$ conda info -e
# conda environments:
#
tensorflow               /Users/takahiro/.pyenv/versions/anaconda3-4.2.0/envs/tensorflow
root                  *  /Users/takahiro/.pyenv/versions/anaconda3-4.2.0
$ pyenv versions
  system
  anaconda2-4.2.0
* anaconda3-4.2.0 (set by /Users/takahiro/.pyenv/version)
  anaconda3-4.2.0/envs/tensorflow
  • 作業フォルダ
$ mkdir tensorflow
$ cd tensorflow
$ pyenv local anaconda3-4.2.0/envs/tensorflow
$ pyenv version
anaconda3-4.2.0/envs/tensorflow (set by /Users/takahiro/workspace/tensorflow/.python-version)
5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6