Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8289855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:37:12+00:00 2026-06-08T12:37:12+00:00

I have several modules that are within a Python package: # my_package contents __init__.py

  • 0

I have several modules that are within a Python package:

# my_package contents
__init__.py
module1.py
module2.py

Within my __init__.py, I’m importing these modules so that they will be accessible once the package is imported by my users.

# __init__.py
import module1
import module2

My question is: how do I programatically access the docstrings for each of my defined functions within each of these modules? I’ve seen others use a form of this:

getattr(module, key). __doc__

but I can’t get it to work for me. Any ideas?

EDIT: A little more background… we’re trying to extract content (one of the important things is the docstrings) from our python packages with the intent of using that as content for documentation. My boss has something already set up that we’re trying to feed into.

Ideally, I’d like to have a package.module.function docstring result

EDIT2: Here’s what is currently not working:

#my package is named 'tpp'
import tpp

for script in dir(tpp):
    if not "__" in script: #not a builtin...
        docstrings1 = getattr( tpp, script).__doc__
        docstrings2 = " ".join(docstrings1.split())#clean out any newline chars
        print script, docstrings

EDIT3: To get a picture of where the docstrings are and how we’ve organized things:

import inspect
import tpp

inspect.getdoc(tpp)
#returns None

inspect.getdoc(tpp.module1)
#returns None

inspect.getdoc(tpp.module1.function1)
#'DOCSTRING TEXT FOUND!'

**Ultimately, I’d like to get a list like [‘module1’, ‘function1’, ‘DOCSTRING TEXT FOUND!’]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T12:37:14+00:00Added an answer on June 8, 2026 at 12:37 pm

    Maybe you want something like this:

    for script in dir(tpp):
        if not "__" in script: #not a builtin...
            docstrings1 = getattr( tpp, script).__doc__
            if docstrings1:  #objects without docstrings return None above, which can't be split.
                docstrings2 = " ".join(docstrings1.split())#clean out any newline chars
                print script, docstrings2
    

    but I don’t guarantee that this will get all the docstrings. You may need to recursively go into items that you retrieve with getattr.

    Here’s a recursive version (which probably will get way more than you want) and will choke on circular dependencies:

    def get_all_doc(obj,indent=''):
        for item in filter(lambda x:not x.startswith('__'),dir(obj)):
            o=getattr(obj,item)
            print "{indent}{item} {doc}".format(indent=indent,
                                                item=item,
                                                doc=o.__doc__)
            get_all_doc(o,indent=indent+'   ')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several Python projects and they all have a conf package: /some_folder/project_1/ conf/
I have a python-based GTK application that loads several modules. It is run from
I have a parent POM with several child modules. For one of these I
Background: I work in a suite of ASP.NET applications that have several different modules.
I have a package that has several components in it that would benefit greatly
I'm writing an application that consists of several maven modules. All of them have
I have several modules (mainly C) that need to be redesigned (using C++). Currently,
I'm working on a site, which will have several modules that either fully available
I have seen several modules (example: Iterator::Simple ) that make use of Perl's angle
I have several test modules that are all invoked together via a driver script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.