question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

shell_plus notebook settings Import error in subdirectory

See original GitHub issue

I have an issue where when I run python manage.py shell_plus --notebook I get an error where it can’t find my Django settings when I navigate and create a notebook in a subdirectory of the project:

WARNING: Attempting to work in a virtualenv. If you encounter problems, please install IPython inside the virtualenv.
[IPKernelApp] WARNING | Error in loading extension: django_extensions.management.notebook_extension
Check your config files in /home/eric/.ipython/profile_default
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/IPython/core/shellapp.py", line 269, in init_extensions
    self.shell.extension_manager.load_extension(ext)
  File "/usr/local/lib/python2.7/dist-packages/IPython/core/extensions.py", line 86, in load_extension
    if self._call_load_ipython_extension(mod):
  File "/usr/local/lib/python2.7/dist-packages/IPython/core/extensions.py", line 133, in _call_load_ipython_extension
    mod.load_ipython_extension(self.shell)
  File "/usr/local/lib/python2.7/dist-packages/django_extensions/management/notebook_extension.py", line 5, in load_ipython_extension
    style=no_style())
  File "/usr/local/lib/python2.7/dist-packages/django_extensions/management/shells.py", line 102, in import_objects
    setup()
  File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 17, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 48, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 44, in _setup
    self._wrapped = Settings(settings_module)
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 92, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
ImportError: No module named MY_DJANGO_APP.settings.common

It works when I create a python notebook from the root directory of my project, but not from any subdirectory.

Any suggestions?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:12
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

25reactions
seanjhcommented, Jan 18, 2017

I managed to work around this problem, in case it’s useful for anyone else. Basically, I added an ipython_config.py file to my notebook sub-directory, then manually inserted my base project directory into sys.path.

Project layout:

/project
    /someapp
    /notebooks
        ipython_config.py
    settings.py

settings.py

NOTEBOOK_ARGUMENTS = [
    '--notebook-dir', 'notebooks',
]

IPYTHON_ARGUMENTS = [
    '--debug',
]

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

In the logs, you should observe IPython locating that ipython_config.py file in your subdirectory. For example:

[D 13:28:12.612 NotebookApp] Starting kernel: [u'/usr/bin/python', u'-m', u'ipykernel', u'-f', u'MY_USER_HOME/.local/share/jupyter/runtime/kernel-....json', u'--debug', u'--ext', u'django_extensions.management.notebook_extension']
[D 13:28:12.616 NotebookApp] Connecting to: tcp://127.0.0.1:45969
[I 13:28:12.616 NotebookApp] Kernel restarted: f2c2cfa1-680e-4b17-bd95-03e1affb9b77
[D 13:28:12.617 NotebookApp] Connecting to: tcp://127.0.0.1:52312
[IPKernelApp] IPYTHONDIR set to: MY_USER_HOME/.ipython
[IPKernelApp] Using existing profile dir: u'MY_USER_HOME/.ipython/profile_default'
[IPKernelApp] Searching path [u'MY_PROJECT_BASE_PATH/notebooks', u'MY_USER_HOME/.ipython/profile_default', '/usr/local/etc/ipython', '/etc/ipython'] for config files
[IPKernelApp] Attempting to load config file: ipython_config.py
[IPKernelApp] Looking for ipython_config in /etc/ipython
[IPKernelApp] Looking for ipython_config in /usr/local/etc/ipython
[IPKernelApp] Looking for ipython_config in MY_USER_HOME/.ipython/profile_default
[IPKernelApp] Looking for ipython_config in MY_PROJECT_BASE_PATH/notebooks
sys.path BEFORE = ['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/local/lib/python2.7/dist-packages/IPython/extensions', 'MY_USER_HOME/.ipython']
sys.path AFTER = ['', MY_PROJECT_BASE_PATH, '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/local/lib/python2.7/dist-packages/IPython/extensions', 'MY_USER_HOME/.ipython']
[IPKernelApp] Loaded config file: MY_PROJECT_BASE_PATH/notebooks/ipython_config.py
[IPKernelApp] Attempting to load config file: ipython_kernel_config.py
[IPKernelApp] Looking for ipython_kernel_config in /etc/ipython
[IPKernelApp] Looking for ipython_kernel_config in /usr/local/etc/ipython
[IPKernelApp] Looking for ipython_kernel_config in MY_USER_HOME/.ipython/profile_default
[IPKernelApp] Looking for ipython_kernel_config in MY_PROJECT_BASE_PATH/notebooks
...
[IPKernelApp] Starting the kernel at pid: 209
...
[IPKernelApp] Loading IPython extensions...
[IPKernelApp] Loading IPython extension: storemagic
[IPKernelApp] Loading IPython extension: django_extensions.management.notebook_extension
[IPKernelApp] 
*** MESSAGE TYPE:kernel_info_request***
[IPKernelApp]    Content: {}
   --->
...
1reaction
jonathanstrongcommented, Oct 4, 2016

+1 would love a solution to this problem - i have like 150 notebooks in the root dir of my project, it’s getting out of hand

Read more comments on GitHub >

github_iconTop Results From Across the Web

shell_plus notebook settings Import error in subdirectory
I have an issue where when I run python manage.py shell_plus --notebook I get an error where it can't find my Django settings...
Read more >
Changing notebook directory leads to an error in loading ...
I'm running an IPython notebook through Django's shell_plus with the following command: sudo ./manage.py shell_plus --notebook.
Read more >
zzodosa life: shell_plus notebook settings Import error in ...
shell_plus notebook settings Import error in subdirectory · Issue #865 · django-extensions/django-extensions · Project layout: · settings.py.
Read more >
shell_plus — django-extensions 3.2.1 documentation
The Django settings module and database models are auto-loaded into the interactive shell's global namespace also for IPython Notebook. Auto-loading is done by ......
Read more >
find subdirectory linux Code Example - Code Grepper
resources/js/bootstrap.js 8:15-31 Module not found: Error: Can't resolve 'axios' · git config --system core.longpaths true · filename too long git ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found