When working with Python packages, you might need to inspect which files were installed and where they're located. This is particularly useful for:
- Debugging import errors
- Verifying installations
- Checking file permissions
- Cleaning up orphaned files
The modern approach uses pip's built-in functionality:
pip show --files package_name
For example, to see files installed with requests:
pip show --files requests
This will display metadata including:
- Location: The base installation directory
- Files: All installed files relative to the location
For more detailed information:
pip install pipdeptree
pipdeptree -p package_name --json
For programmatic access:
import importlib.util
import os
def get_package_files(package_name):
spec = importlib.util.find_spec(package_name)
if spec is None:
return None
origin = spec.origin
if origin.endswith('__init__.py'):
package_dir = os.path.dirname(origin)
return [
os.path.join(root, file)
for root, dirs, files in os.walk(package_dir)
for file in files
]
return [origin]
print(get_package_files('requests'))
For complete package metadata including all files:
import importlib.metadata
def get_package_metadata(package_name):
try:
dist = importlib.metadata.distribution(package_name)
return {
'files': [str(f) for f in dist.files],
'metadata': dist.metadata
}
except importlib.metadata.PackageNotFoundError:
return None
print(get_package_metadata('numpy'))
- For development installations (pip install -e), files may be symlinks
- Some packages may use .egg-info instead of .dist-info
- Virtual environments will show different paths than system installs
Remember that wheel installations might not perfectly match source distributions due to build-time file generation.
When working with Python packages, it's often necessary to inspect which files were installed and where they're located. Unlike system package managers like dpkg or rpm, Python's package tools don't provide a direct single-command solution, but there are several effective approaches.
The most straightforward method is using pip's built-in functionality:
pip show --files package_name
For example, to see files installed by the requests package:
pip show --files requests
This will display the package metadata including the installation location and all files associated with the package.
Python packages store installation records in their metadata. You can access this information programmatically:
import importlib.metadata
files = importlib.metadata.files('package_name')
print(list(files))
For older Python versions (before 3.8), use:
import pkg_resources
files = pkg_resources.resource_listdir('package_name', '')
print(files)
First, find where the package is installed:
python -c "import package_name; print(package_name.__file__)"
Then navigate to the parent directory and list all files recursively. For example:
import os
import requests
package_path = os.path.dirname(requests.__file__)
for root, dirs, files in os.walk(package_path):
for file in files:
print(os.path.join(root, file))
Here's a more comprehensive solution that mimics dpkg -L behavior:
def list_package_files(package_name):
try:
from importlib import metadata
except ImportError:
import importlib_metadata as metadata
try:
package_files = metadata.files(package_name)
if package_files is not None:
return sorted(str(f) for f in package_files)
except metadata.PackageNotFoundError:
pass
try:
import pkg_resources
return [f for f in pkg_resources.resource_listdir(package_name, '')
if not f.endswith('.pyc')]
except:
pass
try:
import pip
from pip._internal.utils.misc import get_installed_distributions
for dist in get_installed_distributions():
if dist.key == package_name.lower():
return list(dist.get_metadata_lines('RECORD'))
except:
pass
return []
print("\n".join(list_package_files('requests')))
Some packages might use namespace packages or have unusual structures. For these cases, you might need to:
# For namespace packages
import pkgutil
for _, name, _ in pkgutil.iter_modules(['namespace']):
print(name)
Remember that some files might be generated at runtime (like .pyc files) or installed in system locations not captured by these methods.