Python Forum

Full Version: -i option changes sys.path (removes leading empty string '')
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have an issue that I found very strange.

Why would invoking Python with '-i' option affect sys.path?

I have two aliases that I use to launch Python:

alias p='python3 '
alias pr='python3 -i ~/.pythonrc'
When I use the latter, sys.path does not include '' at the beginning so I cannot load modules from the CWD.

$ pr
>>> import sys
>>> sys.path
['/home/x', '/usr/lib/python39.zip', '/usr/lib/python3.9', '/usr/lib/python3.9/lib-dynload', '/usr/local/lib/python3.9/site-packages', '/usr/lib/python3.9/site-packages']
>>>
if I add '' to sys.path it works fine:

>>> import sys
>>> sys.path.insert(0,'')
>>> import koop_db
>>>
Also, if I call it without the -i option it works fine:

$ p
Python 3.9.10 (main, Jan 20 2022, 21:37:52)
[GCC 11.2.0] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib/python39.zip', '/usr/lib/python3.9', '/usr/lib/python3.9/lib-dynload', '/usr/local/lib/python3.9/site-packages', '/usr/lib/python3.9/site-packages']
>>>
Does anyone have any idea why it would be designed this way?

Is this a bug
Seems odd. I note that it only seems to happen when a file is passed. If the interpreter is started with no program, or with -c <commands>, then it's back to including the null directory.
From the documents:

https://docs.python.org/3/using/cmdline.html
Quote:-i
When a script is passed as first argument or the -c option is used, enter interactive mode after executing the script or the command, even when sys.stdin does not appear to be a terminal. The PYTHONSTARTUP file is not read.

This can be useful to inspect global variables or a stack trace when a script raises an exception. See also PYTHONINSPECT.

What is in the PYTHONSTARTUP file?

Quote:PYTHONSTARTUP
If this is the name of a readable file, the Python commands in that file are executed before the first prompt is displayed in interactive mode. The file is executed in the same namespace where interactive commands are executed so that objects defined or imported in it can be used without qualification in the interactive session. You can also change the prompts sys.ps1 and sys.ps2 and the hook sys.__interactivehook__ in this file.

Raises an auditing event cpython.run_startup with the filename as the argument when called on startup.
Do you have a PYTHONSTARTUP environment variable?
It's not the -i, it's the fact that you're providing a file to one and not the other. I think the first element is the location of that file (containing directory). If there isn't a file, an empty string is added instead. Nothing changes with the -i.
I checked and do not have $PYTHONSTARTUP set.

It's true that the first element in the search path is the location of the included file, and whether it is -i (interactive) does not matter.

I guess I can see why this choice might have been made.

I can see reasons why the CWD would need to be excluded from sys.path in some cases. Thanks.

Also, I find this related link just now:

https://github.com/microsoft/debugpy/issues/759
You could do the
sys.path.insert(0, '')
in ~/.pythonrc
or perhaps
sys.path.insert(0, os.getcwd())
Inserting an empty string in sys.path is exactly what I did, and it works like a champ! Thanks!