I'm trying to modify a python library that I downloaded and am using. But the changes I'm making aren't doing anything. So I suspect that python is importing a different copy of this library from somewhere else on the filesystem. So...
我正在尝试修改我下载并正在使用的python库。但我正在做的改变没有做任何事情。所以我怀疑python是从文件系统的其他地方导入这个库的不同副本。所以...
When I run import foolib
in python, how can I tell where on the filesystem it's getting that library from?
当我在python中运行import foolib时,我怎么能告诉文件系统从哪里获取该库?
3 个解决方案
#1
6
import foolib
print foolib.__file__
Unfortunately, this only works for some modules. E.g. it works on a module I wrote, but not on sys
.
不幸的是,这仅适用于某些模块。例如。它适用于我编写的模块,但不适用于sys。
#2
8
the correct answer is to use sys.modules
... it works on everything, even sys
. sys.modules
is a dictionary where the keys are the imported names (modules or packages), and the values are their respective locations. here is some usage output from my Mac:
正确的答案是使用sys.modules ...它适用于所有内容,甚至是sys。 sys.modules是一个字典,其中键是导入的名称(模块或包),值是它们各自的位置。这是我的Mac的一些使用输出:
$ python
Python 2.5.1 (r251:54863, Feb 9 2009, 18:49:36)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys, os, django, google
>>> sys.modules['sys']
<module 'sys' (built-in)>
>>> sys.modules['os']
<module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/os.pyc'>
>>> sys.modules['django']
<module 'django' from '/Library/Python/2.5/site-packages/Django-1.1.1-py2.5.egg/django/__init__.pyc'>
>>> sys.modules['google']
<module 'google' from '/usr/local/google_appengine/google/__init__.py'>
#3
2
Look at the foolib.__file__
.
看看foolib .__ file__。
#1
6
import foolib
print foolib.__file__
Unfortunately, this only works for some modules. E.g. it works on a module I wrote, but not on sys
.
不幸的是,这仅适用于某些模块。例如。它适用于我编写的模块,但不适用于sys。
#2
8
the correct answer is to use sys.modules
... it works on everything, even sys
. sys.modules
is a dictionary where the keys are the imported names (modules or packages), and the values are their respective locations. here is some usage output from my Mac:
正确的答案是使用sys.modules ...它适用于所有内容,甚至是sys。 sys.modules是一个字典,其中键是导入的名称(模块或包),值是它们各自的位置。这是我的Mac的一些使用输出:
$ python
Python 2.5.1 (r251:54863, Feb 9 2009, 18:49:36)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys, os, django, google
>>> sys.modules['sys']
<module 'sys' (built-in)>
>>> sys.modules['os']
<module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/os.pyc'>
>>> sys.modules['django']
<module 'django' from '/Library/Python/2.5/site-packages/Django-1.1.1-py2.5.egg/django/__init__.pyc'>
>>> sys.modules['google']
<module 'google' from '/usr/local/google_appengine/google/__init__.py'>
#3
2
Look at the foolib.__file__
.
看看foolib .__ file__。