警告:autodoc:无法导入模块“ OCR”;引发了以下异常:没有名为“ OCR”的模块

时间:2019-08-03 08:37:18

标签: python python-sphinx

我在让'autodoc'读取Python文件(OCR.py)时遇到问题。 我不知道问题可能是什么。也许有人可以帮忙。 非常感谢您的阅读和建议!

Folder Structure:

AutoQA
  Docu
    _build
    QALibs
      Automation.rst
    conf.py
    index.rst
    make.bat
  QALibs
    Automation
      Automation.py
      OCR.py

如果我将OCR.py文件移动到AutoQA/QAlibs/OCR,那么它将起作用。但是我不想这样做,因为它属于自动化文件夹。


Automation.rst:

Automation
==========

.. automodule:: Automation
    :members:
    :undoc-members:
    :inherited-members:

.. automodule:: OCR
    :members:
    :undoc-members:
    :inherited-members:

conf.py:

import sys, os
sys.path.append(os.path.abspath('../QALibs'))

project = 'AutoQA'

extensions = [
    'sphinx.ext.autodoc'
]

html_theme = "sphinx_rtd_theme"

index.rst:

.. toctree::
    :maxdepth: 4
    :caption: QALibs Documentation

    QALibs/Automation

OCR.py ( Excerpt ):

'''
Module **OCR** provides Tools for Screen Reading.
Uses `Tesseract OCR`_.

.. _Tesseract OCR: https://github.com/tesseract-ocr/tesseract
'''
import pytesseract, pyautogui, sys, cv2
sys.path.append('../..')
from QALibs.Automation import Automation
from QALibs.Log import Log


@staticmethod
def findText(text, getRightPosition=False):
...

我还认为可能是因为这里的sys.path.apend。但是,无论是否使用此Line,都没有任何区别。


Sphinx 'make html' Output after 'make clean':

Removing everything under '_build'...
Running Sphinx v2.1.2
making output directory... done
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 23 source files that are out of date
updating environment: 23 added, 0 changed, 0 removed
reading sources... [100%] py-modindex
WARNING: autodoc: failed to import module 'OCR'; the following exception was raised:
No module named 'OCR'
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [100%] py-modindex
generating indices... genindex py-modindex
writing additional pages... searchc:\users\dschiller.iet\appdata\local\programs\python\
  python37-32\lib\
  site-packages\sphinx_rtd_theme\search.html:20: RemovedInSphinx30Warning: 
  To modify script_files in the theme is deprecated. Please insert a 
  <script> tag directly in your theme instead.
  {{ super() }}

copying images... [100%] AutoQA\../Images/AutoQA/Initializer.png
copying static files... done
copying extra files... done
dumping search index in English (code: en) ... done
dumping object inventory... done
build succeeded, 9 warnings.

The HTML pages are in _build\html.

1 个答案:

答案 0 :(得分:0)

感谢mzjn's评论这是解决方案:

Automation.rst:

Automation
==========

.. automodule:: Automation.Automation  # << The Module also need the Package Name
    :members:
    :undoc-members:
    :inherited-members:

# .. automodule:: OCR  # << This is invalid; The Module also need the Package Name
.. automodule:: Automation.OCR
    :members:
    :undoc-members:
    :inherited-members:
OCR.py ( Excerpt ):

'''
Module **OCR** provides Tools for Screen Reading.
Uses `Tesseract OCR`_.

.. _Tesseract OCR: https://github.com/tesseract-ocr/tesseract
'''
import pytesseract, pyautogui, sys, cv2
sys.path.append('../..')
sys.path.append('..')  # << Is needed; Otherwise 'QALibs' not found within Sphinx
from QALibs.Automation import Automation
from QALibs.Log import Log


# @staticmethod  # <<< This is invalid here; Only useable in Classes
def findText(text, getRightPosition=False):
...

代替sys.path.append('..')行,也可以将__init__.py文件与Automation文件夹一起使用。

QALibs/Automation/__init__.py

import sys
sys.path.append('..')  # Used for Sphinx