LoginSignup
14
11

More than 5 years have passed since last update.

[Mac/ubuntu]最新Python実行環境構築手順

Last updated at Posted at 2018-02-13

目的

任意のバージョンのpythonを選べるようにし、常に最新のバージョンを利用できるようにする。
実行環境ごとに依存ライブラリが分離された環境を準備する。
システムワイドに環境を汚染せず、ユーザごとに任意のPythonを実行可能とする。

anyenv+anyenv-updateインストール

ndenvツールの導入を容易にしたり、envツールのアップデートを容易にするためにanyenvから入れていく。

$ git clone https://github.com/riywo/anyenv ~/.anyenv

環境設定

Macの場合

$ echo 'export PATH="$HOME/.anyenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(anyenv init -)"' >> ~/.bash_profile
$ exec $SHELL -l
$ git clone https://github.com/znz/anyenv-update.git ~/.anyenv/plugins/anyenv-update

Ubuntuの場合

$ echo 'export PATH="$HOME/.anyenv/bin:$PATH"' >> ~/.profile
$ echo 'eval "$(anyenv init -)"' >> ~/.profile
$ exec $SHELL -l
$ git clone https://github.com/znz/anyenv-update.git ~/.anyenv/plugins/anyenv-update

pyenv+pyenv-virtualenvインストール

開発環境ごとに依存関係を分離するため、pyenv-virtualenvをあわせて導入。

$ anyenv install pyenv
$ exec $SHELL -l
$ git clone https://github.com/yyuu/pyenv-virtualenv ~/.anyenv/envs/pyenv/plugins/pyenv-virtualenv

任意のバージョンのpythonをインストール

ubuntuの場合の関連パッケージ導入

$ sudo apt install build-essential zlib1g-dev libffi-dev libssl-dev libbz2-dev libreadline-dev libsqlite3-dev
$ pyenv install 3.x.x

最近、MojaveにアップグレードしたMacでインストールに失敗する場合

zipimport.ZipImportError: can't decompress data; zlib not available
make: *** [install] Error 1

上記のようなエラーが出てインストールに失敗する場合は下記のコマンドを試す

$ sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /

上記のコマンドを実行しても下記のようなエラーがでる場合

installer: Error - the package path specified was invalid: '/Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg'.

Command Line Tools (macOS 10.14) for Xcode 10パッケージを下記のURLからダウンロードしてくる
https://developer.apple.com/download/more/

Macでopensslが入っているにも関わらず3.7.0以上がインストールできない場合

下記のようなエラーが出る場合

ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib?

下記の対応で、openssl1.1.1を使用

$ brew install 'openssl@1.1'
$ CONFIGURE_OPTS="--with-openssl=$(brew --prefix openssl@1.1)" pyenv install 3.7.0

インストール可能なバージョンの一覧を見たい場合

$ pyenv install --list

最新のバージョンのpythonがインストールできない場合

$ anyenv update

切り離された実行環境をカレントディレクトリに作成

$ pyenv virtualenv 3.x.x my-develop-environment
$ pyenv local my-develop-environment
14
11
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
14
11