yusuf
yusuf

Reputation: 3781

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

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.

Upvotes: 2

Views: 6443

Answers (1)

tobylaroni
tobylaroni

Reputation: 863

You can't import the renameImages method that is part of the convertImages class. 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.

If you need to use the renameImages method, you use it as part of the instance you use -- just call it like so:

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.

Upvotes: 5

Related Questions