Python Forum
-i option changes sys.path (removes leading empty string '')
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
-i option changes sys.path (removes leading empty string '')
#1
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
Reply
#2
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.
markanth likes this post
Reply
#3
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?
markanth likes this post
Reply
#4
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.
markanth likes this post
Reply
#5
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
Reply
#6
You could do the
sys.path.insert(0, '')
in ~/.pythonrc
or perhaps
sys.path.insert(0, os.getcwd())
markanth likes this post
Reply
#7
Inserting an empty string in sys.path is exactly what I did, and it works like a champ! Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Removing leading whitespaces palladium 1 678 Mar-24-2023, 04:15 PM
Last Post: bowlofred
  Removing leading\trailing spaces azizrasul 8 2,531 Oct-23-2022, 11:06 PM
Last Post: azizrasul
  How to retrieve records in a DataFrame (Python/Pandas) that contains leading or trail mmunozjr 3 1,695 Sep-05-2022, 11:56 AM
Last Post: Pedroski55
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,602 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  Convert string to path using Python 2.7 tester_V 10 6,276 Nov-20-2021, 02:20 PM
Last Post: snippsat
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,150 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  deleting an empty Dir, using pathlib.Path tester_V 9 5,673 Jul-01-2021, 01:53 PM
Last Post: Gribouillis
  Why is my string empty? karabakh 5 2,332 Jan-30-2021, 04:51 AM
Last Post: karabakh
  Dataframe Removes Zeros JoeDainton123 2 4,319 Sep-17-2020, 12:47 PM
Last Post: scidam
  How to keep leading zeros with pandas? eeps24 1 6,471 May-20-2020, 07:51 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020