[PYTHON] Build an environment with pyenv, pyenv-virtualenv, jupyter on OS X El Capitan

A story about building an environment by putting pyenv, pyenv-virtualenv, and jupyter in Mac OS X El Capitan.

Install pyenv, pyenv-virtualenv

$ brew install pyenv pyenv-virtualenv

Install python and create virtual environment

$ PYTHON_CONFIGURE_OPTS="--enable-unicode=ucs2 --enable-framework=$(pyenv root)/versions/2.7.11" pyenv install 2.7.11
$ pyenv virtualenv 2.7.11 tech

If you do not add PYTHON_CONFIGURE_OPTS =" --enable-unicode = ucs2 ", you will get an error later in jupyter notebook ↓ Reference: http://qiita.com/sigmalogneko/items/b5c15f5229387bbfd53a

(test) foo-mbp:test foo$ jupyter notebook
Traceback (most recent call last):
  File "/Users/foo/.pyenv/versions/test/bin/jupyter-notebook", line 7, in <module>
    from notebook.notebookapp import main
  File "/Users/foo/.pyenv/versions/2.7.10/envs/test/lib/python2.7/site-packages/notebook/notebookapp.py", line 30, in <module>
    from zmq.eventloop import ioloop
  File "/Users/foo/.pyenv/versions/2.7.10/envs/test/lib/python2.7/site-packages/zmq/__init__.py", line 66, in <module>
    from zmq import backend
  File "/Users/foo/.pyenv/versions/2.7.10/envs/test/lib/python2.7/site-packages/zmq/backend/__init__.py", line 40, in <module>
    reraise(*exc_info)
  File "/Users/foo/.pyenv/versions/2.7.10/envs/test/lib/python2.7/site-packages/zmq/backend/__init__.py", line 27, in <module>
    _ns = select_backend(first)
  File "/Users/foo/.pyenv/versions/2.7.10/envs/test/lib/python2.7/site-packages/zmq/backend/select.py", line 27, in select_backend
    mod = __import__(name, fromlist=public_api)
  File "/Users/foo/.pyenv/versions/2.7.10/envs/test/lib/python2.7/site-packages/zmq/backend/cython/__init__.py", line 6, in <module>
    from . import (constants, error, message, context,
ImportError: dlopen(/Users/foo/.pyenv/versions/2.7.10/envs/test/lib/python2.7/site-packages/zmq/backend/cython/constants.so, 2): Symbol not found: _PyUnicodeUCS2_DecodeUTF8
  Referenced from: /Users/foo/.pyenv/versions/2.7.10/envs/test/lib/python2.7/site-packages/zmq/backend/cython/constants.so
  Expected in: flat namespace
 in /Users/foo/.pyenv/versions/2.7.10/envs/test/lib/python2.7/site-packages/zmq/backend/cython/constants.so

If this happens,

$ pyenv uninstall 2.7.10
$ PYTHON_CONFIGURE_OPTS="--enable-unicode=ucs2" pyenv install 2.7.10

Redo

Also, PYTHON_CONFIGURE_OPTS ="-enable-framework = $ (pyenv root) /versions/2.7.11 " is an option to run Python in framework mode (?). If you don't do this, matplotlib won't work. Change the version number as appropriate. It doesn't work anyway if you're using virtual env (see below)

Use virtual environment

pyenv activate tech

Install jupyter

pip install jupyter

Matplotlib On iPython Notebook

import matplotlib.pyplot

Then, an error occurs.

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-1-c845eb879342> in <module>()
     19 
     20 sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath('__file__')))))
---> 21 import handcraft_feature_extractor
     22 #import text2vec
     23 import config

/Users/foo/codes/pksha/chatbot-base/handcraft_feature_extractor.py in <module>()
      1 #!/usr/bin/env python
      2 # -*- coding: utf-8 -*-
----> 3 from feature_extractor import FeatureExtractor
      4 import MeCab
      5 import config

/Users/foo/codes/pksha/chatbot-base/feature_extractor.py in <module>()
      3 import matplotlib
      4 import numpy as np
----> 5 import matplotlib.pyplot as plt
      6 import MeCab
      7 import re, pprint

/Users/foo/.pyenv/versions/2.7.9/envs/pksha/lib/python2.7/site-packages/matplotlib/pyplot.py in <module>()
    112 
    113 from matplotlib.backends import pylab_setup
--> 114 _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
    115 
    116 _IP_REGISTERED = None

/Users/foo/.pyenv/versions/2.7.9/envs/pksha/lib/python2.7/site-packages/matplotlib/backends/__init__.pyc in pylab_setup()
     30     # imports. 0 means only perform absolute imports.
     31     backend_mod = __import__(backend_name,
---> 32                              globals(),locals(),[backend_name],0)
     33 
     34     # Things we pull in from all backends

/Users/foo/.pyenv/versions/2.7.9/envs/pksha/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py in <module>()
     22 
     23 import matplotlib
---> 24 from matplotlib.backends import _macosx
     25 
     26 

RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends. If you are Working with Matplotlib in a virtual enviroment see 'Working with Matplotlib in Virtual environments' in the Matplotlib FAQ

http://matplotlib.org/faq/virtualenv_faq.html With reference to

Create PATHTOENV / bin / frameworkpython and grant execute permission

~/.pyenv/versions/tech/bin/frameworkpython


#!/bin/bash

# what real Python executable to use
PYVER=2.7
PATHTOPYTHON=/usr/bin/
PYTHON=${PATHTOPYTHON}python${PYVER}

# find the root of the virtualenv, it should be the parent of the dir this script is in
ENV=`$PYTHON -c "import os; print os.path.abspath(os.path.join(os.path.dirname(\"$0\"), '..'))"`

# now run Python with the virtualenv set as Python's HOME
export PYTHONHOME=$ENV
exec $PYTHON "$@"
$ chmod +x ~/.pyenv/versions/tech/bin/frameworkpython

In this state

$ frameworkpython -m IPython notebook

When IPython Notebook is started with, ʻimport matplotlib.pyplotruns. Did you replace the python you use with that of system inframeworkpython? So, not limited to this example, if you encounter a problem that seems to be an error due to using Python of virtualenv, you should try running it with framework python` instead.

However,

$ frameworkpython -m jupyter notebook

That doesn't work. I feel uncomfortable, so I want to do something about it.

http://www.wirywolf.com/2016/01/pyplot-in-jupyter-inside-pyenv-on-el-capitan.html With reference to

$ jupyter --paths
config:
    /Users/foo/.jupyter
    /Users/foo/.pyenv/versions/2.7.9/envs/pksha/etc/jupyter
    /usr/local/etc/jupyter
    /etc/jupyter
data:
    /Users/foo/Library/Jupyter
    /Users/foo/.pyenv/versions/2.7.9/envs/pksha/share/jupyter
    /usr/local/share/jupyter
    /usr/share/jupyter
runtime:
    /Users/foo/Library/Jupyter/runtime

Define a new Kernel under / Users / foo / Library / Jupyter, the first ofdata:.

$ mkdir -p ~/Library/Jupyter/kernels/matplotlib
$ vi ~/Library/Jupyter/kernels/matplotlib/kernel.json

~/Library/Jupyter/kernels/matplotlib/kernel.json


{
 "argv": [ "/Users/foo/.pyenv/versions/tech/bin/frameworkpython", "-m", "ipykernel",
          "-f", "{connection_file}"],
 "display_name": "matplotlib",
 "language": "python"
}

The Kernel is now defined with the name matplotlib.

Run jupyter notebook normally and When working on Jupyter Notebook, set Kernel to matplotlib.

Recommended Posts

Build an environment with pyenv, pyenv-virtualenv, jupyter on OS X El Capitan
Build a python environment with pyenv (OS X El Capitan 10.11.3)
R environment construction with Jupyter (formerly IPython notebook) (on OS X El Capitan 10.11.3)
Install matplotlib on OS X El Capitan
Build python environment with pyenv on EC2 (ubuntu)
Installing TensorFlow 0.11.0rc2 on OS X El Capitan (10.11.6)
Build a Python environment with OSX El capitan
I installed Pygame with Python 3.5.1 in the environment of pyenv on OS X
Build a Python development environment on Mac OS X
When import tkinter is not possible on Mac OS X 10.11.3 (El Capitan) + pyenv + Python 3.5.1.
I tried to build an environment for machine learning with Python (Mac OS X)
Using NAOqi 2.4.2 Python SDK on Mac OS X El Capitan
Build a python data analysis environment on Mac (El Capitan)
Build a Python environment with WSL + Pyenv + Jupyter + VS Code
Environment construction with pyenv and pyenv-virtualenv
How to rebuild python environment from pyenv on Mac environment (El Capitan)
Build Jupyter Lab (Python) environment with Docker
Build an LNPP environment on Amazon Linux 2
Build Python environment with Anaconda on Mac
GeoDjango + SQLite environment construction on OS X
Build a python virtual environment with pyenv
Build a WardPress environment on AWS with pulumi
Build a python environment with ansible on centos6
Building an Anaconda environment for Python with pyenv
A story about building an IDE environment with WinPython on an old Windows OS.
[Memo] Build a virtual environment with Pyenv + anaconda
Build a virtual environment with pyenv and venv
Put Python 2.7.x on Mac OSX 10.15.5 with pyenv
Run Qiita API v2 Python wrapper in Python3 environment (Mac OS X 10.11 (El Capitan))
Build an Arch Linux environment on Raspberry Pi
Install LightGBM in an OS X virtualenv environment
Build an OpenCV4 environment on Raspberry Pi using Poetry
Build a Django development environment using pyenv-virtualenv on Mac
Build a machine learning Python environment on Mac OS
Build a python environment for each directory with pyenv-virtualenv
How to install caffe on OS X with macports
[Linux] WSL2 Build an environment for laravel7 with Ubuntu 20.04
Build a Python environment on your Mac using pyenv
Install PyQt5 with homebrew on Mac OS X Marvericks (10.9.2)
Create an OpenAI Gym environment with bash on Windows 10
Build a Python development environment using pyenv on MacOS
Create a Python development environment on OS X Lion
Build a machine learning environment natively on Windows 10 (x64)
Build a numerical calculation environment with pyenv and miniconda3
Build python3.x with pyenv
I learned MNIST with Caffe and tried to draw it (MAC OS X El Capitan)
Steps to install the latest version of OpenCV on OS X El Capitan without Homebrew
I installed Caffe so that I can do deep learning on MAC OS X El Capitan
Build an environment for machine learning using Python on MacOSX
Let's get started with Python ~ Building an environment on Windows 10 ~
Add an extension to build a more comfortable Jupyter environment
Build an Ubuntu python development environment on Google Cloud Platform
Introducing Kaggle's Docker Image on Windows to build an environment
Test Python with Miniconda on OS X and Linux with travis-ci
Build an environment on windows10 where you can try MXNet
Build a comfortable development environment with VSCode x Remote Development x Pipenv
Build Python3 for Windows 10 on ARM with Visual Studio 2019 (x86) on Windows 10 on ARM
Create a Todo app with Django ① Build an environment with Docker
How to install Theano on Mac OS X with homebrew
Anyone can understand how to build an initial environment for Python on Mac September 2016 (pyenv + virutalenv)
Create an environment with virtualenv