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

前端 未结 30 2203
庸人自扰
庸人自扰 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 04:43
    • In ipython you can type "importTab".

    • In the standard Python interpreter, you can type "help('modules')".

    • At the command-line, you can use pydoc modules.

    • In a script, call pkgutil.iter_modules().

    0 讨论(0)
  • 2020-11-22 04:45

    I just use this to see currently used modules:

    import sys as s
    s.modules.keys()
    

    which shows all modules running on your python.

    For all built-in modules use:

    s.modules
    

    Which is a dict containing all modules and import objects.

    0 讨论(0)
  • 2020-11-22 04:45

    I ran into a custom installed python 2.7 on OS X. It required X11 to list modules installed (both using help and pydoc).

    To be able to list all modules without installing X11 I ran pydoc as http-server, i.e.:

    pydoc -p 12345
    

    Then it's possible to direct Safari to http://localhost:12345/ to see all modules.

    0 讨论(0)
  • 2020-11-22 04:45

    If none of the above seem to help, in my environment was broken from a system upgrade and I could not upgrade pip. While it won't give you an accurate list you can get an idea of which libraries were installed simply by looking inside your env>lib>python(version here)>site-packages> . Here you will get a good indication of modules installed.

    0 讨论(0)
  • 2020-11-22 04:47

    There are many way to skin a cat.

    • The most simple way is to use the pydoc function directly from the shell with:
      pydoc modules

    • But for more information use the tool called pip-date that also tell you the installation dates.
      pip install pip-date


    0 讨论(0)
  • 2020-11-22 04:47

    I needed to find the specific version of packages available by default in AWS Lambda. I did so with a mashup of ideas from this page. I'm sharing it for posterity.

    import pkgutil
    
    __version__ = '0.1.1'
    
    def get_ver(name):
        try:
            return str(__import__(name).__version__)
        except:
            return None
    
    def lambda_handler(event, context):
        return {
            'statusCode': 200,
            'body': [{
                       'path': m.module_finder.path,
                       'name': m.name,
                       'version': get_ver(m.name),
                     } for m in list(pkgutil.iter_modules())
                     #if m.module_finder.path == "/var/runtime" # Uncomment this if you only care about a certain path
                    ],
        }
    

    What I discovered is that the provided boto3 library was way out of date and it wasn't my fault that my code was failing. I just needed to add boto3 and botocore to my project. But without this I would have been banging my head thinking my code was bad.

    {
      "statusCode": 200,
      "body": [
        {
          "path": "/var/task",
          "name": "lambda_function",
          "version": "0.1.1"
        },
        {
          "path": "/var/runtime",
          "name": "bootstrap",
          "version": null
        },
        {
          "path": "/var/runtime",
          "name": "boto3",
          "version": "1.9.42"
        },
        {
          "path": "/var/runtime",
          "name": "botocore",
          "version": "1.12.42"
        },
        {
          "path": "/var/runtime",
          "name": "dateutil",
          "version": "2.7.5"
        },
        {
          "path": "/var/runtime",
          "name": "docutils",
          "version": "0.14"
        },
        {
          "path": "/var/runtime",
          "name": "jmespath",
          "version": "0.9.3"
        },
        {
          "path": "/var/runtime",
          "name": "lambda_runtime_client",
          "version": null
        },
        {
          "path": "/var/runtime",
          "name": "lambda_runtime_exception",
          "version": null
        },
        {
          "path": "/var/runtime",
          "name": "lambda_runtime_marshaller",
          "version": null
        },
        {
          "path": "/var/runtime",
          "name": "s3transfer",
          "version": "0.1.13"
        },
        {
          "path": "/var/runtime",
          "name": "six",
          "version": "1.11.0"
        },
        {
          "path": "/var/runtime",
          "name": "test_bootstrap",
          "version": null
        },
        {
          "path": "/var/runtime",
          "name": "test_lambda_runtime_client",
          "version": null
        },
        {
          "path": "/var/runtime",
          "name": "test_lambda_runtime_marshaller",
          "version": null
        },
        {
          "path": "/var/runtime",
          "name": "urllib3",
          "version": "1.24.1"
        },
        {
          "path": "/var/lang/lib/python3.7",
          "name": "__future__",
          "version": null
        },
    ...
    

    What I discovered was also different from what they officially publish. At the time of writing this:

    • Operating system – Amazon Linux
    • AMI – amzn-ami-hvm-2017.03.1.20170812-x86_64-gp2
    • Linux kernel – 4.14.77-70.59.amzn1.x86_64
    • AWS SDK for JavaScript – 2.290.0\
    • SDK for Python (Boto 3) – 3-1.7.74 botocore-1.10.74
    0 讨论(0)
提交回复
热议问题