【问题标题】:jupyter notebook under pyenv in systemd only gives system pythonsystemd中pyenv下的jupyter notebook只提供系统python
【发布时间】:2018-10-18 22:18:32
【问题描述】:

我在 pyenv 别名 jupyter 下安装了 Jupyter 笔记本。如果我从控制台运行它(没有 pyenv 激活 jupyter,笔记本使用相同的端口):

$ export PATH=/home/mortenb/.pyenv/versions/jupyter/bin:$PATH
$ /home/mortenb/.pyenv/versions/jupyter/bin/jupyter-notebook \
  --config=/etc/jupyter/jupyter_notebook_config.py

!python --version
Python 3.6.5

!jupyter --paths
config:
    /home/mortenb/.jupyter
    /home/mortenb/.pyenv/versions/3.6.5/envs/jupyter/etc/jupyter
    /usr/local/etc/jupyter
    /etc/jupyter
data:
    /home/mortenb/.local/share/jupyter
    /home/mortenb/.pyenv/versions/3.6.5/envs/jupyter/share/jupyter
    /usr/local/share/jupyter
    /usr/share/jupyter
runtime:
    /run/user/1000/jupyter

正确的内核一切都很好。

但如果我使用 systemd 脚本、同一用户、同一可执行文件自动执行此操作:

# cat /lib/systemd/system/jupyter_notebook.service 
[Unit]
Description=jupyter notebook as a service

[Service]
PATH=/home/mortenb/.pyenv/versions/jupyter/bin:$PATH
Type=simple
User=mortenb
Group=mortenb
PIDFile=/var/run/jupyter_notebook.pid
ExecStart=/home/mortenb/.pyenv/versions/jupyter/bin/jupyter-notebook --config=/etc/jupyter/jupyter_notebook_config.py 

[Install]
WantedBy=multi-user.target

我得到了 python 2.7.12(系统 python 和 Ubuntu 16.04)

!python --version
Python 2.7.12

!jupyter --paths
config:
    /home/mortenb/.jupyter
    /usr/etc/jupyter
    /usr/local/etc/jupyter
    /etc/jupyter
data:
    /home/mortenb/.local/share/jupyter
    /usr/local/share/jupyter
    /usr/share/jupyter
runtime:
    /home/mortenb/.local/share/jupyter/runtime

jupyter-notebook 在两个示例中都有以下 shebang:

#!/home/mortenb/.pyenv/versions/3.6.5/envs/jupyter/bin/python

知道为什么 systemd 启动不同。 谢谢

【问题讨论】:

    标签: python jupyter-notebook systemd


    【解决方案1】:
    <...>
    
    [Service]
    PATH=/home/mortenb/.pyenv/versions/jupyter/bin:$PATH
    Type=simple
    User=mortenb
    Group=mortenb
    PIDFile=/var/run/jupyter_notebook.pid
    ExecStart=/home/mortenb/.pyenv/versions/jupyter/bin/jupyter-notebook --config=/etc/jupyter/jupyter_notebook_config.py
    
    <...>
    

    您的单元文件在语法上不正确。

    systemd 单元文件中没有PATH= 指令(参考systemd.directives(5))。您必须使用Environment= 指令来指定环境变量。

    而且 systemd 似乎不支持扩展。

    实际上,systemd 确实支持Environment= 和其他几个指令中的环境变量扩展。但是,您必须使用${FOO} 作为单词的一部分; $FOO 仅被识别为它自己的一个词。参考。 systemd.service(5):

    支持基本的环境变量替换。在命令行中使用 "${FOO}" 作为单词的一部分,或者作为它自己的单词,在这种情况下,它将被环境变量的值替换,包括它包含的所有空格,从而产生一个争论。在命令行中使用“$FOO”作为单独的单词,在这种情况下,它将被替换为在空格处拆分的环境变量的值,从而导致零个或多个参数。对于这种类型的扩展,在拆分成单词时会尊重引号,然后将其删除。

    【讨论】:

      【解决方案2】:

      问题是路径,将路径设置为与在 shell 中运行时相同的路径解决了它。而且 systemd 似乎不支持扩展。

      替换 jupyter_notebook.service 中的路径行解决了我的问题:

      Environment='PATH=/home/mortenb/.pyenv/versions/jupyter/bin:/home/mortenb/.pyenv/plugins/pyenv-virtualenv/shims:/home/mortenb/.pyenv/shims:/home/mortenb/.pyenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
      

      【讨论】:

        猜你喜欢
        • 2020-03-15
        • 1970-01-01
        • 1970-01-01
        • 2021-12-20
        • 1970-01-01
        • 1970-01-01
        • 2017-02-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多