2

When we run a python script/module .py file, then the interpreter looks for any imports in the directory in which the running script is located, and not in the current working directory.

When we run python module using the -m switch then it loads the current working directory into the path.

However, when we run python package using the -m switch then which directory is loaded into the path? The current directory or directory containing the package or the package itself?

Can someone throw light on this concept.

variable
  • 8,262
  • 9
  • 95
  • 215
  • does this help? https://stackoverflow.com/questions/22241420/execution-of-python-code-with-m-option-or-not – mrxra Aug 11 '20 at 05:56

3 Answers3

0

The current directory.

Seems likes the behavior is similar between a module and a package in this regard. The documentation on running with -m:

https://docs.python.org/3/using/cmdline.html#cmdoption-m

... the current directory will be added to the start of sys.path.

bensha
  • 69
  • 3
0

Running a package with -m will add the current directory to the path, not the package's directory or the directory containing the package.

From the -m docs:

As with the -c option, the current directory will be added to the start of sys.path.

This does not depend on whether the specified module is a package or a normal module. (It could even be a namespace package, which does not have a single directory for its contents or a single containing directory.)

Note that the current directory is added to the path before attempting to resolve the name of the package or module to run, which is why it is even possible to use -m to run modules and packages in the current directory.

user2357112
  • 260,549
  • 28
  • 431
  • 505
-1

From my last experience it is from the directory containing the package.

However to check that, run this to see all directories where python would look into:

import sys
print('\n'.join(sys.path))

Tip: If you would like to load custom .py files from other directories, do:

import sys 
sys.path.insert(1, "<path">)

OR

import sys
sys.path.append("<path>")
Jessy Guirado
  • 220
  • 1
  • 7