4

Recently, in attempting to import a module I'd written, I have been coming across errors that ostensibly should not happen. Here is the idea, I'm writing in my main.py file, and my hierarchy looks like this:

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

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

import parser

it returns an AttributeError. This happens when I call the function inside of parse.py, called getstar(). or

from parser import parse

it returns an ImportError.

So my Python doesn't recognize that parse.py exists? I have done a bit of research, and having an __ init __.py file that's empty should do the trick, but I'm stumped.

4
  • 3
    The AttributeError is because modules in a package aren't attributes of the package -- you have to import them yourself. Commented Aug 18, 2012 at 1:43
  • 1
    You could always do a brute-force import like: import sys sys.path.append('parser') import parser Commented Aug 18, 2012 at 2:20
  • You could be more precise as to what are the error messages returned (and not just their type)? Commented Aug 18, 2012 at 10:36
  • @mjgpy3 - super hacky dude. Even for python =) Commented Aug 18, 2012 at 10:46

1 Answer 1

1

parser is a name of a build-in module in python. when you write

import parser

You import the built-in module. Since that module does not contain the getstar() function or a parse module, you get either AttributeError or ImportError

Try changing the name of the "parser" directory to anything else and it should work. The empty init.py file is not needed

Sign up to request clarification or add additional context in comments.

1 Comment

Worked! Thank you, I was beating my head against a wall trying to figure out what was wrong. Would anyone happen to know if there's a list of do's and don't's for naming modules in Python?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.