2

我使用 Python 已经有几年了,但它从来都不是我的主要语言,而且我从来没有将它用于需要担心依赖关系的项目。

我需要使用virtualenv. 安装它似乎很容易,但我试图理解一种逻辑方法来开发我当前项目正在使用的依赖项列表。

如果我尝试pip freeze -l,我会发现超过 100 个依赖项,其中大部分都没有用于我的这个项目。

有没有一种简单的方法可以发现该项目使用了我的机器上安装的哪些库(以及版本)?

4

1 回答 1

4

I'm not aware of any good way to do this, unfortunately. Generally when I'm faced with this kind of situation, I'll use pip freeze to get the versions of all the packages installed on my system, then just start guessing and checking which ones are needed for the project.

For example, if I know my project needs Django, I'll figure out which version of Django is listed by pip freeze, add that to the project's requirements.txt file, install that version of Django into the virtualenv, then try to run the project and see what crashes.

You could also do a sanity check to make sure that you haven't missed anything by running something like:

egrep -h -R --include='*.py' '^(import|from) ' myproject/ | cut -d. -f1 | awk '{ print $2 }' | sort -u

And double checking that all the non-standard-library packages are installed.

Note, though, that this won't consider packages which aren't explicitly imported…</p>

于 2013-11-07T21:08:06.300 回答