【发布时间】:2019-08-07 11:49:27
【问题描述】:
我目前正在尝试将从外部来源下载的 Python 文件导入到我的代码中。这在 Python shell 中运行得非常好,但是当在 IDLE 文件中使用完全相同的代码时,Python 无法在给定目录中找到该文件,即使它确实找到了该目录本身。
我的目标是导入 Python 自然语言工具包,它需要模块“六”。所以我先导入了“六”,然后将 NLTK 导入到 shell 中。
然后我尝试在脚本模式下重复相同的代码。
互动模式
>>> import os
>>> path = "C:/Users/henri/AppData/Local/Programs/Python/Python37-32/lib/site-packages/pip/_vendor/urllib3/packages"
>>> os.chdir(path)
>>> os.getcwd()
结果:
'C:\\Users\\henri\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\pip\\_vendor\\urllib3\\packages' <br/>
>>> import six
>>> dir(six)
结果:
['BytesIO', 'Iterator', 'MAXSIZE', 'Module_six_moves_urllib', 'Module_six_moves_urllib_error', 'Module_six_moves_urllib_parse', 'Module_six_moves_urllib_request', 'Module_six_moves_urllib_response', 'Module_six_moves_urllib_robotparser', 'MovedAttribute', 'MovedModule', 'PY2', 'PY3', 'PY34', 'StringIO', '_LazyDescr', '_LazyModule', '_MovedItems', '_SixMetaPathImporter', '__author__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_add_doc', '_assertCountEqual', '_assertRaisesRegex', '_assertRegex', '_func_closure', '_func_code', '_func_defaults', '_func_globals', '_import_module', '_importer', '_meth_func', '_meth_self', '_moved_attributes', '_urllib_error_moved_attributes', '_urllib_parse_moved_attributes', '_urllib_request_moved_attributes', '_urllib_response_moved_attributes', '_urllib_robotparser_moved_attributes', 'absolute_import', 'add_metaclass', 'add_move', 'advance_iterator', 'assertCountEqual', 'assertRaisesRegex', 'assertRegex', 'b', 'binary_type', 'byte2int', 'callable', 'class_types', 'create_bound_method', 'create_unbound_method', 'exec_', 'functools', 'get_function_closure', 'get_function_code', 'get_function_defaults', 'get_function_globals', 'get_method_function', 'get_method_self', 'get_unbound_function', 'indexbytes', 'int2byte', 'integer_types', 'io', 'iterbytes', 'iteritems', 'iterkeys', 'iterlists', 'itertools', 'itervalues', 'moves', 'next', 'operator', 'print_', 'python_2_unicode_compatible', 'raise_from', 'remove_move', 'reraise', 'string_types', 'sys', 'text_type', 'types', 'u', 'unichr', 'viewitems', 'viewkeys', 'viewvalues', 'with_metaclass', 'wraps']
脚本模式
import os
# importing six
path="C:/Users/henri/AppData/Local/Programs/Python/Python37-32/lib/site-packages/pip/_vendor/urllib3/packages"
os.chdir(path)
os.getcwd()
print(os.getcwd())
import six
dir(six)
这是尝试运行此代码时产生的错误消息:
重新启动:C:\Users\henri\AppData\Local\Programs\Python\Python37-32\importing_nltk_file_2.py
C:\Users\henri\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pip_vendor\urllib3\packages
回溯(最近一次通话最后一次):
文件“C:\Users\henri\AppData\Local\Programs\Python\Python37-32\importing_nltk_file_2.py”,第 13 行,在 进口六
ModuleNotFoundError:没有名为“六”的模块
那么,有没有人知道为什么 Python 找不到“六”,尽管目录是正确的?
【问题讨论】:
-
您的笔记本电脑上是否运行了不同版本的 Python?另外,请确定:您能否确认在您尝试导入
nltk时出现了ModuleNotFoundError? -
请检查您在 IDE 中使用的 python 版本是否与您在 shell 中使用的相同?
-
您可以考虑通过 sys.path.append(path) 添加模块的位置。
-
如果您使用 pip 并运行 python 3.x,请检查以确保您的 python 版本相同,您可能需要运行 pip3 并且它可能工作......如果它仍然无法工作pip卸载六然后重新安装
标签: python python-import filepath importerror