1

I've only started my project and already got a problem. File parser.py contains following code and nothing more:

class A:
    def __init__(self):
        self.x = 0

for file parser_tests.py I tried following combinations: 1)

import unittest


class ParserTests(unittest.TestCase):
    def one(self):
        from parser import A
        x = A()
        self.assertTrue(True)

2)

import unittest
from parser import A


class ParserTests(unittest.TestCase):
    def one(self):
        x = A()
        self.assertTrue(True)

3)

import unittest
import parser


class ParserTests(unittest.TestCase):
    def one(self):
        x = parser.A()
        self.assertTrue(True)

But all of them lead to AttributeError: 'module' object has no attribute 'A' or ImportError: cannot import name 'A'

How to fix this?

  • 1
    Where is your file located, and where are you running it from? There is a builtin Python module called `parser`, which is likely what you're importing instead of your own file. – BrenBarn May 07 '17 at 19:37

1 Answers1

1

parser is a python standard library module: https://docs.python.org/3/library/parser.html

You'll have to rename your module to something else

anthony sottile
  • 61,815
  • 15
  • 148
  • 207