LoginSignup
5
5

More than 5 years have passed since last update.

複数のpythonのバージョンを動作させようとしてつまづいた話(Mac編)

Last updated at Posted at 2017-06-21

以前の記事で紹介した仮想環境を構築する方法で機械学習のライブラリを色々セットアップしてたのですが、色々調べていくうちにpython3系の動作環境で作成されたコードもあり、毎回Python2用に書き換えるのも面倒なので今回はMacにあるデフォルトのPythonの環境を壊すことなくPython3を導入してみた話をしたいと思います。

開発環境

  • MacOS Sierra
  • virtualenv 15.1.0
  • Homebrew 1.2.3

事前準備

  • Homebrewのアップデート
$ brew update
  • virtualenvのインストール
$ pip install virtualenv
  • Python3のインストール
brew install python3

環境構築

$ mkdir -p $(作業ディレクトリ名)
$ cd $(作業ディレクトリ)
$ python3 -m venv venv
$ source venv/bin/activate

試したところ、上から3行目のコマンドを入力したら、以下のエラーが出ました。

Failed to import the site module
Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 544, in <module>
    main()
  File 

(中略)

"/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/collections/__init__.py", line 32, in <module>
    from reprlib import recursive_repr as _recursive_repr
  File "/usr/local/lib/python2.7/site-packages/reprlib/__init__.py", line 7, in <module>
    raise ImportError('This package should not be accessible on Python 3. '
ImportError: This package should not be accessible on Python 3. Either you are trying to run from the python-future src folder or your installation of python-future is corrupted.

調べてみると、 前に設定したPYTHONPATHが邪魔していたようです。

PYTHONPATH=/usr/local/lib/python2.7/site-packages

これが原因でPython3が動作しなかったようです。(参考
というわけで、以下のコマンドでPYTHON_PATHの設定を解除します。

$ unset PYTHONPATH

これで再度試したら、問題なく環境構築ができ、

(venv)$

となりました。
これでPython3を動かすことができます。あとはこの環境で必要になるライブラリを一からインストールしていけば使えます。ちなみに、元の環境に戻したい場合は前回同様、

$ deactive

と入力すれば出来ます。

5
5
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
5