Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
可以使用 获取当前脚本的路径os.path.pathname(sys.argv[0])。但是我怎样才能获得不是执行脚本的模块的路径,而是脚本导入了模块。我需要它来读取模块的配置文件,该文件应该与模块位于同一文件夹中,但模块可以安装在任何地方。
os.path.pathname(sys.argv[0])
import os import your_module # Get directory of your module print os.path.dirname(your_module.__file__)
演示:
import os import re print os.path.dirname(re.__file__)
输出:
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7