1

I am having an issue with Sphinx which seems to fail on an import in a python file.

I tried to add an autodoc_mock_imports in my conf.py to avoid this error but without success:

autodoc_mock_imports = ['test.lib.labjackue9']

For information, when I comment the line import test.lib.labjackue9 as labjack in test.src.led.py, the documentation is generated without error!

Do you have any ideas?

Here is the error message:

<WORKING FOLDER>/test/doc/source/test.src.rst:34: WARNING: autodoc: failed to import module 'test.src.led'; the following exception was raised:
Traceback (most recent call last):
  File "<WORKING FOLDER>/test/lib/labjackue9.py", line 9, in <module>
    labjack_id = ue9.UE9()
  File "<WORKING FOLDER>/test/lib/labjack/ue9.py", line 92, in __init__
    self.open(**kargs)
  File "<WORKING FOLDER>/test/lib/labjack/ue9.py", line 111, in open
    Device.open(self, 9, Ethernet = ethernet, firstFound = firstFound, serial = serial, localId = localId, devNumber = devNumber, ipAddress = ipAddress, handleOnly = handleOnly, LJSocket = LJSocket)
  File "<WORKING FOLDER>/test/lib/labjack/LabJackPython.py", line 616, in open
    d = openLabJack(devType, ct, firstFound = True, handleOnly = handleOnly, LJSocket = LJSocket)
  File "<WORKING FOLDER>/test/lib/labjack/LabJackPython.py", line 1388, in openLabJack
    handle = _openLabJackUsingExodriver(deviceType, firstFound, pAddress, devNumber)
  File "<WORKING FOLDER>/test/lib/labjack/LabJackPython.py", line 1276, in _openLabJackUsingExodriver
    raise NullHandleException(info)
LabJackPython.NullHandleException: Couldn't open device. Please check that the device you are trying to open is connected.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/sphinx/ext/autodoc.py", line 658, in import_object
    __import__(self.modname)
  File "<WORKING FOLDER>/test/src/led.py", line 7, in <module>
    import test.lib.labjackue9 as labjack
  File "<WORKING FOLDER>/test/lib/labjackue9.py", line 11, in <module>
    raise ConnectionError("LabJack UE9 did not answered")
ConnectionError: LabJack UE9 did not answered

Here is my project folders:

+- test
|
+---- src
+------- __init__.py
+------- led.py
+------- battery.py
+------- const.py
+------- ...
|
+---- lib
+------- __init__.py
+------- labjack
+------------ __init__.py
+------------ LabJackPython.py
+------------ ue9.py
+------- generic.py
+------- labjackue9.py
+------- labjackue9_config.py
+------- ...
|
+---- doc
+------- Makefile
+------- build (folder with HTML files)
+------- source
+---------- conf.py
+---------- index.rst
+---------- ...

EDIT:

Here is the beginning of test.src.led.py:

import os
import time
from threading import Thread
import test.lib.generic as generic
import test.lib.labjackue9 as labjack

from test.src.const import *
from test.lib.labjackue9_config import *

class led(Thread):
   def __init__(self):
...

Here is test.src.rst

test.src package
================

Submodules
----------

test.src.led module
-------------------

.. automodule:: test.src.led
    :members:
    :undoc-members:
    :show-inheritance:

test.src.battery module
-----------------------

.. automodule:: test.src.battery
    :members:
    :undoc-members:
    :show-inheritance:

test.src.const module
---------------------

.. automodule:: test.src.const
    :members:
    :undoc-members:
    :show-inheritance:


Module contents
---------------

.. automodule:: test.src
    :members:
    :undoc-members:
    :show-inheritance:

EDIT2:

Here is test.lib.labjackue9.py:

import sys
sys.path.append("<WORKING FOLDER>/test/lib/labjack")
import ue9

try:
    labjack_id = ue9.UE9()
except Exception:
    raise ConnectionError("LabJack UE9 not reachable")


def labjackid():
    return labjack_id
Berthier
  • 36
  • 6
  • You aren't showing us the source code that causes the error (neither the `.py` nor the corresponding `.rst` where autodoc is used) so there's really no way to know. Please edit the question to include a [*"Minimal, Reproducible, Example."*](https://stackoverflow.com/help/minimal-reproducible-example) – bad_coder Feb 19 '21 at 16:19
  • Python only imports packages, and that requires a `__init__.py` file in the same directory as the module you try to document, unless you use namespaces. – Steve Piercy Feb 20 '21 at 02:09
  • There are a few python-sphinx threads where mock caused problems, like [this one](https://stackoverflow.com/q/62651201) but it's hard to answer without at least the specific lines that cause the error. Without code it's almost a guessing game and probably won't hold much value for future readers. (The thread I linked took the OP 3 posts with our feedback before it was possible to write-up the problem into a question that could be answered.) – bad_coder Feb 20 '21 at 04:47
  • Thanks for your replies! I just updated my question according to your requests. – Berthier Feb 22 '21 at 11:12
  • we need the lines around ` File "/test/lib/labjackue9.py", line 11, in ` – thebjorn Feb 22 '21 at 11:15
  • I showed the content of /test/lib/labjackue9.py – Berthier Feb 22 '21 at 12:11

1 Answers1

0

After connecting the LabJack module to the PC, the problem disappeared and the documentation generation is now successful. I do not really understand why connecting the LabJack module changed the way Sphinx reads the source code, but it seems to work just fine.

Berthier
  • 36
  • 6
  • This might be related to https://stackoverflow.com/q/12391390/407651. I am not familiar with LabJackPython, but a guess is that it contains module-level code that does not work unless some hardware is connected. – mzjn Feb 22 '21 at 14:00