Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python import error: 'Cannot import name'

Im having trouble importing a class on a python module.

Here are my directory structure:

_wikiSpider
  +scrapy.cfg
  _wikiSpider
    +__init__.py
    +items.py
    +items.pyc
    +settings.py
    +settings.pyc
    +pipelines.py
    _spiders
     +__init__.py
     +__init__.pyc
     +articleSpider.py
     +articleSpider.pyc
     +items.py

Code breaks at this line:

from wikiSpider.items import Article

Im not sure why, since class Article is defined at items.py (deepest folder)

Can someone give me an explanation?

like image 564
Jacs Avatar asked Nov 27 '25 00:11

Jacs


1 Answers

Like others, I didn't have a circular reference problem. I'd like to generalize the solution here just a bit more though.

Any file name conflict can cause this. You could have multiple sub files with the same name (as above).

Or it could be the file you're working in.

Eg: trello.py as a pet project. from trello import TrelloApi

Import reference will import itself before importing the pip installed package. Attempts to import trello and reference objects directly will fail with "NameError: name '' is not defined"

like image 86
Beweeted Avatar answered Nov 29 '25 15:11

Beweeted