简体   繁体   English

将.py文件作为模块导入时没有显示文档字符串?

[英]Docstring not showing up when importing .py file as a module?

I am working on a project and I am beginning to write functions in other.py files and importing them to the main.py... I want to write help text for the entire file ( module ) that I am importing, similar to when importing a library ( module ) like this:我正在做一个项目,我开始在 other.py 文件中编写函数并将它们导入到 main.py... 我想为我正在导入的整个文件(模块)编写帮助文本,类似于何时像这样导入库(模块): 将鼠标悬停在 vs 代码中的 tempfile 模块上

When I try writing something similar at the top of the file I'm importing, the docstring doesn't show up.当我尝试在要导入的文件顶部编写类似的内容时,文档字符串不会显示。

Trying this:试试这个: 尝试在我正在导入的文件中写入文档字符串

But getting this (with no docstring showing):但是得到这个(没有显示文档字符串): 将鼠标悬停在我尝试导入的 .py 文件上

It is because of your docstring format probably.这可能是因为您的文档字符串格式。 Try to use such formatting:尝试使用这样的格式:

"""Helpful docstring for a .py file of functions."""

Then, if VS Code still doesn't show a docstring when a module hovering, use Reload Window command after saving your file (ie Ctrl+Shift+P and enter 'reload window').然后,如果 VS Code 在模块悬停时仍然不显示文档字符串,请在保存文件后使用Reload Window命令(即Ctrl+Shift+P并输入“重新加载窗口”)。 You can use pydocstyle linter also.你也可以使用pydocstyle linter It says, for example, that "One-line docstring should fit on one line with quotes".例如,它说“单行文档字符串应该适合带引号的一行”。 The linter is supported by Python VS Code extension. Python VS 代码扩展支持 linter。 But be sure, that python.linting.pydocstyleEnabled option is enabled and pydocstyle is installed itself.但可以肯定的是, python.linting.pydocstyleEnabled选项已启用,并且pydocstyle已自行安装。 You can install it globally or locally for your virtual environment.您可以为您的虚拟环境全局或本地安装它。 I prefer the global installation - pip install pydocstyle .我更喜欢全局安装 - pip install pydocstyle Also the extension checks, whether all enabled linters are installed and can help to install them.扩展程序还检查是否安装了所有启用的 linter 并可以帮助安装它们。

Re: Solved解决

As suggested in the comments both ' ' and " " are equivalent.正如评论中所建议的那样,“”和“”都是等价的。 The problem had to do with reloading the window as suggested with Ctrl+Shift+P and 'reload window'.问题与使用Ctrl+Shift+P和“重新加载窗口”的建议重新加载 window 有关。 文档字符串工作 The docstring from importing a file must not directly update when changed导入文件的文档字符串在更改时不得直接更新

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

相关问题 导入使用CSV模块创建的文件时Excel显示空单元格 - Excel showing empty cells when importing file created with csv module 导入 SMOTE 时出现不平衡学习模块 base.py 文件语法错误 - Imbalances-learn module base.py file syntax error coming up while importing SMOTE 导入 py 文件错误:没有名为“文件名”的模块 - importing py file error: no module named 'filename' 从内部模块导入外部模块的.py文件 - Importing .py file of outer module from inner module 在模块中导入模块时出现Web.py NameError - Web.py NameError When Importing Module in Module 导入 local.py 文件时出现 ModuleNotFoundError - ModuleNotFoundError when importing local .py file 在命令行上导入模块后无法打印文档字符串 - Trouble printing a docstring after importing module on command line Python 模块使用前导下划线导入自身,但没有对应的 .py 文件 - Python module importing itself with leading underscore but there is no corresponding .py file 从.py文件中获取包根和完整模块名称以进行导入 - Get package root and full module name from .py file for importing 在 another.py 中导入模块时,返回 function 和直接运行不一样? - When importing a module in another .py , return of function is different to directly run it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM