Python: is the current directory automatically included in path? -
python 3.4: reading other questions seems if modulename.py
file outside of current directory, if want import must add together path sys.path.insert(0, '/path/to/application/app/folder')
, otherwise import moduelname
statement results in error:
importerror: no module named modulename
does imply python automatically adds other .py files in same directory path? what's going on underneath surface allows import local files without appending python's path? , __init__.py
file under surface?
python adds directory initial script resides first item sys.path
:
as initialized upon programme startup, first item of list, path[0]
, directory containing script used invoke python interpreter. if script directory not available (e.g. if interpreter invoked interactively or if script read standard input), path[0]
empty string, directs python search modules in current directory first. notice script directory inserted before entries inserted result of pythonpath
.
so goes on underneath surface python appends (or rather, prepends) 'local' directory sys.path
for you.
this means directory script lives in first port of phone call when searching module.
__init__.py
has nil this. __init__.py
needed create directory (regular) package; such directory found on python module search path treated module.
python path importerror python-import
No comments:
Post a Comment