我想检测模块是否发生了变化。现在,使用inotify很简单,你只需要知道你想从哪个目录获取通知。
如何在python中检索模块的路径?
我想检测模块是否发生了变化。现在,使用inotify很简单,你只需要知道你想从哪个目录获取通知。
如何在python中检索模块的路径?
当前回答
我想提供一个常见的场景(在Python 3中),并探索一些实现它的方法。
内置函数open()接受相对路径或绝对路径作为其第一个参数。相对路径被视为相对于当前工作目录,因此建议将绝对路径传递给文件。
简单地说,如果你用下面的代码运行一个脚本文件,并不保证example.txt文件会被创建在脚本文件所在的同一目录下:
with open('example.txt', 'w'):
pass
要修复这段代码,我们需要获取脚本的路径,并使其成为绝对路径。为了确保路径是绝对的,我们只需使用os.path.realpath()函数。为了获得脚本的路径,有几个常见的函数返回不同的路径结果:
os.getcwd() os.path.realpath('example.txt') sys.argv[0] __文件__
os.getcwd()和os.path.realpath()函数都会返回基于当前工作目录的路径结果。通常不是我们想要的。系统的第一个元素。Argv list是根脚本(您运行的脚本)的路径,而不管您是在根脚本本身中还是在其任何模块中调用该列表。在某些情况下可能会派上用场。__file__变量包含调用它的模块的路径。
下面的代码正确地在脚本所在的目录中创建了一个文件example.txt:
filedir = os.path.dirname(os.path.realpath(__file__))
filepath = os.path.join(filedir, 'example.txt')
with open(filepath, 'w'):
pass
其他回答
如果你导入的是一个site-package(例如pandas),我建议这样获取它的目录(如果import是一个模块,例如pathlib):
from importlib import resources # part of core Python
import pandas as pd
package_dir = resources.path(package=pd, resource="").__enter__()
在一般情况下。当任务是关于访问站点包的路径/资源时,可以考虑资源。
import os
path = os.path.abspath(__file__)
dir_path = os.path.dirname(path)
如果使用__file__的唯一注意事项是当前相对目录为空(即,当脚本从脚本所在的同一目录运行时,作为脚本运行),那么一个简单的解决方案是:
import os.path
mydir = os.path.dirname(__file__) or '.'
full = os.path.abspath(mydir)
print __file__, mydir, full
结果是:
$ python teste.py
teste.py . /home/user/work/teste
诀窍在于“或”。'在dirname()调用之后。它将dir设置为.,这意味着当前目录,并且对于任何与路径相关的函数都是有效的目录。
因此,实际上并不需要使用abspath()。但是如果您无论如何都使用它,那么就不需要这个技巧了:abspath()接受空白路径并将其正确地解释为当前目录。
正如其他答案所说,最好的方法是使用__file__(下面再次演示)。然而,有一个重要的警告,即如果你单独运行模块(即作为__main__), __file__是不存在的。
例如,假设你有两个文件(都在你的PYTHONPATH上):
#/path1/foo.py
import bar
print(bar.__file__)
and
#/path2/bar.py
import os
print(os.getcwd())
print(__file__)
运行foo.py会得到如下输出:
/path1 # "import bar" causes the line "print(os.getcwd())" to run
/path2/bar.py # then "print(__file__)" runs
/path2/bar.py # then the import statement finishes and "print(bar.__file__)" runs
然而,如果你试图单独运行bar.py,你会得到:
/path2 # "print(os.getcwd())" still works fine
Traceback (most recent call last): # but __file__ doesn't exist if bar.py is running as main
File "/path2/bar.py", line 3, in <module>
print(__file__)
NameError: name '__file__' is not defined
希望这能有所帮助。在测试其他解决方案时,这个警告花费了我大量的时间和混乱。
import module
print module.__path__
包还支持一个特殊的属性__path__。这是 初始化为包含保存目录名称的列表 在执行该文件中的代码之前,包的__init__.py。 这个变量可以修改;这样做会影响将来的搜索 包中包含的模块和子包。 虽然不经常需要此特性,但可以使用它来扩展 在包中找到的模块集。
源