1

I need to write a script that reads a certain module (not as an import) and yields a list of its function names.

Something like that:

# script_to_be_listed.py

def function1():
    pass

def function2():
    pass

Where this:

python my_reader_script.py script_to_be_listed.py

Should print this:

function1
function2

I have already tried using inspect and dir but it only works if I import the module (which I don't know exactly what will be given).

If there is no native module to help me out on such task, could you guys suggest me a nice way to do so?

OBS: I'm doing this to automatically create some Lambda functions on AWS and naming them as the functions from script_to_be_listed.py.

1

1 Answer 1

2

To expand on @user2357112's comment, you can use the pyclbr.readmodule_ex function to read a module, iterate through the items of the returning dict, and print the key if the value is an instance of pyclbr.Function:

import pyclbr

for name, obj in pyclbr.readmodule_ex('module_to_be_browsed', ['/path/to/script']).items():
    if isinstance(obj, pyclbr.Function):
        print(name)
Sign up to request clarification or add additional context in comments.

1 Comment

Worked nicely. Thanks for helping me out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.