【问题标题】:Django and IPython: subdirectoriesDjango 和 IPython:子目录
【发布时间】:2016-01-21 16:46:48
【问题描述】:

我喜欢使用 IPython(或现在的 Jupyter)笔记本来进行我的 Django 项目所需的某些计算。 Shell_plus 为我完成了这项工作。但是随着 IPython 笔记本数量的增长,它在 Django 项目根文件夹中变得非常混乱。我创建了名为 Calculations 的子目录,但正如预期的那样,Django 不会加载到此类笔记本中。有什么方法可以平滑地附加子目录路径?

【问题讨论】:

  • 你如何运行你的笔记本?通过manage.py shell_plus --notebook 然后在浏览器中打开它们,对吗?
  • Alex,是的,我就是这样运行它们的。
  • 尝试在个人资料中设置您的PYTHONPATH:stackoverflow.com/questions/22578980/…
  • 顺便说一句,已解决 here.

标签: python django ipython jupyter


【解决方案1】:

正如上面 cmets 所指出的,this github issue 有解决方案。我已经提炼出我认为的最小步骤。

  1. Django 项目布局

     /project
       /someapp
       /notebooks
         ipython_config.py
       settings.py
    
  2. settings.py的内容

     NOTEBOOK_ARGUMENTS = [
              '--notebook-dir', 'notebooks',
              ]
    
  3. notebooks/ipython_config.py的内容

    import sys
    import os
    
    FILE_PATH = os.path.abspath(os.path.dirname(__file__))
    PROJECT_BASE_PATH = os.path.abspath(os.path.join(FILE_PATH, '..'))
    
    # Allows the kernel to "see" the project during initialization. This
    # FILE_PATH corresponds to Jupyter's "notebook-dir", but we want notebooks to
    # behave as though they resided in the base directory to allow for clean
    # imports.
    print("sys.path BEFORE = {}".format(sys.path))
    sys.path.insert(1, PROJECT_BASE_PATH)
    print("sys.path AFTER = {}".format(sys.path))
    
    # Any additional initialization logic goes here
    
  4. 使用 shell_plus 启动

    python manage.py shell_plus --notebook

  5. 我必须为日志文件创建一个文件夹

    mkdir -p notebooks/logs

【讨论】:

    猜你喜欢
    • 2014-04-01
    • 2012-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-29
    • 2017-03-31
    • 1970-01-01
    相关资源
    最近更新 更多