Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an ldd like command for python

Tags:

python

This is the problem: You try to run a python script that you didn't write yourself, and it is missing a module. Then you solve that problem and try again - now another module is missing. And so on.

Is there anything, a command or something, that can go through the python sources and check that all the necessary modules are available - perhaps even going as far as looking up the dependencies of missing modules online (although that may be rather ambitious)? I think of it as something like 'ldd', but of course this is much more like yum or apt-get in its scope.

Please note, BTW, I'm not talking about the package dependencies like in pip (I think it is called, never used it), but about the logical dependencies in the source code.

like image 221
j4nd3r53n Avatar asked Aug 10 '26 19:08

j4nd3r53n


2 Answers

There are several packages that analyze code dependencies:

https://docs.python.org/2/library/modulefinder.html

Modulefinder seems like what you want, and reports what modules can't be loaded. It looks like it works transitively from the example, but I am not sure.

https://pypi.org/project/findimports/

This also analyzes transitive imports, I am not sure however what the output is if a module is missing.

... And some more you can find with your favorite search engine

like image 92
kutschkem Avatar answered Aug 13 '26 10:08

kutschkem


To answer the original question more directly, I think...

lddcollect is available via pip and looks very good.

Emphases mine:

Typical use case: you have a locally compiled application or library with large number of dependencies, and you want to share this binary. This tool will list all shared libraries needed to run it. You can then create a minimal rootfs with just the needed libraries. Alternatively you might want to know what packages need to be installed to run this application (Debian based systems only for now).

There are two modes of operation.

  1. List all shared library files needed to execute supplied inputs
  2. List all packages you need to apt-get install to execute supplied inputs as well as any shared libraries that are needed but are not under package management. In the first mode it is similar to ldd, except referenced symbolic links to libraries are also listed. In the second mode shared library dependencies that are under package management are not listed, instead the name of the package providing the dependency is listed.
lddcollect --help
Usage: lddcollect [OPTIONS] [LIBS_OR_DIR]...

  Find all other libraries and optionally Debian dependencies listed
  applications/libraries require to run.

  Two ways to run:

  1. Supply single directory on input
     - Will locate all dynamic libs under that path
     - Will print external libs only (will not print any input libs that were found)
  2. Supply paths to individual ELF files on a command line
     - Will print input libs and any external libs referenced

  Prints libraries (including symlinks) that are referenced by input files,
  one file per line.

  When --dpkg option is supplied, print:

    1. Non-dpkg managed files, one per line
    2. Separator line: ...
    3. Package names, one per line

Options:
  --dpkg / --no-dpkg  Lookup dpkg libs or not, default: no
  --json              Output in json format
  --verbose           Print some info to stderr
  --ignore-pkg TEXT   Packages to ignore (list package files instead)
  --help              Show this message and exit.

I can't test it against my current use case for ldd right now, but I quickly ran it against a binary I've built, and it seems to report the same kind of info, in fact almost twice as many lines!

like image 37
underscore_d Avatar answered Aug 13 '26 10:08

underscore_d



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!