Python: is the current directory automatically included in path?
Asked Answered
D

2

32

Python 3.4: From reading some other SO questions it seems that if a moduleName.py file is outside of your current directory, if you want to import it you must add it to the path with sys.path.insert(0, '/path/to/application/app/folder'), otherwise an import moduelName statement results in this error:

ImportError: No module named moduleName

Does this imply that python automatically adds all other .py files in the same directory to the path? What's going on underneath the surface that allows you to import local files without appending the Python's path? And what does an __init__.py file do under the surface?

Depolymerize answered 26/6, 2014 at 16:26 Comment(1)
The local directory of the initial script is included in the path.Wagonage
W
29

Python adds the directory where the initial script resides as first item to sys.path:

As initialized upon program startup, the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter. If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first. Notice that the script directory is inserted before the entries inserted as a result of PYTHONPATH.

So what goes on underneath the surface is that Python appends (or rather, prepends) the 'local' directory to sys.path for you.

This simply means that the directory the script lives in is the first port of call when searching for a module.

__init__.py has nothing to do with all this. __init__.py is needed to make a directory a (regular) package; any such directory that is found on the Python module search path is treated as a module.

Wagonage answered 26/6, 2014 at 16:29 Comment(4)
Do you have any idea why the current directory is only added in those particular cases rather than all the time? I usually want the current directory to be included when I'm running a script and it's often tripped up me and others.Ramonitaramos
@AlexHall it is never intended to be the current working directory. It’s the location of the script that is added. That you often run a script in your current working directory makes it perhaps look like that it’s the current working directory, but that is coincidence, and unfortunate circumstance. You rarely would want the current working directory added because that’d mean that what the script imports is then dependent on where on the filesystem the user is currently working, and that’d lead to hard to debug issues.Wagonage
@AlexHall when you run a script without qualifying path, python filename.py then there is no full path for the script and so Python ends up adding '' to sys.path, which means lookups end up using the current working directory. That’s something you want to avoid if your code uses os.cwd() before running additional imports.Wagonage
@AlexHall now, if you really really want to have the current working directory added anyway then just add it explicitly. sys.path.insert(0, '').Wagonage
A
1

I have faced same problem when running python script from Intellij Idea. There is a script in a

C:\Users\user\IdeaProjects\Meshtastic-python\meshtastic

It uses

from meshtastic import portnums_pb2, channel_pb2, config_pb2

and fails. I have realized that it looks for

C:\Users\user\IdeaProjects\Meshtastic-python\meshtastic\meshtastic

and changed working directory of this script in Run Configuration from

C:\Users\user\IdeaProjects\Meshtastic-python\meshtastic

to

C:\Users\user\IdeaProjects\Meshtastic-python

so it can find this module UNDERNEATH workdir during execution

C:\Users\user\IdeaProjects\Meshtastic-python\meshtastic
Assortment answered 31/8, 2022 at 11:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.