Python 3.x 访问jupyter笔记本不同文件夹中python文件中定义的myclass
我有以下文件夹结构Python 3.x 访问jupyter笔记本不同文件夹中python文件中定义的myclass,python-3.x,jupyter-notebook,Python 3.x,Jupyter Notebook,我有以下文件夹结构 | |---notebook | | | --- MyNoteBook.ipnyb | |---python | | | --- image_rotation_utils.py 我在python.image\u rotation\u utils.py中有以下内容 class ImageRotationUtils: """ Utils for rotating
|
|---notebook
| |
| --- MyNoteBook.ipnyb
|
|---python
| |
| --- image_rotation_utils.py
我在python.image\u rotation\u utils.py中有以下内容
class ImageRotationUtils:
""" Utils for rotating images."""
def __init__(self, image_path=None, image_name=None, output_path=None, output_annotations_file=None ):
""" Initializes the class.
Args:
image_path: the relative path to the image
image_name: image file name
output_path: the relative path where rotated output file is stored.
output_annotations_file: annotations file where rotated anotations are stored.
"""
self.image_path = image_path
self.image_name = image_name
self.output_path = output_path
self.output_annotations_file = output_annotations_file
#other functions defined
现在在MyNoteBook.ipnyb中,我有以下单元格
单元1:
import sys
sys.path.insert(0, '..')
from python.image_rotation_utils import *
上面的代码现在在下面的单元格2中成功执行
myUtils = ImageRotationUtils()
我有下面的错误
----> 1 ImageRotationUtils()
TypeError: __init__() missing 4 required positional arguments: 'image_path', 'image_name', 'output_path', and 'output_annotations_file'
当我在构造函数中有默认值时,为什么会出现这个错误?在类定义中没有
我是python新手,尝试以高效的方式编写代码。我犯了多大的错误。请提供输入您粘贴的内容在我看来还行,您的代码对我来说没有错误
ImageRotationUtils
,但随后更改了代码,并尝试再次重新导入--新的重新导入出现问题,因此您的定义没有被改写?如果是这种情况,请重新启动环境(或Jupyter中的内核)并重新运行代码为了方便起见并避免在代码中调整
sys.path
,我建议在加载环境之前将python目录添加到PYTHONPATH
环境变量中,这样您就可以直接导入了。@t6989b您是对的,我在导入后重新导入时做了更改,我遇到了这个问题。正如您所建议的,我在jupyter中重新启动了内核,然后它就工作了。谢谢你的意见。