Inconsistent behavior that adding current directory to sys.path

Hi Python Discussion,

I ran into a weird problem lately about current directory added in sys.path when running scripts. (i.e. not interactive mode) I have tested it on two different environment and got different results. I know the default behavior is not adding the current directory but only the parent directory of the script. However, one of the environment I’m testing have it Python kept adding the current directory and I wonder how to figure out the reason. Here are the tests I ran:
In test.py:

import sys
print(sys.path)

(env 1: CentOS 7;conda 24.3.0;CPython 3.10.13;under base conda environment)
test 1: current dir added

(base) [shaoq1@mutant shaoq1]$ python bin/dev_test/test.py 
['/panfs/home/shaoq1/bin/dev_test', '/panfs/home/shaoq1', '/home/shaoq1/bin/miniconda3/lib/python310.zip', '/home/shaoq1/bin/miniconda3/lib/python3.10', '/home/shaoq1/bin/miniconda3/lib/python3.10/lib-dynload', '/home/shaoq1/bin/miniconda3/lib/python3.10/site-packages']

test 2: current dir added

(base) [shaoq1@mutant shaoq1]$ python -S bin/dev_test/test.py 
['/panfs/home/shaoq1/bin/dev_test', '', '/home/shaoq1/bin/miniconda3/lib/python310.zip', '/home/shaoq1/bin/miniconda3/lib/python3.10', '/home/shaoq1/bin/miniconda3/lib/python3.10/lib-dynload']

(env 2: Ubuntu 22.04; conda 23.5.2; CPython 3.11.4)
test 3: current dir not added

(base) shaoqz@Catalystjr:~$ python test/test.py 
['/home/shaoqz/test', '/home/shaoqz/bin/miniconda3/lib/python311.zip', '/home/shaoqz/bin/miniconda3/lib/python3.11', '/home/shaoqz/bin/miniconda3/lib/python3.11/lib-dynload', '/home/shaoqz/bin/miniconda3/lib/python3.11/site-packages']

Is there any suggestion about where I can look into to figure out the reason of such difference? Is this a known issue?

Thanks in advance.
QZ

hmm – in the Python 3.11 changelog:

“”"
New -P command line option and PYTHONSAFEPATH environment variable to disable automatically prepending potentially unsafe paths to sys.path
“”"

I notice that you are using the P flag with 3.10, but it probably does nothing there.

And I’d expect that without the P flag, you would have gotten the cwd under 3.11

Is there any chance that conda Python 3.11 has a different default?

Or that while not well documented, 3.11 stopped prepending the cwd?

By the way, my conda py3.11 does the same thing as yours (appends only the path that the script is in) – but if I use the P flag, it doesn’t even add that:

(py3) chris@ChristophersAir ~ % python junk/test_path.py
['/Users/chris/Junk', '/Users/chris/miniforge3/envs/py3/lib/python311.zip', '/Users/chris/miniforge3/envs/py3/lib/python3.11', '/Users/chris/miniforge3/envs/py3/lib/python3.11/lib-dynload', '/Users/chris/miniforge3/envs/py3/lib/python3.11/site-packages']

(py3) chris@ChristophersAir ~ % python -P junk/test_path.py
['/Users/chris/miniforge3/envs/py3/lib/python311.zip', '/Users/chris/miniforge3/envs/py3/lib/python3.11', '/Users/chris/miniforge3/envs/py3/lib/python3.11/lib-dynload', '/Users/chris/miniforge3/envs/py3/lib/python3.11/site-packages']

For what it’s worth, I like this behavior :slight_smile:

Hi Chris,

Thank you for the reply! I think the behavior you showed is the one I expected.
However, in my case, not only the parent directory of the script is pended in to sys.path but also cwd. So in your case, it will be both /User/chris/Junk and /User/chris when -P is not specified.
The flag I used in my case is -S which disables the site module. It seems the problem is about how site works differently between env1 and env2?