简体   繁体   English

即使显式导入 Python 名称“os”也未定义

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

I have a module called imtools.py that contains the following function:我有一个名为imtools.py的模块,其中包含以下函数:

import os 

def get_imlist(path):
    return[os.path.join(path,f) for f in os.listdir(path) if f.endswith('.jpg')]

When I attempt to call the function get_imlist from the console using import imtools and imtools.get_imlist(path) , I receive the following error:当我尝试使用import imtoolsimtools.get_imlist(path)从控制台调用函数get_imlist ,我收到以下错误:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\...\PycharmProjects\first\imtools.py", line 5, in get_imlist
NameError: name 'os' is not defined

I'm new at Python and I must be missing something simple here, but cannot figure this out.我是 Python 新手,我一定在这里遗漏了一些简单的东西,但无法弄清楚。 If I define the function at the console it works fine.如果我在控制台定义函数,它工作正常。 The specific history of this module script is as follows: initially it was written without the import os statement, then after seeing the error above the import os statement was added to the script and it was re-saved.这个模块脚本的具体来历是这样的:最初是没有import os语句写的,后来看到上面的错误,在脚本中添加了import os语句,重新保存。 The same console session was used to run the script before and after saving.保存前后使用相同的控制台会话来运行脚本。

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.根据一些小提示,我猜测您的代码最初没有import os行,但您在源代码中更正了这一点并重新导入了文件。

The problem is that Python caches modules.问题在于 Python 缓存模块。 If you import more than once, each time you get back the same module - it isn't re-read.如果您多次import ,则每次返回同一个模块时 - 不会重新读取。 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) .要在编辑后重新导入imtools.py文件,您必须使用reload(imtools)

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.我也有同样的问题,我也在尝试遵循 Jan Erik Solem 的《使用 Python 编程计算机视觉》一书”[ http://programmingcomputervision.com/] 。我试图在互联网上探索以查看问题,但我没有找到任何有价值的解决方案,但我已经通过自己的努力解决了这个问题。

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:首先,您只需将“imtools.py”放入 Python 安装位置的父文件夹中,例如 C:\\Python,因此将文件放入该目标并键入以下命令:

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:而不是使用 imtools.get_imlist() 键入代码,您只需从代码中删除 imtools,例如:

get_imlist()

This may solve your problem as I had found my solution by the same technique I used.这可能会解决您的问题,因为我已经通过我使用的相同技术找到了我的解决方案。

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

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