Search code examples
pythonpython-importfilepathimporterror

How to make Python find a file in script mode?


I'm currently trying to import Python files downloaded from external sources into my code. This has worked perfectly fine within the Python shell, but when using the exact same code within an IDLE file, Python is unable to find the file in the given directory, even though it does find the directory itself.

My aim is to import the Python Natural Language Toolkit, which requires the module 'six'. So I've imported first 'six', then NLTK into a shell. I've then tried to repeat the same code within script mode.

INTERACTIVE MODE

>>> import os 
>>> path = "C:/Users/henri/AppData/Local/Programs/Python/Python37-32/lib/site-packages/pip/_vendor/urllib3/packages" 
>>> os.chdir(path) 
>>> os.getcwd() 

Result:

'C:\\Users\\henri\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\pip\\_vendor\\urllib3\\packages'  <br/>
>>> import six 
>>> dir(six)

Result:

['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']

SCRIPT MODE

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)

And this is the resulting error message when trying to run this code:

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'

So, does anyone have an idea why Python can't find 'six', although the directory is unmistakeably correct?


Solution

  • when you run a python script all import statements are run first and then other parts of your code are executed. So, import six is executed before os.chdir() and hence, the error