如何使用jupyter设置python路径
我的项目结构如下如何使用jupyter设置python路径,python,package,jupyter-notebook,ipython,Python,Package,Jupyter Notebook,Ipython,我的项目结构如下 project_name/ project_name/ __init__.py sub_package1/ __init__.py some_module1.py sub_package2/ __init__.py some_module2.py scripts/ some_script.py n
project_name/
project_name/
__init__.py
sub_package1/
__init__.py
some_module1.py
sub_package2/
__init__.py
some_module2.py
scripts/
some_script.py
notebooks/
some_notebook.py
top level files <requirements.txt, .gitignore, README.md, ...>
实现这一目标的最佳方法是什么?我提出了一些想法: 1) 在“我的笔记本”文件夹中,创建一个包含内容的文件context.py
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
然后,在我的笔记本上,我可以做到
from context import project_name
从那里开始工作。这还可以,但当然,如果能够直接导入项目名称,而不必在我的笔记本中添加上下文,那就更好了
2)
在笔记本中执行
notebook_dir = os.path.split(os.getcwd())[0]
if notebook_dir not in sys.path:
sys.path.insert(0, notebook_dir)
因为dunder文件不存在于交互式shell中。在这里,我不完全确定如果我在某个地方更改cwd会发生什么,毕竟在我的笔记本的开头有这样的混乱是有点不可取的
3)
将项目名称根文件夹添加到python路径。如果这可以用非硬编码的方式完成,那就太好了。我使用pyenv和pyenv virtualenv
谢谢你的建议,
马特
notebook_dir = os.path.split(os.getcwd())[0]
if notebook_dir not in sys.path:
sys.path.insert(0, notebook_dir)