如何让Python以脚本方式查找文件?
How to make Python find a file in script mode?
我目前正在尝试将从外部源下载的 Python 文件导入到我的代码中。这在 Python shell 中工作得非常好,但是当在 IDLE 文件中使用完全相同的代码时,Python 无法在给定目录中找到该文件,即使它找到了找到目录本身。
我的目标是导入 Python 自然语言工具包,这需要模块 'six'。所以我首先导入 'six',然后将 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)
这是尝试 运行 此代码时产生的错误消息:
RESTART: 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
Traceback (most recent call last):
File "C:\Users\henri\AppData\Local\Programs\Python\Python37-32\importing_nltk_file_2.py", line 13, in
import six
ModuleNotFoundError: No module named 'six'
所以,有没有人知道为什么 Python 找不到 'six',尽管目录无疑是正确的?
当您 运行 一个 python 脚本时,所有导入语句都是 运行 首先,然后执行代码的其他部分。因此,导入六在 os.chdir() 之前执行,因此,错误
我目前正在尝试将从外部源下载的 Python 文件导入到我的代码中。这在 Python shell 中工作得非常好,但是当在 IDLE 文件中使用完全相同的代码时,Python 无法在给定目录中找到该文件,即使它找到了找到目录本身。
我的目标是导入 Python 自然语言工具包,这需要模块 'six'。所以我首先导入 'six',然后将 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)
这是尝试 运行 此代码时产生的错误消息:
RESTART: 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
Traceback (most recent call last):
File "C:\Users\henri\AppData\Local\Programs\Python\Python37-32\importing_nltk_file_2.py", line 13, in import six
ModuleNotFoundError: No module named 'six'
所以,有没有人知道为什么 Python 找不到 'six',尽管目录无疑是正确的?
当您 运行 一个 python 脚本时,所有导入语句都是 运行 首先,然后执行代码的其他部分。因此,导入六在 os.chdir() 之前执行,因此,错误