AttributeError: 'Directors' object has no attribute

0

I'm trying to get a value from a very simple class, but I have a problem that I can not solve. Here are the codes below:

Main class:

from directores.directores import Directores
# A classe Directores encontra-se em directores/directores.py

def main():
    print (Strings.start_description)

    # Sobrescrever arquivos encontrados
    arcpy.env.overwriteOutput = True

    # Criando todos os diretรณrios
    directores = Directores(Parameters.main_folder + r"\")
    print(directores.getInput_folder_SRTM())

The Directors class:

class Directores(object):

    def __init__(self, main_folder):
        # Diretórios secundários de entrada - INPUT
        self.input_folder = main_folder + r"\Input\"
        self.input_folder_SRTM = self.input_folder + r"SRTMs\"

    def getInput_folder_SRTM(self):
        return self.input_folder_SRTM

With the following error:

Runtime error 
Traceback (most recent call last):
  File "<string>", line 50, in <module>
  File "<string>", line 36, in main
AttributeError: 'Directores' object has no attribute 'getInput_folder_SRTM'

In each folder I created an empty file __init__.py

If you need more information, I'll be happy to help. And if you give me some tips I also thank you very much!

    
asked by anonymous 26.09.2018 / 05:39

1 answer

1

The chance is there that you have another version of this project, with another folder directores that contains an old version of the code, and when you do import you are bringing the file from that other folder.

Try to put these lines at the beginning of your file that you call "main class":

from directores import directores as directores_module
print (directores_module.__file__) 

This will print the path of the directores.py file it is importing.

Now the problem may not even be this, but let's take advantage of the other tips you asked for:

  • Use Python 3. It does not make sense to start a project with Python 2.7 today - it's a 10-year-old version of the language that goes out of use in one year and two months. You lose various improvements - especially with regard to accent text handling. Even if you need Python 2.7 for other purposes on the same computer, using virtualenv allows you to have Python 2 and Python 3 projects in parallel on the same machine without any ambiguity.
  • Use their names with consistency. The Python style guide recommends using "snake_case" names: that is, words in function names and variables are separated with "_" - and only class names in "CamelCase". You are not required to follow the conventions of style - but mix underscore with camel case within of the same names (seriously, it hurts your eyes just to look)
  • If you are using prefixed strings with "r", you do not have to double \ to separate directories - a single \ is enough. But better yet, in Python you can use the forward slash to separate directories - / even in Windows. It is a more universal form and your code is not system dependent. Better yet: use Python 3.7 (see tip 1) and use pathlib instead of strings for directory paths. link
  • Do not use getters and setters if you are not going to do anything with the value inside those methods. In Python all attributes are public, except by convention. If everything your getInput_folder_SRTM method is going to do is return self.Input_folder_SRTM , you do not have to call the method - access the direct attribute on the instance with directores.Input_folder_SRTM on your other module. If you want to transform / filter the value when it is set, or transform it when it is used, use property to write the getter and the setter - but realize how cool: you can only worry about the property in another area of your project and add getters and setters without changing any code that uses the attribute. >
  • Use a code version manager such as git and isolated environments, such as virtualenv . It may be that your problem is not what I mentioned above - but it is rather expensive to be due to ancient design versions spread across the computer. With git you end up with this problem of having multiple versions of the same code in different folders as you learn how to use it. And with virtualenv you isolate exactly which packages and modules are installed for each Python project - avoiding interference between different versions.
  • 26.09.2018 / 15:33