如何使用python查找python路径?

时间:2021-07-12 07:15:19

How do I find out which directories are listed in my system’s PYTHONPATH variable, from within a Python script (or the interactive shell)?

如何从Python脚本(或交互shell)中找出系统的PYTHONPATH变量中列出了哪些目录?

3 个解决方案

#1


176  

sys.path might include items that aren't specifically in your PYTHONPATH environment variable. To query the variable directly, use:

sys。path可能包含在PYTHONPATH环境变量中没有特定的项。要直接查询变量,使用:

import os
try:
    user_paths = os.environ['PYTHONPATH'].split(os.pathsep)
except KeyError:
    user_paths = []

#2


443  

You would probably also want this:

你可能也想这样:

import sys
print(sys.path)

Or as a one liner from the terminal:

或作为终端机的一个班轮:

python -c "import sys; print('\n'.join(sys.path))"

#3


11  

Can't seem to edit the other answer. Has a minor error in that it is Windows-only. The more generic solution is to use os.sep as below:

似乎无法编辑另一个答案。有一个小错误,它是只针对windows的。更通用的解决方案是使用os。9月:

sys.path might include items that aren't specifically in your PYTHONPATH environment variable. To query the variable directly, use:

sys。path可能包含在PYTHONPATH环境变量中没有特定的项。要直接查询变量,使用:

import os
os.environ['PYTHONPATH'].split(os.pathsep)

#1


176  

sys.path might include items that aren't specifically in your PYTHONPATH environment variable. To query the variable directly, use:

sys。path可能包含在PYTHONPATH环境变量中没有特定的项。要直接查询变量,使用:

import os
try:
    user_paths = os.environ['PYTHONPATH'].split(os.pathsep)
except KeyError:
    user_paths = []

#2


443  

You would probably also want this:

你可能也想这样:

import sys
print(sys.path)

Or as a one liner from the terminal:

或作为终端机的一个班轮:

python -c "import sys; print('\n'.join(sys.path))"

#3


11  

Can't seem to edit the other answer. Has a minor error in that it is Windows-only. The more generic solution is to use os.sep as below:

似乎无法编辑另一个答案。有一个小错误,它是只针对windows的。更通用的解决方案是使用os。9月:

sys.path might include items that aren't specifically in your PYTHONPATH environment variable. To query the variable directly, use:

sys。path可能包含在PYTHONPATH环境变量中没有特定的项。要直接查询变量,使用:

import os
os.environ['PYTHONPATH'].split(os.pathsep)