Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does invoking a module with -m option set sys.path[0] to empty string?

I have a python script foo.py in my current directory C:\test.

Here is the code.

import sys
print('sys.path:', sys.path)
print('sys.argv:', sys.argv)

When I execute it as a script, I see this output.

C:\test>python foo.py
sys.path: ['C:\\test', 'C:\\Windows\\system32\\python34.zip', 'C:\\Python34\\DLLs', 'C:\\Python34\\lib', 'C:\\Python34', 'C:\\Python34\\lib\\site-packages']
sys.argv: ['foo.py']

But when I execute it as a module, I see this output.

C:\test>python -m foo
sys.path: ['', 'C:\\Windows\\system32\\python34.zip', 'C:\\Python34\\DLLs', 'C:\\Python34\\lib', 'C:\\Python34', 'C:\\Python34\\lib\\site-packages']
sys.argv: ['C:\\test\\foo.py']

Why does sys.path[0] become empty string when I execute my program as a module?

The documentation at http://docs.python.org/3.4/library/sys.html#sys.path mentions this:

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.

So if the script directory is not available, only then it is supposed to set sys.path[0] to ''. But in my case, even while executing python -m foo, the script directory foo is clearly available. So it should not set sys.path[0] to '' as per the documentation. Instead, it should set it to 'C:\\test'.

Is it a bug in the documentation or a bug in the Python interpreter or a bug in my understanding?

like image 931
Lone Learner Avatar asked Mar 03 '14 18:03

Lone Learner


People also ask

What is empty string in SYS path?

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.

What does Sys path do in Python?

sys. path is a built-in variable within the sys module. It contains a list of directories that the interpreter will search in for the required module. When a module(a module is a python file) is imported within a Python file, the interpreter first searches for the specified module among its built-in modules.

How does Sys path get populated?

As the docs explain, sys. path is populated using the current working directory, followed by directories listed in your PYTHONPATH environment variable, followed by installation-dependent default paths, which are controlled by the site module.

How does Sys path get set?

By default, sys. path is constructed as a concatenation of (1) the current working directory, (2) content of PYTHONPATH environment variable, and (3) a set of default paths supplied by the installed Python interpreter.


1 Answers

Look at what man python says about -m:

-m module-name

Searches sys.path for the named module and runs the corresponding .py file as a script.

If you think about it, it makes no sense to add the directory containing the .py file to be run into sys.path, as it already has to be there to be found in the first place. So, IMO, the behavior is correct.

But the documentation says that if it is run as a script, the directory containing it should be prepended to sys.path. I would say that it is an error in the documentation, it should say:

[...] (e.g. if the interpreter is invoked interactively or if the script is read from standard input or if the script is run using the -m option) [...]

like image 188
rodrigo Avatar answered Sep 29 '22 09:09

rodrigo