Python: Making a class instance gives AttributeError

I have a problem. When I try to make a class instance, I get an error. Here's some code:

import parser

def main():
    tokens = [["TYPE_ONE", "value one"], ["TYPE_TWO", "value two"]]
    parse = parser.Parser(tokens)
    parse.parse()

main()

And parser.py:

class Parser(object):
    def __init__(self, tokens):
        self.tokens = tokens
        self.token_index = 0

    def parse(self):
        while self.token_index < len(self.tokens):
        token_type = self.tokens[self.token_index][0]
        token_value = self.tokens[self.token_index][1]
        print(token_type, token_value)
        self.token_index += 1

But the script gives the following error:

Traceback (most recent call last):
  File "C:/Users/edyal/OneDrive/Desktop/Paigoa/src/main.py", line 8, in <module>
    main()
  File "C:/Users/edyal/OneDrive/Desktop/Paigoa/src/main.py", line 5, in main
    parse = parser.Parser(tokens)
AttributeError: module 'parser' has no attribute 'Parser'

Upvotes: 0

Views: 148

Answers (2)

Grigoriy Mikhalkin
Grigoriy Mikhalkin

Reputation: 5593

From modules documentation:

When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path

List of built-in modules is installation-dependent and, usually, small subset of standard library and it's very unlikely, that parser module is built-in module in your installation(you can check if it's by executing 'parser' in sys.builtin_module_names line).

More likely, problem is in your directory structure. For example, if your directory structure is following:

.
main.py
└── parser
    ├── __init__.py
    └── parser.py

and __init__.py doesn't imports parser.py, to import parser/parser.py you have to execute this code: from parser import parser

Upvotes: 0

heroworkshop
heroworkshop

Reputation: 465

This is a classic gotcha in python: if you name your module something that already exists then you don't get the module you were thinking of. Usually this happens the other way round. For example you call your csv parser csv.py and then inside your try to import csv. You import yourself not the standard python csv module.

In this case it must be the other way round: You want to import your parser module and you instead get the standard parser module. If I import parser I get this:

>>> import parser
>>> dir(parser)
['ParserError', 'STType', '__copyright__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '__version__', '_pickler', 'compilest', 'expr', 'isex
pr', 'issuite', 'sequence2st', 'st2list', 'st2tuple', 'suite', 'tuple2st']

First I recommend renaming your parser.py to something more specific like paigoa_token_parser.py Now change your import:

import paigoa_token_parser

Now you might get an import error and at this point you should check your paths. Is your parser in the same folder as main.py? If not then you might want to add it to the python path

Upvotes: 1

Related Questions