Python 依赖项?

发布于 2024-10-20 12:00:31 字数 43 浏览 5 评论 0原文

是否可以以编程方式检测给定 SVN 中的 python 项目的依赖关系?

Is it possible to programmatically detect dependencies given a python project residing in SVN?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黄昏下泛黄的笔记 2024-10-27 12:00:31

这里有一个增加一些精度的变化,如果您发现经常检查各种代码的依赖关系,这可能很有用:

  • 仅捕获由正在分析的代码执行的导入语句。
  • 自动排除所有系统加载的模块,因此您不必清除它。
  • 还报告从每个模块导入的符号。

代码:

import __builtin__
import collections
import sys

IN_USE = collections.defaultdict(set)
_IMPORT = __builtin__.__import__

def _myimport(name, globs=None, locs=None, fromlist=None, level=-1):
    global IN_USE
    if fromlist is None:
        fromlist = []
    IN_USE[name].update(fromlist)
    return _IMPORT(name, globs, locs, fromlist, level)

# monkey-patch __import__
setattr(__builtin__, '__import__', _myimport)

# import and run the target project here and run the routine
import foobar
foobar.do_something()

# when it finishes running, dump the imports
print 'modules and symbols imported by "foobar":'
for key in sorted(IN_USE.keys()):
    print key
    for name in sorted(IN_USE[key]):
        print '  ', name

示例 foobar 模块:

import byteplay
import cjson

def _other():
    from os import path
    from sys import modules

def do_something():
    import hashlib
    import lxml
    _other()

输出:

modules and symbols imported by "foobar":
_hashlib
array
   array
byteplay
cStringIO
   StringIO
cjson
dis
   findlabels
foobar
hashlib
itertools
lxml
opcode
   *
   __all__
operator
os
   path
sys
   modules
types
warnings

Here is a twist which adds some precision, and which might be useful if you find you're frequently checking dependencies of miscellaneous code:

  • Catches only import statements executed by the code being analyzed.
  • Automatically excludes all system-loaded modules, so you don't have to weed through it.
  • Also reports the symbols imported from each module.

Code:

import __builtin__
import collections
import sys

IN_USE = collections.defaultdict(set)
_IMPORT = __builtin__.__import__

def _myimport(name, globs=None, locs=None, fromlist=None, level=-1):
    global IN_USE
    if fromlist is None:
        fromlist = []
    IN_USE[name].update(fromlist)
    return _IMPORT(name, globs, locs, fromlist, level)

# monkey-patch __import__
setattr(__builtin__, '__import__', _myimport)

# import and run the target project here and run the routine
import foobar
foobar.do_something()

# when it finishes running, dump the imports
print 'modules and symbols imported by "foobar":'
for key in sorted(IN_USE.keys()):
    print key
    for name in sorted(IN_USE[key]):
        print '  ', name

Example foobar module:

import byteplay
import cjson

def _other():
    from os import path
    from sys import modules

def do_something():
    import hashlib
    import lxml
    _other()

Output:

modules and symbols imported by "foobar":
_hashlib
array
   array
byteplay
cStringIO
   StringIO
cjson
dis
   findlabels
foobar
hashlib
itertools
lxml
opcode
   *
   __all__
operator
os
   path
sys
   modules
types
warnings
摘星┃星的人 2024-10-27 12:00:31

绝对地!如果您使用 UNIX 或 Linux shell,则可以使用 grepawk 的简单组合;基本上,您要做的就是搜索包含“import”关键字的行。

但是,如果您在任何环境中工作,您只需编写一个小的 Python 脚本来为您进行搜索(不要忘记字符串被视为不可变序列,因此您可以执行类似 if "import" 的操作一行中:...

一个棘手的问题是将这些导入的模块与其包名称相关联(首先想到的是 PIL) code> 模块,在 Ubuntu 中它由 python-imaging 包提供)。

Absolutely! If you are working from a UNIX or Linux shell, a simple combination of grep and awk would work; basically, all you want to do is search for lines containing the "import" keyword.

However, if you are working from any environment, you could just write a small Python script to do the searching for you (don't forget that strings are treated as immutable sequences, so you can do something like if "import" in line: ....

The one sticky spot, would be associating those imported modules to their package name (the first one that comes to mind is the PIL module, in Ubuntu it's provided by the python-imaging package).

清旖 2024-10-27 12:00:31

Python 代码可以使用运行时构造的字符串导入模块,因此唯一可靠的方法是运行代码。真实示例:当您使用 SQLAlchemy 的 dbconnect 打开数据库时,该库将根据数据库字符串的内容加载一个或多个 db-api 模块。

如果您愿意运行代码,这里有一个相对简单的方法,通过检查 sys.modules 完成时:

>>> from sys import modules
>>> import codeofinterest
>>> execute_code_of_interest()
>>> print modules
[ long, list, of, loaded, modules ]

这里,您也应该记住,如果 execute_code_of_interest() 修改 sys,理论上这可能会失败.modules,但我相信这在生产代码中相当罕见。

Python code can import modules using runtime-constructed strings, so the only surefire way would be to run the code. Real-world example: when you open a database with SQLAlchemy's dbconnect, the library will load one or more db-api modules depending on the content of your database string.

If you're willing to run the code, here is a relatively simple way to do this by examining sys.modules when it finishes:

>>> from sys import modules
>>> import codeofinterest
>>> execute_code_of_interest()
>>> print modules
[ long, list, of, loaded, modules ]

Here, too, you should keep in mind that this could theoretically fail if execute_code_of_interest() modifies sys.modules, but I believe that's quite rare in production code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文