pseudomoon.jp - Python
2024-03-28T15:55:27+09:00
Masayuki Takagiwa
urn:md5:d6dd39e3819f71547ceb10b0c58a51f0
Dotclear
Jupyter notebook で connected が点滅して処理が進まない
urn:md5:1afa5b6ce1e50a6cc70786659bfcab51
2020-03-16T19:22:00+09:00
2020-12-24T16:42:55+09:00
takagiwa
Python
<p><a href="https://okadahiroshi.hatenablog.com/entry/2018/10/10/142111">自宅で実行している Jupyter Notebook を外からアクセスしたかったので、リバースプロキシを設定しました</a> こちらの下の方、</p>
<ul>
<li>proxy_http_version 1.1;</li>
</ul>
<p>を同じように設定したら動くようになった。</p>
<p>ついでに ssl_stapling の設定も合わせてみた。</p>
https://www.pseudomoon.jp/dotclear/index.php?post/2020/03/16/Jupyter-notebook-%E3%81%A7-connected-%E3%81%8C%E7%82%B9%E6%BB%85%E3%81%97%E3%81%A6%E5%87%A6%E7%90%86%E3%81%8C%E9%80%B2%E3%81%BE%E3%81%AA%E3%81%84#comment-form
https://www.pseudomoon.jp/dotclear/index.php?feed/atom/comments/116
pip のエラー
urn:md5:940adc2582beb4f5b4535b182bd8b4e6
2017-06-15T18:36:00+09:00
2017-06-15T18:36:00+09:00
takagiwa
Python
<p>pip でインストールしたもののアップデートをかけたら、pip 自身がエラーを吐くようになった。</p> <p>症状は <a href="https://github.com/pypa/pip/issues/4216#issuecomment-282262502">こちら</a> の状況と同じで、pip で何もできなくなる。</p>
<p>環境は、Python 3.5.3、pip は 9.0.1 、OS は FreeBSD 10.3 。</p>
<p>対処方法として <a href="https://github.com/pypa/pip/issues/4216#issuecomment-294962353">こんな</a> のがでていたけど、そもそもその pip が動かないんだよ!</p>
<p>最終的に <a href="https://github.com/pypa/pip/issues/4216#issuecomment-296063821">こんな</a> かたちで、git から pip のソース持ってきて setup.py install 。</p>
https://www.pseudomoon.jp/dotclear/index.php?post/2017/06/15/pip-%E3%81%AE%E3%82%A8%E3%83%A9%E3%83%BC#comment-form
https://www.pseudomoon.jp/dotclear/index.php?feed/atom/comments/77
FreeBSD と Python3 で Jupyter notebook を動かした話
urn:md5:6d6d2c6234e03c7767c19ac520a9e23a
2017-02-02T22:04:00+09:00
2017-02-03T08:55:16+09:00
takagiwa
Python
FreeBSD
IPython
Jupyter
Python
Python3
<p>pkg や ports で Python3 をインストールしようとしても、周りのライブラリなどが複数のバージョンの Python2 をインストールしようとするので、観念して pkg や ports を使わずにセットアップした話。</p> <p>ターゲットはもちろん、「ゼロから作るDeep Learning」</p>
<p>参考にしたサイトはこちら</p>
<ul>
<li><a href="http://www.mianchen.com/running-my-own-jupyter-notebook-server-in-a-freebsd-jail/">Running My Own Jupyter Notebook Server in a FreeBSD Jail</a></li>
<li><a href="http://ja.akionux.net/wiki/index.php/Jupyter_notebook%E3%82%92Apache2%E3%81%AB%E3%82%88%E3%82%8B%E3%83%97%E3%83%AD%E3%82%AD%E3%82%B7%E7%B5%8C%E7%94%B1%E3%81%AE%E3%83%AA%E3%83%A2%E3%83%BC%E3%83%88%E3%82%B5%E3%83%BC%E3%83%90%E3%81%A7%E5%AE%9F%E8%A1%8C%E3%81%99%E3%82%8B">Jupyter notebookをApache2によるプロキシ経由のリモートサーバで実行する</a></li>
</ul>
<pre></pre>
<p>方針は、</p>
<ul>
<li>Jail 環境にインストール</li>
<li>Python はソースから</li>
<li>Python のライブラリは pip で。</li>
<li>Python の外のライブラリなどは pkg で。
<ul>
<li>pkg install -n で、あらかじめ Python2 を使っていないかチェック</li>
</ul></li>
<li>Apache 2.4 でプロキシ</li>
</ul>
<p>あらかじめホームディレクトリに install ディレクトリを作り、そこに Python のソースコードのアーカイブをコピー済み。</p>
<pre>
>pkg install curl sqlite3 blas atlas lapack png freetype2 pkgconf fontconfig
>cd install
>tar zvxf Python-3.5.3.tgz
>cd Python-3.5.3
>./configure
>make
>make install
>rehash
>python3 --version
Python 3.5.3
>make clean
>cd ~
>pip3 install pyzmq
>rehash
>pip3 install jupyter
>rehash
</pre>
<p>実際は後からやったけど、あらかじめパスワードを生成しておく。</p>
<pre>
>python3
>>> from notebook.auth import passwd; passwd()
Enter password:
Verify password:
'sha1:.........................'
>>> exit();
>mkdir notebook
>jupyter notebook --generate-config
</pre>
<p>これで jupyter のコンフィグレーションファイルが /root/.jupyter/jupyter_notebook_config.py に出力される。<br />
これを編集。</p>
<pre>
c.NotebookApp.ip ='*'
c.NotebookApp.open_browser = False
c.NotebookApp.notebook_dir = '/notebook' #default start up directory
c.NotebookApp.port = 8888
</pre>
<p>起動してみる。</p>
<pre>
>jupyter notebook
</pre>
<p>他の PC のブラウザから、この jail の IP アドレスとポート番号 8888 にアクセス。<br />
さっき指定したパスワードでログイン&ログアウト。<br />
jail で Ctrl+C で終了。</p>
<pre>
>pip3 install numpy
</pre>
<p>pkg でインストールしたパッケージの依存関係で gcc がインストールされていて、numpy を呼び出すプログラムがみんなライブラリを呼ばないといけないらしい。<br /></p>
<pre>
>setenv LD_LIBRARY_PATH /usr/local/lib/gcc49
</pre>
<p>を実行しておく。</p>
<pre>
>pip3 install scipy
>rehash
>pip3 install pandas
>rehash
</pre>
<p>matplotlib は git でとってきたいけれど、pkg では Python2 がインストールされてしまう。<br />
jail の親 OS に git をインストールして、そこから jail の中のディレクトリに入って、そこでとる。<br />
/usr/jail/***/root/install で git を実行。</p>
<pre>
>cd ~/install/matplotlib
>python3 setup.py install
>rehash
>jupyter notebook
</pre>
<p>これで jupyter で、import numpy as np など実行して問題なければOK。<br />
jupyter は Ctrl+C で終了させる。</p>
<p>次は Apache 。</p>
<pre>
>pkg install -n apache24
</pre>
<p>Python Image Library (PIL) が欲しくなったので、これもインストールしてみる。</p>
<pre>
>pkg install jpeg tiff lcms webp openjpeg pngquant
>pip3 install pillow
</pre>
<p>ただ、PIL で画像を表示してくれなかったのはまだ分からない。</p>
<hr />
<p>以下は設定ファイルの変更箇所。</p>
<pre>
>diff jupyter_notebook_config.py jupyter_notebook_config.py.org
68d67
< c.NotebookApp.base_url = '/notebook'
160d158
< c.NotebookApp.ip = '*'
198d195
< c.NotebookApp.notebook_dir = '/root/notebook'
205d201
< c.NotebookApp.open_browser = False
215d210
< c.NotebookApp.password = ''
</pre>
<p>jupyter</p>
<ul>
<li>base_url は、サブディレクトリでの運用の実験のために、/notebook に変更</li>
<li>ip は本当は localhost のままで行きたかったけれど、エラーになったので保留。多分 Jail 関係の気がする。</li>
</ul>
<pre>
>diff httpd.conf httpd.conf.sample
126c126
< LoadModule proxy_module libexec/apache24/mod_proxy.so
---
> #LoadModule proxy_module libexec/apache24/mod_proxy.so
129c129
< LoadModule proxy_http_module libexec/apache24/mod_proxy_http.so
---
> #LoadModule proxy_http_module libexec/apache24/mod_proxy_http.so
133c133
< LoadModule proxy_wstunnel_module libexec/apache24/mod_proxy_wstunnel.so
---
> #LoadModule proxy_wstunnel_module libexec/apache24/mod_proxy_wstunnel.so
505c505
< Include etc/apache24/extra/httpd-vhosts.conf
---
> #Include etc/apache24/extra/httpd-vhosts.conf
</pre>
<p>httpd.conf</p>
<ul>
<li>proxy 系を有効にする。</li>
<li>vhosts 運用を見越して、あとの設定は extra/httpd-vhosts.conf に。</li>
</ul>
<pre>
>cat extra/httpd-vhosts.conf
# Virtual Hosts
#
# Required modules: mod_log_config
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
# <URL:http://httpd.apache.org/docs/2.4/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost *:80>
ServerName jupyter.example.jp
ProxyRequests Off
ProxyPreserveHost On
DocumentRoot /notebook/
<Location /notebook/>
RequestHeader unset Accept-Encoding
ProxyPass http://localhost:8888/notebook/
ProxyPassReverse http://localhost:8888/notebook/
ProxyPreserveHost on
Require all granted
</Location>
<Location /notebook/api/kernels/>
ProxyPass ws://localhost:8888/notebook/api/kernels/
ProxyPassReverse ws://localhost:8888/notebook/api/kernels/
</Location>
ErrorLog /var/log/httpd/jupyternb.example.com-error_log
CustomLog /var/log/httpd/jupyternb.example.com-access_log common
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
</VirtualHost>
</pre>
<p>httpd-vhosts.conf</p>
<ul>
<li>あらかじめログ用ディレクトリ /var/log/httpd/ を作っておくか、ログファイルのパスを変える。</li>
<li>DocumentRoot 、Location 、ProxyPass 、ProxyPassReverse は、jupyter_notebook_config.py の base_url にあわせる。</li>
<li>/api/kernels/ はよく分かってないのでそのまま。</li>
</ul>
<hr />
<p>自動起動がしたくてファイルを作ってみたけど、自動では動いてくれなかった。</p>
<pre>
/usr/local/etc/rc.d >cat jupyter
#!/bin/sh
LD_LIBRARY_PATH=/usr/local/lib/gcc49 nohup /usr/local/bin/jupyter notebook --config=/root/.jupyter/jupyter_notebook_config.py > /dev/null 2>&1 &
</pre>
<p>これでは異常もログにのこらないから酷いものだけれど。</p>
<hr />
<p>Numpy のインストールは悩まされた。</p>
<pre>
building 'matplotlib.ft2font' extension
Traceback (most recent call last):
File "setup.py", line 305, in <module>
**extra_args
File "/usr/local/lib/python3.5/distutils/core.py", line 148, in setup
dist.run_commands()
File "/usr/local/lib/python3.5/distutils/dist.py", line 955, in run_commands
self.run_command(cmd)
File "/usr/local/lib/python3.5/distutils/dist.py", line 974, in run_command
cmd_obj.run()
File "/usr/local/lib/python3.5/distutils/command/build.py", line 135, in run
self.run_command(cmd_name)
File "/usr/local/lib/python3.5/distutils/cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "/usr/local/lib/python3.5/distutils/dist.py", line 974, in run_command
cmd_obj.run()
File "setup.py", line 147, in run
return BuildExtCommand.run(self)
File "/usr/local/lib/python3.5/site-packages/setuptools/command/build_ext.py", line 75, in run
_build_ext.run(self)
File "/usr/local/lib/python3.5/distutils/command/build_ext.py", line 339, in run
self.build_extensions()
File "/usr/local/lib/python3.5/distutils/command/build_ext.py", line 448, in build_extensions
self._build_extensions_serial()
File "/usr/local/lib/python3.5/distutils/command/build_ext.py", line 473, in _build_extensions_serial
self.build_extension(ext)
File "/usr/local/lib/python3.5/site-packages/setuptools/command/build_ext.py", line 196, in build_extension
_build_ext.build_extension(self, ext)
File "/usr/local/lib/python3.5/distutils/command/build_ext.py", line 530, in build_extension
include_dirs=ext.include_dirs,
File "/root/install/matplotlib/setupext.py", line 924, in __get__
result = obj._hooks[self._name]() + result
File "/root/install/matplotlib/setupext.py", line 944, in include_dirs_hook
import numpy
File "/usr/local/lib/python3.5/site-packages/numpy/__init__.py", line 142, in <module>
from . import add_newdocs
File "/usr/local/lib/python3.5/site-packages/numpy/add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "/usr/local/lib/python3.5/site-packages/numpy/lib/__init__.py", line 18, in <module>
from .polynomial import *
File "/usr/local/lib/python3.5/site-packages/numpy/lib/polynomial.py", line 13, in <module>
import numpy.core.numeric as NX
AttributeError: module 'numpy' has no attribute 'core'
</pre>
<p>これは</p>
<pre>
>setenv LD_LIBRARY_PATH /usr/local/lib/gcc49
</pre>
<p>を実行していないだけだった。</p>
https://www.pseudomoon.jp/dotclear/index.php?post/2017/02/02/FreeBSD-%E3%81%A8-Python3-%E3%81%A7-Jupyter-notebook-%E3%82%92%E5%8B%95%E3%81%8B%E3%81%97%E3%81%9F%E8%A9%B1#comment-form
https://www.pseudomoon.jp/dotclear/index.php?feed/atom/comments/75
IPython Notebook
urn:md5:df102c64b9928d46c40baf6036493df3
2015-06-18T20:57:00+09:00
2015-06-18T20:57:00+09:00
takagiwa
Python
FreeBSD
IPython
Python
<p>面白そうだったのでセットアップしてみた。</p>
<p>FreeBSD なら</p>
<pre>
# pkg install devel/ipython
</pre>
<p>で一発。<br />
ローカルで動かすなら</p>
<pre>
$ ipython notebook
</pre>
<p>でいいけれど、サーバーで動かしたいとき。</p>
<pre>
# ipython notebook --no-browser --port=80 --ip=xxx.xxx.xxx.xxx
</pre>
<p>ポート 80 はルート権限でないと使わせてくれない?あと Listen するアドレスの指定が必要だった。<br />
さすがにこのまま外には公開できないのでもう少しセットアップ。</p>
<p>必要なことは全て</p>
<pre>
$ ipython help notebook --help-all
</pre>
<p>ここから。</p>
<p>とりあえず TLS 対応してみたい。ひとまず勝手証明書で。ディレクトリは適当。</p>
<pre>
# mkdir /usr/local/etc/cert
# cd /usr/local/etc/cert/
root@freebsd:/usr/local/etc/cert # openssl genrsa -des3 -out server.key 1024
Generating RSA private key, 1024 bit long modulus
......++++++
...........................................................++++++
e is 65537 (0x10001)
Enter pass phrase for server.key:
Verifying - Enter pass phrase for server.key:
root@freebsd:/usr/local/etc/cert # openssl req -new -days 365 -key server.key -out server.csr
Enter pass phrase for server.key:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]: **
State or Province Name (full name) [Some-State]: *****
Locality Name (eg, city) []:*****
Organization Name (eg, company) [Internet Widgits Pty Ltd]:*****
Organizational Unit Name (eg, section) []:*****
Common Name (e.g. server FQDN or YOUR name) []:*****.example.jp
Email Address []:*****@example.jp
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
root@freebsd:/usr/local/etc/cert #
root@freebsd:/usr/local/etc/cert # openssl x509 -req -signkey server.key -days 365 -in server.csr -out server.crt
Signature ok
subject=/C=**/ST=*****/L=*****/O=*****/OU=*****/CN=*****.example.jp/emailAddress=*****@example.jp
Getting Private key
Enter pass phrase for server.key:
root@freebsd:/usr/local/etc/cert # mv server.key server.key.bak
root@freebsd:/usr/local/etc/cert # openssl rsa -in server.key.bak -out server.key
Enter pass phrase for server.key.bak:
writing RSA key
root@freebsd:/usr/local/etc/cert # ls
server.crt server.csr server.key server.key.bak
</pre>
<p>(この鍵の作り方は古いかもしれない)<br />
として関連ファイルを作ったあとで、</p>
<pre>
# ipython notebook --no-browser --port=443 --ip=***.example.jp --NotebookApp.certfile=/usr/local/etc/cert/server.crt --NotebookApp.keyfile=/usr/local/etc/cert/server.key
</pre>
<p>とすると https://***.example.jp/ で TLS 接続。</p>
<p>IPython のためのディレクトリも作っておこう。</p>
<pre>
# mkdir /home/ipython
# mkdir /home/ipython/.ipython
# mkdir /home/ipython/notebook
</pre>
<p>あとは、開きっぱなしっていうのもいやなので、パスワードを設定したい。</p>
<pre>
$ ipython
Python 2.7.9 (default, Jun 12 2015, 19:23:08)
Type "copyright", "credits" or "license" for more information.
IPython 3.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: from IPython.lib import passwd; passwd()
Enter password:
Verify password:
Out[1]: 'sha1:****************************************************************'
In [2]: exit
</pre>
<p>というわけで全部設定。ロギングも入れた。</p>
<pre>
# ipython notebook --no-browser --port=443 --ip=*****.example.jp --NotebookApp.certfile=/usr/local/etc/cert/server.crt --NotebookApp.keyfile=/usr/local/etc/cert/server.key --NotebookApp.password='sha1:****************************************************************' --ipython-dir=/home/ipython/.ipython --notebook-dir=/home/ipython/notebook --log-level=WARN
</pre>
<p>ログインのときユーザー名は無いので、共用には向かないみたい。</p>
<p>あとこのままではルート権限で動いているので、起動しっぱなし、というのは怖い。</p>
https://www.pseudomoon.jp/dotclear/index.php?post/2015/06/18/IPython-Notebook#comment-form
https://www.pseudomoon.jp/dotclear/index.php?feed/atom/comments/43