Michael B. Currie
Michael B. Currie

Reputation: 14656

How to access a Python package's docstring?

Let's say I have a package omegaproject I'm importing from a script test.py:

test.py
omegaproject/
  __init__.py
  omega.py

Say that in __init__.py I've put """Hello there""" and nothing else.

Now, say that test.py consists of:

import omegaproject 

print(omegaproject.__doc__)

Shouldn't running test.py cause Python to display 'Hello there'? Instead, it displays nothing.

In other words, where do I specify the docstring of a package?

Upvotes: 0

Views: 108

Answers (1)

Michael B. Currie
Michael B. Currie

Reputation: 14656

Let's call this a "trick question" :)

As Martijn points out, in fact the docstring does appear as desired.

The problem was in this sequence of events:

  1. I originally had __init__.py empty.
  2. Then I ran test.py, which printed nothing.
  3. Then I edited __init__.py to have a docstring reading """Hello there"""
  4. Then on the interactive shell I ran: import omegaproject followed by omegaproject.__doc__

But omegaproject had already been imported! So the second import did nothing, the docstring did not get updated, and so the docstring was still showing as blank.

Upvotes: 1

Related Questions