LoginSignup
4
0

More than 5 years have passed since last update.

サービス化したJupyter notebookからローカルに構築したtensorflowを使えるようにした

Posted at

やったこと

サービスにしたJupyter Notebookからローカルのtensorflowをインポートできるようにした。
備忘録として残しておく。

経緯

こちらの記事を参考にtensorflow-gpu環境を作ったが、Jupyter Notebook上からimportできなかった。

今回つかうJupyter Notebook色々な理由がありsystemdで以下のようにサービス化されたものである。

jupyter.service
[Unit]
Description=Jupyter Notebook

[Service]
Type=simple
PIDFile=/run/jupyter.pid
ExecStart=/usr/local/bin/jupyter notebook
User=username
Group=usergroup
WorkingDirectory=/fileshare/
Restart=always
RestartSec=10
#KillMode=mixed

[Install]
WantedBy=multi-user.target

これにより起動しているJupyter Notebookで以下のようにtensorflowをimportしようとした。

source.py
import tensorflow as tf

が、以下のエラーが出てimportできなかった

ImportError: libcudnn.so.6: cannot open shared object file: No such file or directory

で、jupyter上からpathを確認してみたところどうもpathが通っていないっぽい。

import os
print(os.environ.get('LD_LIBRARY_PATH')) #-->None

色々調べた結果、serviceから実行すると .bashrc でexportしている環境変数はserviceでは読み込まれないらしい。
そのため、 EnviromentFile を利用して該当サービスに環境変数を無理やり通すことにした。

jupyter.service
[Unit]
Description=Jupyter Notebook

[Service]
Type=simple
PIDFile=/run/jupyter.pid
ExecStart=/usr/local/bin/jupyter notebook
User=username
Group=usergroup
WorkingDirectory=/fileshare/
Restart=always
RestartSec=10
EnviromentFile=/etc/sysconfig/jupyter #追加
#KillMode=mixed

[Install]
WantedBy=multi-user.target

EnvironmentFileはこう。

PATH=/your/first/path:/your/second/path
LD_LIBRARY_PATH=/usr/local/cuda/lib64:
CUDA_ROOT=/usr/local/cuda

これでimportできるようになったと思ったら permission denied で起動できてないのでログみて該当ディレクトリを chown した。
そしたら無事Jupyter Notebookが起動し、tensorflowのimportに成功した。

まとめ

serviceの環境変数はEnvironmentFileで通そう。

4
0
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
4
0