How can I get a list of locally installed Python modules?

前端 未结 30 2573
庸人自扰
庸人自扰 2020-11-22 04:32

I would like to get a list of Python modules, which are in my Python installation (UNIX server).

How can you get a list of Python modules installed in your computer?

30条回答
  •  自闭症患者
    2020-11-22 05:07

    1. to get all available modules, run sys.modules
    2. to get all installed modules (read: installed by pip), you may look at pip.get_installed_distributions()

    For the second purpose, example code:

    import pip
    for package in pip.get_installed_distributions():
        name = package.project_name # SQLAlchemy, Django, Flask-OAuthlib
        key = package.key # sqlalchemy, django, flask-oauthlib
        module_name = package._get_metadata("top_level.txt") # sqlalchemy, django, flask_oauthlib
        location = package.location # virtualenv lib directory etc.
        version = package.version # version number
    

提交回复
热议问题