我想检测模块是否发生了变化。现在,使用inotify很简单,你只需要知道你想从哪个目录获取通知。

如何在python中检索模块的路径?


当前回答

如果你使用pip安装它,“pip show”工作得很好(“Location”)

$ PIP show detectron2

Name: detectron2
Version: 0.1
Summary: Detectron2 is FAIR next-generation research platform for object detection and segmentation.
Home-page: https://github.com/facebookresearch/detectron2
Author: FAIR
Author-email: None
License: UNKNOWN
Location: /home/ubuntu/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages
Requires: yacs, tabulate, tqdm, pydot, tensorboard, Pillow, termcolor, future, cloudpickle, matplotlib, fvcore

更新:

$ python -m PIP show mymodule

(author: wisbucky。)

其他回答

您可以导入您的模块 然后点击它的名字,你会得到它的完整路径

>>> import os
>>> os
<module 'os' from 'C:\\Users\\Hassan Ashraf\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\os.py'>
>>>

我也会尝试解决这个问题的一些变化:

查找被调用脚本的路径 查找当前执行脚本的路径 查找被调用脚本的目录

(其中一些问题已经在SO上被问过,但在这里已经被关闭并重新定向。)

使用__file__的注意事项

对于已导入的模块:

import something
something.__file__ 

将返回模块的绝对路径。然而,给定下面的脚本foo.py:

#foo.py
print '__file__', __file__

用'python foo.py'调用它只会返回'foo.py'。如果你加上一个shebang:

#!/usr/bin/python 
#foo.py
print '__file__', __file__

并使用。/foo.py调用它,它将返回'./foo.py'。从不同的目录调用它(例如把foo.py放在目录栏中),然后调用任何一个

python bar/foo.py

或者添加一个shebang并直接执行文件:

bar/foo.py

将返回'bar/foo.py'(相对路径)。

查找目录

现在从那里获取目录os.path.dirname(__file__)也可能很棘手。至少在我的系统上,如果从与文件相同的目录调用它,它将返回一个空字符串。前女友。

# foo.py
import os
print '__file__ is:', __file__
print 'os.path.dirname(__file__) is:', os.path.dirname(__file__)

将输出:

__file__ is: foo.py
os.path.dirname(__file__) is: 

换句话说,它返回一个空字符串,所以如果您想将它用于当前文件(而不是导入模块的文件),这似乎不可靠。为了解决这个问题,你可以调用abspath:

# foo.py
import os
print 'os.path.abspath(__file__) is:', os.path.abspath(__file__)
print 'os.path.dirname(os.path.abspath(__file__)) is:', os.path.dirname(os.path.abspath(__file__))

输出如下:

os.path.abspath(__file__) is: /home/user/bar/foo.py
os.path.dirname(os.path.abspath(__file__)) is: /home/user/bar

注意,abspath()不解析符号链接。如果您想这样做,请使用realpath()。例如,创建一个符号链接file_import_testing_link指向file_import_testing.py,内容如下:

import os
print 'abspath(__file__)',os.path.abspath(__file__)
print 'realpath(__file__)',os.path.realpath(__file__)

执行将输出绝对路径,如下所示:

abspath(__file__) /home/user/file_test_link
realpath(__file__) /home/user/file_test.py

File_import_testing_link -> file_import_testing.py

使用检查

@SummerBreeze提到了使用inspect模块。

对于导入的模块,这似乎工作得很好,而且相当简洁:

import os
import inspect
print 'inspect.getfile(os) is:', inspect.getfile(os)

返回绝对路径。查找当前执行脚本的路径:

inspect.getfile(inspect.currentframe())

(感谢@jbochi)

inspect.getabsfile(inspect.currentframe()) 

给出了当前执行脚本的绝对路径(感谢@Sadman_Sakib)。

import module
print module.__path__

包还支持一个特殊的属性__path__。这是 初始化为包含保存目录名称的列表 在执行该文件中的代码之前,包的__init__.py。 这个变量可以修改;这样做会影响将来的搜索 包中包含的模块和子包。 虽然不经常需要此特性,但可以使用它来扩展 在包中找到的模块集。

下面是一个快速bash脚本,以防它对任何人都有用。我只是想设置一个环境变量,这样我就能把它推到代码中。

#!/bin/bash
module=${1:?"I need a module name"}

python << EOI
import $module
import os
print os.path.dirname($module.__file__)
EOI

壳牌的例子:

[root@sri-4625-0004 ~]# export LXML=$(get_python_path.sh lxml)
[root@sri-4625-0004 ~]# echo $LXML
/usr/lib64/python2.7/site-packages/lxml
[root@sri-4625-0004 ~]#

当你导入一个模块时,你可以访问大量的信息。检查dir(a_module)。至于路径,有一个dunder: a_module.__path__。也可以只打印模块本身。

>>> import a_module
>>> print(dir(a_module))
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']
>>> print(a_module.__path__)
['/.../.../a_module']
>>> print(a_module)
<module 'a_module' from '/.../.../a_module/__init__.py'>