Django和IPython:子目录

Django和IPython:子目录,python,django,ipython,jupyter,Python,Django,Ipython,Jupyter,我喜欢使用IPython(或Jupyter now)笔记本进行Django项目所需的某些计算。Shell_plus为我做这项工作。但随着IPython笔记本电脑数量的增长,Django项目根文件夹变得非常混乱。我创建了一个名为Calculations的子目录,但正如预期的那样,Django并没有加载到这样的笔记本中。有没有办法平滑地附加子目录路径 正如上面评论中所指出的,我们有解决方案。下面是我认为最简单的步骤 Django项目布局 /project /someapp /noteb

我喜欢使用IPython(或Jupyter now)笔记本进行Django项目所需的某些计算。Shell_plus为我做这项工作。但随着IPython笔记本电脑数量的增长,Django项目根文件夹变得非常混乱。我创建了一个名为Calculations的子目录,但正如预期的那样,Django并没有加载到这样的笔记本中。有没有办法平滑地附加子目录路径

正如上面评论中所指出的,我们有解决方案。下面是我认为最简单的步骤

  • Django项目布局

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

     NOTEBOOK_ARGUMENTS = [
              '--notebook-dir', 'notebooks',
              ]
    
    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
    
  • 笔记本/ipython_config.py的内容

     NOTEBOOK_ARGUMENTS = [
              '--notebook-dir', 'notebooks',
              ]
    
    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
    
  • 使用shell_plus启动

    python manage.py shell\u plus——笔记本电脑

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

    mkdir-p笔记本/日志


  • 你如何运行你的笔记本?通过
    manage.py shell\u plus--notebook
    然后在浏览器中打开它们,对吗?Alex,是的,我就是这样运行它们的。尝试在配置文件中设置你的
    PYTHONPATH
    :顺便说一句,已解决。