python-3.x Django:shell_plus --notebook模块导入错误

izj3ouym  于 2023-10-21  发布在  Python
关注(0)|答案(1)|浏览(101)

在Django项目中:

python manage.py shell_plus --ipython

正在按预期加载模型导入,例如,工作正常。
当试图

python manage.py shell_plus --notebook

开始了。但是当尝试创建一个新的django shell加notebook时,我会得到以下导入错误:

[I 12:43:00.851 NotebookApp] Kernel started: 68eac163-4064-41ac-824d-5c839562814c, name: django_extensions
[IPKernelApp] WARNING | Error in loading extension: django_extensions.management.notebook_extension
Check your config files in /home/pumpkin/.ipython/profile_default
Traceback (most recent call last):
  File "/home/pumpkin/miniconda3/envs/django22/lib/python3.7/site-packages/IPython/core/shellapp.py", line 261, in init_extensions
    self.shell.extension_manager.load_extension(ext)
  File "/home/pumpkin/miniconda3/envs/django22/lib/python3.7/site-packages/IPython/core/extensions.py", line 87, in load_extension
    if self._call_load_ipython_extension(mod):
  File "/home/pumpkin/miniconda3/envs/django22/lib/python3.7/site-packages/IPython/core/extensions.py", line 134, in _call_load_ipython_extension
    mod.load_ipython_extension(self.shell)
  File "/home/pumpkin/miniconda3/envs/django22/lib/python3.7/site-packages/django_extensions/management/notebook_extension.py", line 10, in load_ipython_extension
    style=no_style(),
  File "/home/pumpkin/miniconda3/envs/django22/lib/python3.7/site-packages/django_extensions/management/shells.py", line 152, in import_objects
    setup()
  File "/home/pumpkin/miniconda3/envs/django22/lib/python3.7/site-packages/django/__init__.py", line 19, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/home/pumpkin/miniconda3/envs/django22/lib/python3.7/site-packages/django/conf/__init__.py", line 79, in __getattr__
    self._setup(name)
  File "/home/pumpkin/miniconda3/envs/django22/lib/python3.7/site-packages/django/conf/__init__.py", line 66, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/pumpkin/miniconda3/envs/django22/lib/python3.7/site-packages/django/conf/__init__.py", line 157, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/home/pumpkin/miniconda3/envs/django22/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/pumpkin/django_projects/book_management/book_management/__init__.py", line 3, in <module>
    from .celery import app as celery_app
  File "/home/pumpkin/django_projects/book_management/book_management/celery.py", line 9, in <module>
    from celery import Celery
ModuleNotFoundError: No module named 'celery'

我可能错过了一些东西,我很高兴给予更多的细节,并帮助重现的问题。

版本:

  • django-extensions 3.1.1
  • 简体中文
  • jupyter 1.0.0
  • jupyter-client 6.1.12
  • jupyter-console 6.4.0
  • jupyter-core 4.7.1
  • jupyterlab-pygments 0.1.2
  • jupyterlab-widgets 1.0.0

编辑:
我可以把这个问题概括一下。执行时:python manage.py shell_plus --notebook和打印sys.path,我们得到

sys.path=
['/home/pumpkin/django_projects/book_management',
 '/home/pumpkin/miniconda3/envs/booking_j2/lib/python39.zip',
 '/home/pumpkin/miniconda3/envs/booking_j2/lib/python3.9',
 '/home/pumpkin/miniconda3/envs/booking_j2/lib/python3.9/lib-dynload',
 '/home/pumpkin/miniconda3/envs/booking_j2/lib/python3.9/site-packages']

这是预期的(它是当前激活的conda env)
但是当尝试启动一个新的Django shell-plus notebook时,我们可以看到跟踪依赖于另一个conda env:

[I 14:21:31.694 NotebookApp] Creating new notebook in 
[I 14:21:32.373 NotebookApp] Kernel started: 8d4013f7-ae9e-4bf4-80a3-ca9483e72f0b, name: django_extensions
[IPKernelApp] WARNING | Error in loading extension: django_extensions.management.notebook_extension
Check your config files in /home/pumpkin/.ipython/profile_default
Traceback (most recent call last):
  File "/home/pumpkin/miniconda3/envs/django22/lib/python3.7/site-packages/IPython/core/shellapp.py", line 261, in init_extensions
    self.shell.extension_manager.load_extension(ext)

在这个conda环境中,没有安装所需的模块。因此出现了错误。
现在我的问题是,为什么ipython(?)不运行在正确的conda环境?
请注意,当运行python manage.py shell_plus --ipython时,sys.path与预期的一样。同样,当启动一个经典的python 3笔记本时,导入也会按照预期进行。

whhtz7ly

whhtz7ly1#

你应该将内核更改为Django shell Plus或类似的东西,在你的Django应用程序环境中。


欲了解更多信息,请查看此帖子:https://medium.com/ayuth/how-to-use-django-in-jupyter-notebook-561ea2401852

相关问题