Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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 3.x 访问jupyter笔记本不同文件夹中python文件中定义的myclass_Python 3.x_Jupyter Notebook - Fatal编程技术网

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新手,尝试以高效的方式编写代码。我犯了多大的错误。请提供输入

您粘贴的内容在我看来还行,您的代码对我来说没有错误

  • 也许是在代码之前或之后的某个东西破坏了类定义
  • 也许您正在Jupyter或IPython中运行此功能,并且您以前导入了
    ImageRotationUtils
    ,但随后更改了代码,并尝试再次重新导入--新的重新导入出现问题,因此您的定义没有被改写?如果是这种情况,请重新启动环境(或Jupyter中的内核)并重新运行代码
  • 我建议将一个简单的初始化代码(如构造函数行)放入同一个源文件中,并将其作为单独的进程执行,以测试这是代码还是环境问题

  • 为了方便起见并避免在代码中调整
    sys.path
    ,我建议在加载环境之前将python目录添加到
    PYTHONPATH
    环境变量中,这样您就可以直接导入了。

    @t6989b您是对的,我在导入后重新导入时做了更改,我遇到了这个问题。正如您所建议的,我在jupyter中重新启动了内核,然后它就工作了。谢谢你的意见。