sagantaf

IT関連の技術記事を書くブログ。

Astropy〜Linux環境にインストールする方法

Astropy をインストールする方法を書いておく。

Anacondaを使っている場合はデフォルトでインストールされている。 以下のコマンドでバージョンを確認するだけで済む。

$ conda list |grep astropy
astropy                   3.0.2            py36h3010b51_1
pytest-astropy            0.3.0                    py36_0

astropyが表示されればOK。

pipを使ってインストールする

方法としては、単純にpipでastropyを指定してインストールするだけ。

$ pip install astropy --no-deps


すでにnumpyなどをインストールしていて、バージョンを変えたくない場合は、--no-depsのオプションを付けて、Astropyだけをインストールする。
ついでにNumpyもインストールしてしまいたい場合は、オプションをつけずに下記のようにインストールする。

$ pip install astropy 
Collecting astropy
  Downloading https://files.pythonhosted.org/packages/9a/dd/379071efa94c223c596594a429ab41b8e1bb7052e40c733842a9f28674c3/astropy-3.0.4-cp36-cp36m-manylinux1_x86_64.whl (6.0MB)
    100% |################################| 6.0MB 222kB/s
Collecting numpy>=1.10.0 (from astropy)
  Downloading https://files.pythonhosted.org/packages/22/02/bae88c4aaea4256d890adbf3f7cf33e59a443f9985cf91cd08a35656676a/numpy-1.15.2-cp36-cp36m-manylinux1_x86_64.whl (13.9MB)
    100% |################################| 13.9MB 117kB/s
Installing collected packages: numpy, astropy
Successfully installed astropy-3.0.4 numpy-1.15.2

numpyも一緒にインストールされていることが分かる。

またAstropyを動かすには以下のライブラリが必須になる。
・python3.5もしくは3.6
・numpy 1.10.0 以降
・pytest 3.1以降

先述の通り、numpyはastropyをインストールする時に合わせてインストールされるが、pytestはされないので、以下のコマンドでインストールする。

$ pip install pytest


他にもscipyとかmatplotlibなどに依存した機能もあるけど、必要になったときにインストールする。

Astropyがインストールされたかどうか確認する

ここで公式ページの通りに実施すると、以下のRuntimeErrorが発生する。

$ python3
Python 3.6.5 (default, Apr  1 2018, 05:46:30)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import astropy
>>> astropy.test()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/hoge/.local/lib/python3.6/site-packages/astropy/utils/decorators.py", line 827, in test
    func = make_function_with_signature(func, name=name, **wrapped_args)
  File "/home/hoge/.local/lib/python3.6/site-packages/astropy/tests/runner.py", line 260, in test
    return runner.run_tests(**kwargs)
  File "/home/hoge/.local/lib/python3.6/site-packages/astropy/tests/runner.py", line 601, in run_tests
    return super(TestRunner, self).run_tests(**kwargs)
  File "/home/hoge/.local/lib/python3.6/site-packages/astropy/tests/runner.py", line 202, in run_tests
    self._has_test_dependencies()  # pragma: no cover
  File "/home/hoge/.local/lib/python3.6/site-packages/astropy/tests/runner.py", line 182, in _has_test_dependencies
    raise RuntimeError(cls._missing_dependancy_error)
RuntimeError: Test dependencies are missing. You should install the 'pytest-astropy' package.


実はpytestじゃなくてastropy-pytestをインストールする必要があったということ。。。
そのため、一旦Ctrl+dでpythonを抜けて、pytest-astropyをインストールする。

pip3 install pytest-astropy


これでテストが実施できる。 再度テストコマンドを実行すると以下のように表示されるはず。(かなり時間がかかる場合がある)

$ python3
Python 3.6.5 (default, Apr  1 2018, 05:46:30)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import astropy as ap
>>> ap.test()
/usr/lib/python3.6/importlib/_bootstrap.py:219: ImportWarning: can't resolve package from __spec__ or __package__, falling back on __name__ and __path__
  return f(*args, **kwds)
========================================= test session starts =========================================
platform linux -- Python 3.6.5, pytest-3.8.1, py-1.6.0, pluggy-0.7.1

(中略)

=============== 10183 passed, 335 skipped, 66 xfailed, 5059 warnings in 446.75 seconds ================
0


test session starts
が表示されれば問題なくテストが開始されており、Astropyが使える状態になったということ。

参考ページ ・Astropy公式ページ https://astropy.readthedocs.io/en/stable/install.html

上記公式ページには、ソースからビルドする方法も書かれている。