如何让Python在脚本模式下查找文件?

如何让Python在脚本模式下查找文件?,python,python-import,filepath,importerror,Python,Python Import,Filepath,Importerror,我目前正在尝试将从外部源下载的Python文件导入到我的代码中。这在pythonshell中工作得非常好,但是当在空闲文件中使用完全相同的代码时,Python无法在给定的目录中找到该文件,即使它确实找到了目录本身 我的目标是导入Python自然语言工具包,它需要模块“six”。所以我先导入了“六”,然后将NLTK导入到一个shell中。 然后,我尝试在脚本模式中重复相同的代码 交互模式 >>> import os >>> path = "C:/Users/h

我目前正在尝试将从外部源下载的Python文件导入到我的代码中。这在pythonshell中工作得非常好,但是当在空闲文件中使用完全相同的代码时,Python无法在给定的目录中找到该文件,即使它确实找到了目录本身

我的目标是导入Python自然语言工具包,它需要模块“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/>
['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']
结果:

'C:\\Users\\henri\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\pip\\_vendor\\urllib3\\packages'  <br/>
['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\u nltk\u file\u 2.py
C:\Users\henri\AppData\Local\Programs\Python37-32\lib\site packages\pip\u vendor\urllib3\packages
回溯(最近一次呼叫最后一次):
文件“C:\Users\henri\AppData\Local\Programs\Python37-32\importing\u nltk\u File\u 2.py”,第13行,在 导入六个
ModuleNotFoundError:没有名为“six”的模块


那么,有没有人知道为什么Python找不到“six”,尽管目录绝对正确?

当您运行Python脚本时,所有导入语句都会首先运行,然后执行代码的其他部分。因此,import six是在os.chdir()之前执行的,因此,当您运行python脚本时,会出现错误

,首先运行所有import语句,然后执行代码的其他部分。因此,import six是在os.chdir()之前执行的,因此会出现错误

您的笔记本电脑上是否运行不同版本的Python?另外,可以肯定的是:当您尝试导入<代码> NLTK 时,您可以确认“<代码> ModuleNotFoundError <代码> >吗?请检查您在shell中使用的是相同的Python版本吗?您可以考虑通过Sys.Posi.AppEnter(PATH)添加模块的位置。。如果您使用pip并运行Python3.x,请检查以确保您的python版本相同。您可能需要运行pip3,并且它可能会工作。。。。如果它仍然不能工作,也许pip卸载六然后重新安装。你的笔记本电脑上运行着不同版本的Python吗?另外,可以肯定的是:当您尝试导入<代码> NLTK 时,您可以确认“<代码> ModuleNotFoundError <代码> >吗?请检查您在shell中使用的是相同的Python版本吗?您可以考虑通过Sys.Posi.AppEnter(PATH)添加模块的位置。。如果您使用pip并运行Python3.x,请检查以确保您的python版本相同。您可能需要运行pip3,并且它可能会工作。。。。如果它仍然不起作用,也许pip卸载六然后重新安装它谢谢所有对我的问题有贡献的人!通过添加
sys.path.append(path)
,我已经能够解决这个问题。感谢所有对我的问题做出贡献的人!我已经通过添加
sys.path.append(path)
解决了这个问题。