简体   繁体   English

ImportError:无法在Python中导入名称…

[英]ImportError: cannot import name … in python

I have class as the following: 我有以下课程:

import skimage.io as io
import numpy as np
import scipy.io as sio
import glob, re, os

class convertImages:

    def __init__(self, directory):
        self.directory = directory

    def renameImages(self):
        path = self.directory
        i = 1
        files = [s for s in os.listdir(path) if os.path.isfile(os.path.join(path, s))]
        files.sort(key = lambda s: os.path.getmtime(os.path.join(path, s)))

        for file in files:
            os.rename(path + file, path + str(i) + '.png')
            i = i + 1

I want to call this class from my Main: 我想从我的主班叫这个班:

import convertImages
from convertImages import renameImages

ci = convertImages('Pictures/trialRGB')

But get this damn error: ImportError: cannot import name renameImages 但是会出现该死的错误: ImportError: cannot import name renameImages

I don't know what is the stupid logic behind this. 我不知道这背后的愚蠢逻辑是什么。 I have done everything according to tutorial. 我已经按照教程完成了所有操作。 Please help me to fix this problem. 请帮助我解决此问题。

You can't import the renameImages method that is part of the convertImages class. 您不能导入属于convertImages类的一部分的renameImages方法。 And based on your code, you don't need to. 而且根据您的代码,您不需要。

Just delete the from convertImages import renameImages line, and your code should run without any problems. 只需from convertImages import renameImages行中删除,您的代码就可以正常运行。

If you need to use the renameImages method, you use it as part of the instance you use -- just call it like so: 如果需要使用renameImages方法,则可以将其用作所使用实例的一部分,就像这样调用它:

ci.renameImages()

You need to run that method as part of an instance -- otherwise, it won't work. 您需要将该方法作为实例的一部分运行-否则它将无法正常工作。

UPDATE (from comments): You also need to change import convertImages to from convertImages import convertImages . 更新(来自注释):您还需要将import convertImages更改为from convertImages import convertImages

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM