Python 依赖项?
是否可以以编程方式检测给定 SVN 中的 python 项目的依赖关系?
Is it possible to programmatically detect dependencies given a python project residing in SVN?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里有一个增加一些精度的变化,如果您发现经常检查各种代码的依赖关系,这可能很有用:
代码:
示例
foobar
模块:输出:
Here is a twist which adds some precision, and which might be useful if you find you're frequently checking dependencies of miscellaneous code:
Code:
Example
foobar
module:Output:
绝对地!如果您使用 UNIX 或 Linux shell,则可以使用
grep
和awk
的简单组合;基本上,您要做的就是搜索包含“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
andawk
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
import
ed modules to their package name (the first one that comes to mind is thePIL
module, in Ubuntu it's provided by thepython-imaging
package).Python 代码可以使用运行时构造的字符串导入模块,因此唯一可靠的方法是运行代码。真实示例:当您使用 SQLAlchemy 的 dbconnect 打开数据库时,该库将根据数据库字符串的内容加载一个或多个 db-api 模块。
如果您愿意运行代码,这里有一个相对简单的方法,通过检查
sys.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:Here, too, you should keep in mind that this could theoretically fail if
execute_code_of_interest()
modifiessys.modules
, but I believe that's quite rare in production code.