Python name 'os' is not defined even though it is explicitly imported

前端 未结 2 1984
花落未央
花落未央 2021-02-13 13:14

I have a module called imtools.py that contains the following function:

import os 

def get_imlist(path):
    return[os.path.join(path,f) for f in o         


        
相关标签:
2条回答
  • 2021-02-13 13:29

    Same problem is with me I am also trying to follow the book of Programming Computer Vision with Python by Jan Erik Solem" [http://programmingcomputervision.com/]. I tried to explore on internet to see the problem but I did not find any valuable solution but I have solved this problem by my own effort.

    First you just need to place the 'imtools.py' into the parent folder of where your Python is installed like C:\Python so place the file into that destination and type the following command:

    from PIL import Image
    from numpy import *
    from imtools import *
    

    Instead of typing the code with imtools.get_imlist() you just to remove the imtools from the code like:

    get_imlist()
    

    This may solve your problem as I had found my solution by the same technique I used.

    0 讨论(0)
  • 2021-02-13 13:45

    Based on small hints, I'm going to guess that your code didn't originally have the import os line in it but you corrected this in the source and re-imported the file.

    The problem is that Python caches modules. If you import more than once, each time you get back the same module - it isn't re-read. The mistake you had when you did the first import will persist.

    To re-import the imtools.py file after editing, you must use reload(imtools).

    0 讨论(0)
提交回复
热议问题