Python Forum
-i option changes sys.path (removes leading empty string '') - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: -i option changes sys.path (removes leading empty string '') (/thread-38041.html)



-i option changes sys.path (removes leading empty string '') - markanth - Aug-26-2022

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


RE: -i option changes sys.path (removes leading empty string '') - bowlofred - Aug-26-2022

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.


RE: -i option changes sys.path (removes leading empty string '') - deanhystad - Aug-26-2022

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?


RE: -i option changes sys.path (removes leading empty string '') - bowlofred - Aug-26-2022

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.


RE: -i option changes sys.path (removes leading empty string '') - markanth - Aug-26-2022

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


RE: -i option changes sys.path (removes leading empty string '') - Gribouillis - Aug-26-2022

You could do the
sys.path.insert(0, '')
in ~/.pythonrc
or perhaps
sys.path.insert(0, os.getcwd())



RE: -i option changes sys.path (removes leading empty string '') - markanth - Aug-26-2022

Inserting an empty string in sys.path is exactly what I did, and it works like a champ! Thanks!