【问题标题】:Specific jupyter notebook config file for a virtual environment虚拟环境的特定 jupyter notebook 配置文件
【发布时间】:2021-01-22 19:19:27
【问题描述】:

我在 python3 (with venv) 中创建了一个虚拟环境来使用几个包,包括 jupyter notebook。

运行bash命令jupyter --config-dir我注意到jupyter notebook在虚拟环境中使用的配置文件是我机器的主要配置文件(~/.jupyter/jupyter_notebook_config.py)。

如何创建一个新的配置文件并告诉虚拟环境使用它。目标不是修改主要的,因为我需要它用于其他项目。

【问题讨论】:

    标签: python jupyter-notebook python-venv


    【解决方案1】:

    来自https://jupyter.readthedocs.io/en/latest/use/jupyter-directories.html

    Config files are stored by default in the ~/.jupyter directory.
    
    JUPYTER_CONFIG_DIR
    
        Set this environment variable to use a particular directory, other than the default, for Jupyter config files.
    
    

    所以你需要做的是在激活环境时设置JUPYTER_CONFIG_DIR 环境变量。

    您如何执行此操作取决于您使用的虚拟环境管理器。我相信venv 你应该可以添加

    export JUPYTER_CONFIG_DIR=/your/custom/config/dir
    

    到您激活虚拟环境时运行的activate 脚本(应该在$venv/environmentname/bin/activate 中)。

    您可能更喜欢的另一个选项(这是我选择的路线,因为我使用的是 pyenv+virtualenv,它不使用 activate 脚本),您可以使用其他工具,例如 direnv 或 @ 987654323@按项目设置环境变量。在 direnv 的情况下(这是一个更简单的 IMO),您使用上面的 export 命令在您的项目目录中创建一个 .envrc 文件,然后每次您将 cd 进入您的项目目录时,它都会运行命令在.envrc 文件中。如果您的虚拟环境是按项目组织的,这将非常有效。

    一旦JUPYTER_CONFIG_DIR 环境变量被正确设置,当你运行jupyter notebook --generate-config 时你的环境被激活后,它应该在新目录中创建一个新的配置文件。

    【讨论】:

      猜你喜欢
      • 2018-07-06
      • 2018-04-06
      • 1970-01-01
      • 2020-12-16
      • 1970-01-01
      • 1970-01-01
      • 2017-07-10
      • 2018-08-01
      • 1970-01-01
      相关资源
      最近更新 更多