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

前端 未结 30 2593
庸人自扰
庸人自扰 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: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

提交回复
热议问题