Python package import error - Python does not recognize the package

Recently, while trying to import a module that I wrote, I came across errors that supposedly should not occur. Here is the idea that I write in my main.py file, and my hierarchy is as follows:

 starsearch/ main.py parser/ __init__.py parse.py 

the __ init __.py in parser / is empty, but when I try in my program:

 import parser 

returns an AttributeError . This happens when I call a function inside parse.py called getstar() . or

 from parser import parse 

returns an ImportError .

So my Python does not recognize that parse.py exists? I did a bit of work, and the __ init __.py , which is empty, should do the trick, but I'm at a dead end.

+4
source share
1 answer

parser is the name of the built-in module in python. when you write

 import parser 

You import the built-in module. Since this module does not contain the getstar () function or the parsing module, you get either the AttributeError attribute or ImportError

Try changing the name of the parser directory to something else, and it should work. Empty init .py file not required

+1
source

All Articles