Python packages are denoted by the existence of a file named __init__.py
. So if you want to count packages in terms of that formal definition, you can just count the number of files you find with this name. Here's code that will do that:
import os
containing_folder = '/usr/local/lib/python3.7/dist-packages'
f = []
for (dirpath, dirnames, filenames) in os.walk(containing_folder):
if '__init__.py' in filenames:
f.append(os.path.basename(dirpath))
print(f)
print('there are', len(f), 'folders in the python 3.7 module')
If you just want to count the number of packages at the first level of a directory, which is probably what you want, here's code that does that:
import os
containing_folder = '/usr/local/lib/python3.7/dist-packages'
r = []
for entity in os.listdir(containing_folder):
f = os.path.join(containing_folder, entity, '__init__.py')
if os.path.isdir(os.path.join(containing_folder, entity)) and os.path.join(entity) and os.path.exists(f):
r.append(entity)
print(len(r))
When I ran this code on one of my Python installs, and compared it against what I get when I do pip list | wc -l
on that same version of Python, I got almost the same result...125
for the Python code, 129
for pip
.