Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从jupyter笔记本调用外部py模块_Python_Jupyter Notebook - Fatal编程技术网

Python 从jupyter笔记本调用外部py模块

Python 从jupyter笔记本调用外部py模块,python,jupyter-notebook,Python,Jupyter Notebook,我有一个这样的项目结构 我想在aNotebook中使用utils.py中的一些函数,因此在我的项目上运行pip install--editable命令,并将以下内容放在笔记本的第一个单元格中 %load_ext autoreload %autoreload 2 我认为这应该允许我这样做 import src.constants from src.utils import write_data write_data(....) 但是我遇到了一个问题,

我有一个这样的项目结构

我想在
aNotebook
中使用
utils.py
中的一些函数,因此在我的项目上运行
pip install--editable
命令,并将以下内容放在笔记本的第一个单元格中

    %load_ext autoreload
    %autoreload 2
我认为这应该允许我这样做

    import src.constants
    from src.utils import write_data

    write_data(....)
但是我遇到了一个问题,在
constants.py
中定义的常量似乎不稳定。换句话说,
utils.py
中依赖于常量的函数在运行时没有定义

此外,为了确保常量可用于
utils.py
中的函数,而不要求用户知道它们是必需的,
utils.py
导入
constants.py

我的问题,是否有一本入门书介绍如何将Jupyter笔记本电脑与普通的旧python模块结合使用,以便两者都可以驻留在同一个项目中?这似乎是一个很容易的工作很多

编辑:添加MVC并出错

***src\utils.py的内容

def test1():
    return 'got to the test program'
我想出来了

import os
import sys
# This adds the project root to the searchpath
sys.path.append(os.path.pardir)

# Then I can use 
from src.utils import test1

正如我正确理解的那样,在您的utils.py中,您从常量导入运行
*
,当您从utils.py导入时,这些常量是未定义的?是的,它们在Jupyter笔记本中未定义,并且它们似乎不粘在代码中。您可以运行一个示例并在帖子中添加回溯错误吗?