バイオ系博士の備忘録

プログラミング関係の備忘録

pipenvのパッケージがバージョン指定で入らず苦労してる話。

macでpipenvを使いたい

仮想環境とかよくわかんないけど、pipenvは楽そう。
今までの仮想環境は削除する(Qiitaの記事を参考にした)。
いい感じに仮想環境構築して、「Python 機械学習プログラミング 達人データサイエンティストによる理論と実践 」を読み進めたい。

[問題点] pipenvでバージョン指定できない

パッケージの依存関係の問題なのか、最新版のパッケージを入れることはできても、ダウングレードして入れることができていない。
早急に解決したいが、取り急ぎの備忘録として公開する。

[追記]下記オプションで(一部)解決した。

$ pipenv install --sequential
インストール

基本的に、公式ドキュメントのこことかこことかを参考にする。

$ cd myproject
$ brew install pipenv
python3を使う
$ pipenv --python 3
Warning: Python 3 was not found on your system…
You can specify specific versions of Python with:
  $ pipenv --python path/to/python
$ brew ls python3
...
/usr/local/Cellar/python/3.7.3/bin/python3.7
...
$ pipenv --python /usr/local/Cellar/python/3.7.3/bin/python3.7
Removing existing virtualenv…
Creating a virtualenv for this project…
...
✔ Successfully created virtual environment! 
Virtualenv location: /Users/myname/.local/share/virtualenvs/python-machine-learning-book-2nd-edition-eAx8UWz8
仮想環境構築とパッケージのインストール

パッケージのバージョンを指定するとPipfile.lockでエラーがでたので、取り急ぎ最新バージョンで(190402)。

$ pipenv install NumPy SciPy scikit-learn matplotlib pandas
Creating a virtualenv for this project…
Pipfile: /Users/myname/python-machine-learning-book-2nd-edition/Pipfile
Using /usr/local/Cellar/pipenv/2018.11.26_2/libexec/bin/python3.7 (3.7.3) to create virtualenv…
⠸ Creating virtual environment...Already using interpreter /usr/local/Cellar/pipenv/2018.11.26_2/libexec/bin/python3.7
Using real prefix '/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7'
New python executable in /Users/myname/.local/share/virtualenvs/python-machine-learning-book-2nd-edition-eAx8UWz8/bin/python3.7
Not overwriting existing python script /Users/myname/.local/share/virtualenvs/python-machine-learning-book-2nd-edition-eAx8UWz8/bin/python (you must use /Users/myname/.local/share/virtualenvs/python-machine-learning-book-2nd-edition-eAx8UWz8/bin/python3.7)
Installing setuptools, pip, wheel...
done.

✔ Successfully created virtual environment! 
Virtualenv location: /Users/myname/.local/share/virtualenvs/python-machine-learning-book-2nd-edition-eAx8UWz8
Creating a Pipfile for this project…
Installing NumPy…
Adding NumPy to Pipfile's [packages]…
✔ Installation Succeeded 
Installing SciPy…
Adding SciPy to Pipfile's [packages]…
✔ Installation Succeeded 
Installing scikit-learn…
Adding scikit-learn to Pipfile's [packages]…
✔ Installation Succeeded 
Installing matplotlib…
Adding matplotlib to Pipfile's [packages]…
✔ Installation Succeeded 
Installing pandas…
Adding pandas to Pipfile's [packages]…
✔ Installation Succeeded 
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
✔ Success! 
Updated Pipfile.lock (46c561)!
Installing dependencies from Pipfile.lock (46c561)…
  🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 11/11 — 00:00:02
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
仮想環境に入る
$ pipenv shell
Launching subshell in virtual environment…
bash-3.2$  . /Users/myname/.local/share/virtualenvs/python-machine-learning-book-2nd-edition-eAx8UWz8/bin/activate
(python-machine-learning-book-2nd-edition) bash-3.2$
(python-machine-learning-book-2nd-edition) bash-3.2$ python --version
Python 3.7.3
Pythonのシェルに入る
>>> import numpy as np
>>> print(np.arange(10))
[0 1 2 3 4 5 6 7 8 9]
パッケージのバージョンを指定する
$ pipenv run pip freeze
cycler==0.10.0
kiwisolver==1.0.1
matplotlib==3.0.3
numpy==1.16.2
pandas==0.24.2
pyparsing==2.3.1
python-dateutil==2.8.0
pytz==2018.9
scikit-learn==0.20.3
scipy==1.2.1
six==1.12.0

実際に欲しいのは下記だが、エラーがでる(解決済み)。
NumPy==1.12.1
SciPy==0.19.0
scikit-learn==0.18.1
matplotlib==2.0.2
pandas==0.20.1

ためしにpandasだけ入れようとしても、

$ pipenv install pandas==0.20.1
Installing pandas==0.20.1…
Adding pandas to Pipfile's [packages]…
✔ Installation Succeeded 
Pipfile.lock (a71011) out of date, updating to (46c561)…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
✔ Success! 
Updated Pipfile.lock (a71011)!
Installing dependencies from Pipfile.lock (a71011)…
An error occurred while installing
...
Installing initially failed dependencies…
...
ERROR: ERROR: Package installation failed...
  ☤  ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/1 — 00:00:05

入らない。

pipenv.exceptions.InstallError

これをどうにかしないといけないようなので、引き続き調べる。


[190403追記] pipenv install --sequentialする

依存関係のエラーらしい。
一部は--sequential optionで一つずつ入れられた。

$ pipenv --rm
Removing virtualenv (/Users/myname/.local/share/virtualenvs/python-machine-learning-book-2nd-edition-eAx8UWz8)…
python-machine-learning-book-2nd-edition $ rm Pipfile Pipfile.lock 
$ pipenv install --sequential SciPy==0.19.0
Installing SciPy==0.19.0…
Adding SciPy to Pipfile's [packages]…
✔ Installation Succeeded 
Pipfile.lock (0c5e59) out of date, updating to (91cb02)…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
✔ Success! 
Updated Pipfile.lock (0c5e59)!
Installing dependencies from Pipfile.lock (0c5e59)…
  🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 3/3 — 00:00:11
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.

ただし、入れられないものもあり、それらはエラーがでる。

$ pipenv install --sequential matplotlib==2.0.2
Installing matplotlib==2.0.2…
Adding matplotlib to Pipfile's [packages]…
✔ Installation Succeeded 
Pipfile.lock (a671fc) out of date, updating to (ed87c2)…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
✘ Locking Failed! 
...

ただし、pipenv run pip freezeでは入っておらず、実際にimportもできない。

[190508追記] numpy errorだった

numpy1.12はpython3.7に未対応らしく、pyenvでpythonのバージョンも管理すれば解決できそう。
この記事を参考にpyenvでpython3.5.7を入れておく。

$ pipenv --python 3.5.7

続いてjupyterにkernelを追加する。
仮想環境に入り

(test) $ pipenv shell
(test) bash-3.2$ ipython3 kernel install --user --name=test --display-name=test
(test) bash-3.2$  jupyter kernelspec list
  test       /Users/myname/Library/Jupyter/kernels/test
  python3    /usr/local/share/jupyter/kernels/python3


以後はこの仮想環境がjupyter kernelに追加されるので、外からjupyter notebookを開いてもOK。