0

I have a file parser.py

class Parser:
    ...
    ...

    @staticmethod
    def someMethod():
        Parser.argsParser.someNewMethod()

And a file worker.py

import connection, parser
...
...

class Worker:
    def __init__(self):
        try:
            parsed = parser.Parser()

And a file driver.py:

sys.path.append('./lib')
import worker, parser

parser.Parser.someMethod()

Now my directory structure is

/some/path/driver.py
/some/path/lib/worker.py
/some/path/lib/parser.py

When I run driver.py I get:

Traceback (most recent call last):
  File "./lib/worker.py", line 13, in __init__
    parsed = parser.Parser()
AttributeError: 'module' object has no attribute 'Parser'
'module' object has no attribute 'Parser'

However, when I copy driver.py into ./lib and run, I do not find any problems. Can anyone point me to what is going on?

3 Answers 3

1

There is a builtin Python module called parser, which is being imported instead of your module. The best solution is don't name your module that, or, if you do, put it inside a package so you don't import it directly at the top level (i.e., so you do import mypackage.parser instead of import parser).

1

First make sure you have a __init__.py file in your lib directory. Then you can import the Parser class in your driver.py file as follows:

from lib.parser import Parser

The __init__.py file lets the python interpreter know that the directory is to be considered a python module (https://docs.python.org/2/tutorial/modules.html).

1
  • thanks! this clarifies some of the usage of init.py, I however have to accept the other answer as it was more accurate to the question :) Commented Sep 3, 2014 at 22:22
-1

Set your PYTHONPATH properly:

export PYTHONPATH=/some/path:/some/path/lib
1
  • I think it has nothing to do with paths, the problem is not being able to find the class "Parser" and NOT the file "parser.py" Commented Sep 3, 2014 at 22:09

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.