我有一个需要在新的虚拟环境(在另一台机器上)中运行的大型 python 程序。该程序导入了几个外部模块(需要先在新环境中安装)。
例如,我的模块之一具有以下导入:
import matplotlib
import os
from kivy.uix.label import Label
import my_file.my_module_5 as my_mod_5
另一个模块有:
import my_module_7
import django
在这种情况下,我需要创建一个这样的列表:
['matplotlib', 'kivy', 'django']
请注意,我自己的模块不包括在内,因为它们是将迁移到新环境的程序的一部分,不必安装。属于 python 一部分的模块也不是
os
.我创建了一个函数,用于查找项目中所有导入的模块并过滤掉属于项目本身的模块。但是,它也会返回标准的 Python 模块,如
os
, sys
等等。def all_modules_in_project():
"""
Finds all modules imported in the current working directory tree.
:return: Set of module names.
"""
project_directories = set()
project_files = set()
modules_imported = set()
for path, dirs_names, files_names_in_dir in os.walk(os.getcwd()):
project_directories |= set(dirs_names)
for file_name in files_names_in_dir:
if file_name.endswith('.py'):
project_files.add(file_name[:-3])
with open(path + '/' + file_name, 'r') as opened_file:
file_lines = opened_file.readlines()
for line in file_lines:
# import XXX
match = re.match(r'import\s([\w\.]+)', line)
if match:
modules_imported.add(match.groups()[0])
# from XXX
match = re.match(r'from\s([\w\.]+)', line)
if match:
modules_imported.add(match.groups()[0])
# Removes XXX that were matched as follows `import proj_dir. .. .XXX`
for module in modules_imported.copy():
matched = re.match(r'(\w+)\.', module)
if matched:
pre_dot = matched.groups()[0]
if pre_dot in project_directories:
modules_imported.remove(module)
else:
# Replaces `xxx.yyy` with `xxx`
modules_imported.remove(module)
modules_imported.add(pre_dot)
return modules_imported - project_files - project_directories
被安装?
(我不需要 all installed packages ;我只需要程序导入的那些)
最佳答案
您可以使用 get_installed_distributions
在脚本中获取已安装的第三方发行版列表。方法来自 pip
:
from pip import get_installed_distributions
# Get a list of distribution objects (see pkg_resources documentation)
dist_list = get_installed_distributions()
# Get a list of distribution names as strings
dist_names = [dist.project_name for dist in dist_list]
然后,您可以在函数中使用该列表来确定一个包是标准 lib 事物还是第三方事物,例如:
if pre_dot in dist_names:
modules_imported.add(module)
有关分布对象的更多信息,请查看 pkg_resources documentation
编辑:由于您在 venv 中,您将拥有 pip,但如果您没有 pip,则可以使用您知道要安装的软件包获得如下类似的列表(我正在使用
setuptools
在这个例子中):from pkg_resources import get_distribution, find_distributions
site_packages_dir = get_distribution('setuptools').location
third_party_packages = []
for dist in find_distributions(site_packages_dir):
third_party_packages.append(dist.project_name)
关于python - 查找所有导入的外部库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35491825/