简体   繁体   English

在 jupyter-notebook 上导入失败

[英]import fails on jupyter-notebook

NOTE: this is a duplicate of import from local directory on python2 .注意:这是从 python2 上的本地目录导入的副本。

Let me say upfront, sorry as I've read thru the import rules for python, ipython and jupython and can't keep them straight anymore...让我先说一下,对不起,因为我已经通读了 python、ipython 和 jupython 的导入规则,不能再让它们保持直立了......

Environment:环境:

ubuntu linux server with jupyterhub
jupyter home is $HOME/opt
$HOME/opt/mynotebooks                      # contains my .pynb notebooks
$HOME/opt/mynotebooks/py_lib               # contains .py files
$HOME/opt/mynotebooks/py_lib/app_config.py # myapp config/links to myapp
$HOME/opt/mynotebooks/py_lib/class1.py     # contains .py files
/opt/myapp/src/app                         # contains my app .py classes
/opt/myapp/src/app/appclass.py             # contains appclass

My notebook1.pynb contains the following start-up cell :我的 notebook1.pynb 包含以下启动单元:

import os, sys
MY_NOTEBOOK = os.path.join(os.getenv('HOME'),'opt','mynotebooks')
# add my notebook to ipython path
os.chdir(MY_NOTEBOOK)
sys.path.append(MY_NOTEBOOK) 
import py_lib.app_config
print(os.getenv('MYAPP_STORE')

The above cell works ok.上面的单元格工作正常。

Next, the load py_lib.class1 cell to do work...接下来,加载 py_lib.class1 单元格来做工作……

from py_lib.class1 import myclass1

But the above gives error: "No module named class1".但上面给出了错误:“没有名为 class1 的模块”。

If I move the app_config.py into my notebook dir it works use the following start-up cell...(NOTE import app_config changed without py_lib... I restart kernel and clear all output before re-running).如果我将 app_config.py 移动到我的笔记本目录中,它可以使用以下启动单元... The sys.path is the same when I do the py_lib.class1 import.当我执行 py_lib.class1 导入时,sys.path 是相同的。 Why can't it find it when app_config is 1 level down?为什么当 app_config 低 1 级时找不到它? Makes no sense?没有意义?

import os, sys
MY_NOTEBOOK = os.path.join(os.getenv('HOME'),'opt','mynotebooks')
# add my notebook to ipython path
os.chdir(MY_NOTEBOOK)
sys.path.append(MY_NOTEBOOK) 
import app_config
print(os.getenv('MYAPP_STORE')

(BTW: this latter startup fails if I save notebook to py and run via python.) (顺便说一句:如果我将 notebook 保存到 py 并通过 python 运行,则后一种启动会失败。)

$HOME/opt/mynotebooks/nb_lib/app_config.py $HOME/opt/mynotebooks/nb_lib/app_config.py

APP_SITE = os.path.join('/opt/myapp/src')
APP_STORE = os.path.join('/opt/myapp/store')
os.environ['APP_SITE'] = APP_SITE
os.environ['APP_STORE'] = APP_STORE
# Link to APP_SITE
os.chdir(APP_SITE)
sys.path.append(APP_SITE)  
APP_UPLOAD = os.path.join(APP_STORE,'upload')

I found the issue to be a duplicate of import from local directory .我发现问题是从本地目录导入的重复项。

Specifically, "Python 2.5 for Ubuntu 8.10 does not have the current directory (empty string) in sys.path for the interpreter."具体来说,“适用于 Ubuntu 8.10 的 Python 2.5 在 sys.path 中没有用于解释器的当前目录(空字符串)。”

Unfortunately, my app is still on py2 so I run the notebook on py2.不幸的是,我的应用程序仍在 py2 上,所以我在 py2 上运行笔记本。 I have put a sample of the AppNotebooks .我已经放了一个AppNotebooks的样本。 So fix is to migrate to py3.所以修复是迁移到py3。 Or for app, put config into a sub package (ie. py_lib).或者对于应用程序,将配置放入子包(即 py_lib)中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM