Daniel Turon
Daniel Turon

Reputation: 21

cannot import function from file in same directory

I have the following code:

from fitnessFunction import labDistance

The fitnessFunction.py folder is in the same directory, and has this code

def labDistance(self,createdRGB, realRGB):
    createdLAB = color.rgb2lab(createdRGB)
    realLAB = color.rgb2lab(realRGB)

    cLAB = createdLAB.reshape(createdLAB.shape[0]*createdLAB.shape[1],3)
    rLAB = realLAB.reshape(realLAB.shape[0]*realLAB.shape[1],3)

    distance = 0
    for i in range(cLAB.shape[0]):
        deltaX = cLAB[i][0] - rLAB[i][0]
        deltaY = cLAB[i][1] - rLAB[i][1]
        deltaZ = cLAB[i][2] - rLAB[i][2]
        delta = math.sqrt(deltaX**2 + deltaY**2 + deltaZ**2)
        distance = distance + delta
    
    return -1*distance

However I get the following error when importing...

cannot import name 'labDistance' from 'fitness'

Upvotes: 0

Views: 34

Answers (1)

Lolix Dudu
Lolix Dudu

Reputation: 313

You have to change from

from fitnessFunction to from .fitnessFunction in order to specify that you are importing it from a local file

Upvotes: 1

Related Questions