是第一个进入系统路径应该代表当前的工作目录?

2024-05-31 23:50:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直假设sys.path中的第一个条目默认是当前工作目录。但事实证明,在我的系统中,第一个条目是脚本所在的路径。因此,如果我执行的脚本位于/usr/bin中,则sys.path中的第一个条目是/usr/bin。是我的系统配置错误,还是这是预期的行为?在


Tags: path路径目录脚本binusr系统错误
2条回答

这是设计的:

As initialized upon program startup, the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter.

来源:http://docs.python.org/library/sys.html#sys.path

您可以使用os.getcwd()获取当前目录。在

相关问题 更多 >