Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

pythonpath - Why is my Python 'sys.path' different for each tool I use to run my code?

I'm confused about the rules used to construct my Python sys.path. I understand that the fist entry will be "the directory containing the script that was used to invoke the Python interpreter", or an empty string if Python was invoked interactively, and that the last entires will be entries based on the installation of Python. But I'm confused about what's going on between these two sets of entries, in particular, how they relate to my PYTHONPATH.

When I (A) execute a script that prints out the entries of my sys.path using python test.py at the system command prompt in the ../scriptpath directory, I get:

/Users/Rax/Documents/Projects/scriptpath
... (my system PYTHONPATH, as configured in .bash_profile)  #(i)
/Users/Rax/Documents/Projects/scriptpath
... (system entries)

which is not the same as what I get when (B) I run import test while in ../scriptpath in the IPython shell

(empty string)
/usr/local/bin
... (my system PYTHONPATH)  #(i)
/Users/Rax/Documents/Projects/scriptpath
... (system entries)
/Library/Python/2.7/site-packages/IPython/extensions

or (C) in a Python shell

(empty string)
... (my system PYTHONPATH)  #(i)
/Users/Rax/Documents/Projects/scriptpath
... (system entries)

while, (D) when I run test.py in my IDE (PyCharm) I get either

/Users/Rax/Documents/Projects/scriptpath
... (user-added entries in the PyCharm Interpreter Paths settings)  #(i)
... (system entries)

or (E)

/Users/Rax/Documents/Projects/scriptpath
... (user-added entries in the PyCharm Interpreter Paths settings)  #(i)
/Users/Rax/Documents/Projects/scriptpath
... (system entries)

depending on whether I have an option to include the "contents root" enabled or not.

I think I understand where the (i) entries come from in each case: all are using my system PYTHONPATH, except PyCharm, which uses the "user added" paths at the end of "Paths" in "Interpreter Settings"; but I don't understand where the other entires come from (or for that matter, how PyCharm is able to ignore my system PYTHONPATH).

Why is the .. scriptpath entry sometimes repeated before and after (i)? What rules are used to add the other entries (such as /usr/local/bin in (B)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...