在 Jupyter 中我自己的小模块没有加载,但在 python/bpython 中一切都很好。打字的时候
import sys
print(sys.path)
我的模块的路径不会在 Jupyter 中显示,但在 python/bpython 中它仍然存在。
我在用:
.bashrc 中的 PYTHONPATH 将我的模块、Jupyter 和 bpython 包含在 virtualenv 中。
最相似的问题是这个Cannot import modules in jupyter notebook; wrong sys.path
如何配置 Jupyter 以自动加载我的模块?
sys.path.append('/path/to/your/code')
?如果可行,您可以将此命令添加到 ipython 配置文件。
这是我在 jupyter notebook 中的项目中所做的,
import sys
sys.path.append("../") # go to parent dir
from customFunctions import *
然后,要影响 customFunctions.py
中的更改,
%load_ext autoreload
%autoreload 2
Jupyter 基于 ipython,永久解决方案可能是更改 ipython 配置选项。
创建配置文件
$ ipython profile create
$ ipython locate
/Users/username/.ipython
编辑配置文件
$ cd /Users/username/.ipython
$ vi profile_default/ipython_config.py
以下行允许您将模块路径添加到 sys.path
c.InteractiveShellApp.exec_lines = [
'import sys; sys.path.append("/path/to/your/module")'
]
在 jupyter 启动时,上一行将被执行
在这里您可以找到有关 ipython 配置 https://www.lucypark.kr/blog/2013/02/10/when-python-imports-and-ipython-does-not/ 的更多详细信息
Jupyter 有自己的 PATH 变量 JUPYTER_PATH。
将此行添加到 .bashrc
文件对我有用:
export JUPYTER_PATH=<directory_for_your_module>:$JUPYTER_PATH
JUPYTER_PATH
的路径中导入 python 模块。查看 docs 似乎是 Jupyter“数据”,例如“kernelspecs、nbextensions 或 voila templates”。
假设您的项目具有以下结构,并且您希望在 notebook.ipynb
中进行导入:
/app
/mypackage
mymodule.py
/notebooks
notebook.ipynb
如果您在没有任何 virtualenv 的 docker 容器中运行 Jupyter,则在项目文件夹中创建 Jupyter (ipython) 配置可能会很有用:
/app
/profile_default
ipython_config.py
ipython_config.py
的内容:
c.InteractiveShellApp.exec_lines = [
'import sys; sys.path.append("/app")'
]
打开笔记本并查看:
print(sys.path)
['', '/usr/local/lib/python36.zip', '/usr/local/lib/python3.6', '/usr/local/lib/python3.6/lib-dynload', '/usr /local/lib/python3.6/site-packages', '/usr/local/lib/python3.6/site-packages/IPython/extensions', '/root/.ipython', '/app']
现在您可以在笔记本中进行导入,而无需在单元格中附加任何 sys.path
:
from mypackage.mymodule import myfunc
经过验证的解决方案对我不起作用,因为我的笔记本不在我的 sys.path 中。但是,这有效;
import os,sys
sys.path.insert(1, os.path.join(os.getcwd() , '..'))
我不喜欢sys.path.append("../")
。这实际上将字符串 ../
添加到路径而不是绝对路径。
这会在加载数据文件(如 csv)时导致路径差异。如果我们沿着这条路线走,我们还需要为这些文件路径添加相对路径。
这会导致 __file__ 引用在其中具有相对的 .. 。如果围绕 __file__ 参考编写的逻辑可能会中断。
以下方法将绝对路径添加到 sys.path 并且我没有遇到任何问题:
import sys
import os
sys.path.insert(0, os.path.abspath('..'))
现在一切的工作方式与普通 python 和 jupyter notebook 的工作方式相同。
我们可以检查 sys 路径并使用以下方法进行验证:
# Testing
import sys
for p in sys.path:
print(p)
我们仍然会遇到访问文件及其在我们的模块中使用的路径的问题。这是因为像 open
这样的文件 io 使用基于当前目录的路径。
最好的解决方案是清理我们构造文件路径的方式,以获取来自该特定 python 模块的相对路径。
例如:
def get_transactions():
transaction_path = Path(__file__).parent.parent.parent / 'data_source/some_input.csv'
print(transaction_path)
transactions_df = pd.read_parquet(path=transaction_path) if os.path.isfile(transaction_path) else None
return transactions_df
您可以使用绝对导入:
/root
/app
/config
newfile.py
/source
file.ipynb
# In the file.ipynb importing the newfile.py file
import os
os.chdir(ROOT_PATH)
from app.config import newfile
https://i.stack.imgur.com/oEUGC.png
os.chdir(ROOT_PATH)
,然后才能导入。更新了我的答案,并在笔记本中进行了测试。
append
吗?我建议最好预先添加;例如sys.path.insert(0, "..")
。如果您要在路径前添加“..”,则将首先搜索父目录,这可以说是您应该想要发生的事情。此外,这与 Jupyter notebook 修改路径的方式一致——它将当前 notebook 目录的路径作为路径上的第一项。