-
I implement a helper function to get the installed path of a package rather than a module. import os.path as osp
from pkg_resources import get_distribution
def get_installed_path(package: str) -> str:
pkg = get_distribution(package)
possible_path = osp.join(pkg.location, package)
if osp.exists(possible_path):
return possible_path
else:
if pkg.has_metadata('top_level.txt'):
module_name = pkg.get_metadata('top_level.txt').split('\n')[0]
return osp.join(pkg.location, module_name)
else:
raise ValueError(f'can not get the installed path of {package}')
>>> get_installed_path('mysql-python')
'.../lib/python3.7/site-packages/MySQLdb'
>>> get_installed_path('python-memcached')
'.../lib/python3.7/site-packages/memcache' |
Beta Was this translation helpful? Give feedback.
Answered by
webknjaz
Jul 27, 2021
Replies: 1 comment 5 replies
-
Is there a question somewhere in this statement? The only thing I'll mention is that today it's best to use |
Beta Was this translation helpful? Give feedback.
5 replies
Answer selected by
webknjaz
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is there a question somewhere in this statement?
The only thing I'll mention is that today it's best to use
importlib.metadata
in most places where you'd usepkg_resources
in the past.