Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportError: cannot import name ABC #4059

Closed
LustigesPeterle opened this issue Sep 15, 2017 · 2 comments
Closed

ImportError: cannot import name ABC #4059

LustigesPeterle opened this issue Sep 15, 2017 · 2 comments

Comments

@LustigesPeterle
Copy link

LustigesPeterle commented Sep 15, 2017

Subject: sphinx has trouble importing Python Standard Library component abc

Problem

  • when I execute the make html command, I encounter an error. I have a python projct, that uses abstract classes and therefore I'm using the standard library module abc.
    Sphinx has no problem importing any other standard library component like os or pickle, that are in the same directory /usr/lib/python[version] with [version] being 2.7, 3.5 or any other python version installed.
  • The html will have more or less empty pages.
  • Prior to the make html command I use sphinx-apidoc
  • conf.py path is set to the correct directory (it doesn' have any other problems importing standard library components or custom modules).

Procedure to reproduce the problem

Minimal example:

###################
# abstractNode.py 
###################
from abc import ABC, abstractclassmethod

class abstractNode(ABC):
    '''
    abstract parent
    '''
    @abstractmethod
    def moduleNode(self, parameter=[]):
        '''
        Implement Node Functionality here
        '''
        pass
###################
# Node.py 
###################
from foo.bar import abstractNode

class Node(abstractNode):
    '''
    implementation of abstract parent
    '''
    def moduleNode(self, parameter=[]):
        '''
        implementation written here
        '''
        pass

Error logs / results

File "/usr/lib/python2.7/dist-packages/sphinx/ext/autodoc.py", line 529 in import_object
    __import__(self.module)
File "/home/pi/.../abstractNode.py", line 6, in <module>
    from abc import ABC, abstractclassmethod
ImportError: cannot import name ABC

File "/usr/lib/python2.7/dist-packages/sphinx/ext/autodoc.py", line 529 in import_object
    __import__(self.module)
File "/home/pi/.../Node.py", line 11, in <module>
    from foo.bar import abstractNode
File "/home/pi/.../abstractNode.py", line 6, in <module>
    from abc import ABC, abstractclassmethod
ImportError: cannot import name ABC

Environment info

  • OS: Linux - raspbian
  • Python version: 3.5
  • Sphinx version: 1.4.9
@LustigesPeterle
Copy link
Author

Although ABC and abstract classes are defined in 2.7 documentation, and are present in /usr/lib/python2.7, you must force sphinx to use python 3.x

Unfortunately I have not found an elegant method to do so, but my method descibed below solves the problem:

  1. Uninstall sphinx

  2. Install sphinx with python3:

sudo apt-get install python3-sphinx

or

sudo pip3 install sphinx

(in contrast to python-sphinx and pip install)

  1. Create a sphinx documentation the way you are used to

(maybe someone has a better idea to force sphinx to use python 3.x?)

@tk0miya
Copy link
Member

tk0miya commented Oct 4, 2017

sorry for late response. abc.ABC appears since py3.4. So you need to build your docs with python3 because autodoc imports the module on building.

(maybe someone has a better idea to force sphinx to use python 3.x?)

Please install sphinx with pip3 command.

Thanks,

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants