问题
There's any way to exclude the import part of a module and then document it with sphinx-python? I have a module that imports another package (other different project) and then the sphinx gives this error:
""" File "/usr/local/lib/python2.7/dist-packages/Sphinx-1.1.3-py2.7.egg/sphinx/ext/autodoc.py", line 321, in import_object import(self.modname) File "/home/x/GitHub/project/mod_example1.py", line 33, in from other_pck import Klass, KlassStuff ImportError: No module named other_pck """
And if I comment the import parts in the module that calls/imports that package the sphinx can do the autodoc. I tried with all the sphinx autodoc modules: autoclass, automodule, etc... but the result is always the same once it try's to import the other package.
Thanks
回答1:
You are fixing the issue wrong way. The correct way to fix the issue is to make Sphinx aware of your existing other packages as autodoc functionality must import Python packages to scan the source code. Python packages cannot be imported without all their dependencies resolved and you cannot cherry-pick lines of source code out of it, because this is how Python is built(*)
Possible solutions are
Creating a Python virtualenv environment where both Sphinx and the other packages reside, so that they can see each other http://opensourcehacker.com/2012/09/16/recommended-way-for-sudo-free-installation-of-python-software-with-virtualenv/
Setting PYTHONPATH environment variable or editing sys.path in Sphinx config file, so that missing packages are added in the import list when Sphinx is run http://scienceoss.com/minimal-sphinx-setup-for-autodocumenting-python-modules/
*) In theory you can, but this is outside the scope of Sphinx and this question
来源:https://stackoverflow.com/questions/15088792/sphinx-autodoc-dies-on-importerror-of-third-party-package