Python environment construction (pyenv, anaconda, tensorflow)

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

Most of the libraries used in data science are included in advance.

$ 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 command

$ 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 installation

$ 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]

Change the environment for each working folder

How to install another version with pyenv and use pyenv local

$ 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)

How to create a virtual environment for conda and use 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)

Recommended Posts

Python environment construction (pyenv, anaconda, tensorflow)
Python + Anaconda + Pycharm environment construction
Anaconda3 python environment construction procedure
Python environment construction and TensorFlow
Python3 TensorFlow environment construction (Mac and pyenv virtualenv)
Anaconda python environment construction on Windows 10
Python environment construction
Environment construction (python)
install tensorflow in anaconda + python3.5 environment
Python environment construction (pyenv + poetry + pipx)
python environment construction
Python --Environment construction
pyenv + anaconda + python3
[Python] Django environment construction (pyenv + pyenv-virtualenv + Anaconda) for macOS
Python environment construction
Python3 TensorFlow for Mac environment construction
python environment construction
Python3.6 environment construction (using Win environment Anaconda)
Python environment construction on Mac (pyenv, virtualenv, anaconda, ipython notebook)
[Ubuntu 18.04] Python environment construction with pyenv + pipenv
Python (anaconda) development environment construction procedure (SpringToolsSuites) _2020.4
python windows environment construction
Python development environment construction
pyenv + fish environment construction
python2.7 development environment construction
Anaconda environment construction memo
Mac environment construction Python
Python environment construction @ Win7
Python environment construction (Anaconda + VSCode) @ Windows10 [January 2020 version]
Building an Anaconda environment for Python with pyenv
Python 3.x environment construction by Pyenv (CentOS, Ubuntu)
From Python environment construction to virtual environment construction with anaconda
[Ubuntu 18.04] Tensorflow 2.0.0-GPU environment construction
Use Anaconda in pyenv environment
Install Python environment with Anaconda
Anaconda environment construction on CentOS7
Introduced Tensorflow (Win / Anaconda environment)
Python environment construction (Windows10 + Emacs)
CI environment construction ~ Python edition ~
Python environment construction For Mac
Anaconda3 × Pycharm environment construction memo
Python3 environment construction (for beginners)
Python environment construction under Windows7 environment
[MEMO] [Development environment construction] Python
Ubuntu14.04 + GPU + TensorFlow environment construction
[Tensorflow] Tensorflow environment construction on Windows 10
[Python] Anaconda, pyenv, virtualenv, .bash_profile
Environment construction of python2 & 3 (OSX)
Building a Python environment for pyenv, pyenv-virtualenv, Anaconda (Miniconda)
[Environment construction] @anaconda that runs keras / tensorflow on GPU
Python environment construction memo on Windows 10
Get started with Python! ~ ① Environment construction ~
Python + Unity Reinforcement learning environment construction
[Python] Anaconda environment construction (installation, startup, virtual environment, package management) Mac environment
Anaconda environment construction on Mac (2018 version)
I checked Mac Python environment construction
[Python3] Development environment construction << Windows edition >>
Environment construction memo of pyenv + conda
Python development environment construction on macOS
Environment construction of python3.8 on mac
Python3 environment construction with pyenv-virtualenv (CentOS 7.3)