简体   繁体   English

pydoc 文档应该自动创建文件吗?

[英]Should pydoc document automatically created file?

I try to understand how pydoc works, and wanted to have it display the docstring of a file.我试图了解pydoc工作原理,并希望让它显示文件的文档字符串。

I have an unaltered file urls.py (created using Django, but that seems irrelevant for my pydoc question):我有一个未更改的文件urls.py (使用 Django 创建,但这似乎与我的pydoc问题无关):

"""box_whiskers_demo URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls),
]

But the following command issued in a python prompt但是在python提示符下发出以下命令

import urls

gives me the error message django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.给我错误消息django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. . . What I expected was that Similarly, the command我所期望的是,同样,命令

from urls import urlpatterns

gives me exactly the same error message.给了我完全相同的错误信息。

I supposed that I could get Python to read the file, then access the docstring, as I can with我想我可以让 Python 读取文件,然后访问文档字符串,就像我可以的那样

import pydoc
help(pydoc)

The latter outputs meaningful, error-free documentation.后者输出有意义的、无错误的文档。 Knowing that the file urls.py is no module, I read知道文件urls.py不是模块,我读了

If the argument to pydoc looks like a path (that is, it contains the path separator for your operating system, such as a slash in Unix), and refers to an existing Python source file, then documentation is produced for that file.如果 pydoc 的参数看起来像一个路径(也就是说,它包含操作系统的路径分隔符,例如 Unix 中的斜杠),并且引用了现有的 Python 源文件,则为该文件生成文档。

in the pydoc documentation , and "that file" has a fine, multi-line docstring.pydoc 文档中,“那个文件”有一个很好的多行文档字符串。

Does the error imply, that "that file" is neither a错误是否意味着“那个文件”既不是

name of a function, module, or package函数、模块或包的名称

or does the error originate somewhere else?还是错误源自其他地方? Should I expect the docstring as output from any of these pydoc call at all?我应该期待 docstring 作为这些pydoc调用中的任何一个的输出吗?

I think I managed to nail down the problem: As Python looked for the admin package, and did not find it, the problems started.我想我设法确定了这个问题:当 Python 寻找admin包但没有找到时,问题就开始了。 Thus with commenting out the statement calling for admin.因此,注释掉调用admin.的语句admin. , using , 使用

urlpatterns = [
#    path('admin/', admin.site.urls),
]

My call from bash我来自bash电话

pydoc urls

gives me beautiful docstring:给了我漂亮的文档字符串:

Help on module urls:

NAME
    urls - box_whiskers_demo URL Configuration

DESCRIPTION
    The `urlpatterns` list routes URLs to views. For more information please see:
        https://docs.djangoproject.com/en/3.1/topics/http/urls/
    Examples:
    Function views
        1. Add an import:  from my_app import views
        2. Add a URL to urlpatterns:  path('', views.home, name='home')
    Class-based views
        1. Add an import:  from other_app.views import Home
        2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
    Including another URLconf
        1. Import the include() function: from django.urls import include, path
        2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))

DATA
    path = functools.partial(<function _path at 0x7f3c9b88d...ern=<class '...
    urlpatterns = []

FILE
    /home/morten/Dropbox/Python/django/box_whiskers_demo/box_whiskers_demo/urls.py

(END)

NB I get the exact same output with the following sequence from inside a python command:注意,我从 python 命令中获得了完全相同的输出,并且具有以下序列:

import urls
help(urls)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM