在 Django 项目中:
python manage.py shell_plus --ipython
正在按预期加载模型导入,例如,工作得很好。
当试图
python manage.py shell_plus --notebook
Jupyter 将启动。但是当尝试创建一个新的 django shell plus 笔记本时,我会得到以下导入错误:
[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 扩展 3.1.1
- Django 3.1.6
- jupyter 1.0.0
- jupyter 客户端 6.1.12
- jupyter 控制台 6.4.0
- jupyter 核心 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 env 中,未安装所需的模块。因此错误。
现在我的问题是,为什么 ipython(?) 不能在正确的 conda env 中运行?
请注意,运行时python manage.py shell_plus --ipython
按sys.path
预期运行。此外,在启动经典的 python 3 笔记本时,导入也会按预期进行。