-1

I'm trying to test run my program in C:\reader\compressed\ where I have created two test files 'test.gzip' and 'test.bz2' but when I go run the Reader class I get that the module reader has no attribute 'Reader'.

I'm opening python to "import reader" then "r = reader.Reader('test.bz2')" Directory looks like: enter image description here enter image description here

I can import all the modules I've created.

import reader
import reader.compressed
import reader.compressed.gzipped
import reader.compressed.bzipped

Here is my reader.py:

import os

from reader.compressed import gzipped, bzipped

extension_map = {
    '.bz2': bzipped.opener, 
    '.gz': gzipped.opener,
}

class Reader:
def __init__(self, filename):
    extension = os.path.splitext(filename)[1]
    opener = extension_map.get(extension, open)
    self.f = opener(filename, 'rt')

    def close(self):
        self.f.close()

    def read(self):
        return self.f.read()

I've checked my modules to see that they are there along with the new files to test that are supposed to return the messages.

>>> import reader
>>> r= reader.reader.Reader('test.gz')
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
AttributeError: module 'reader' has no attribute 'reader'

Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "C:\Develop\Python\reader\__init__.py", line 13, in 
 __init__
 self.f = opener(filename, 'rt') 
 File"C:\Python\Python36\lib\gzip.py", line 53, in open
binary_file = GzipFile(filename, gz_mode, compresslevel)
File  
 "C:\Programs\Python\Python36\lib\gzip.py", line 163, in 
 __init__
fileobj = self.myfileobj = builtins.open(filename, mode or 
'rb')
FileNotFoundError: [Errno 2] No such file or directory: 
'test.gz'
April_Nara
  • 1,024
  • 2
  • 15
  • 39

2 Answers2

1

Your file name should not be same as a standard module name. So, rename reader.py to reader1.py or so.

toxdes
  • 421
  • 4
  • 8
1

The Reader class is not defined in a top-level reader module. Rather, it's defined in reader.reader, since the top level reader is a package.

If your current code that's causing the error is:

import reader
r= reader.Reader('test.gzip')

You need to change it to use reader.reader instead of just reader:

import reader.reader
r = reader.reader.Reader('test.gzip')

Or alternatively, you could move the contents of reader/reader.py into reader/__init__.py, since the latter file is where the contents of the reader package are defined. Or you could split the difference, and keep the reader.py file while still making the class available at package level, using something like this in __init__.py (note I've not tested this, there are some weird corner cases with relative imports from __init__.py files):

from .reader import Reader
Blckknght
  • 100,903
  • 11
  • 120
  • 169
  • I've already tried both of those while troubleshooting to still get the problem. – April_Nara Jun 18 '18 at 20:28
  • Please show the code you tried and the full traceback of the exception you get when you run it. If you do have a `reader.Reader` class, or you actually try to use `reader.reader.Reader`, you should at least be getting a different error, even if it doesn't work. – Blckknght Jun 18 '18 at 22:43