Attributeerror: 'Directores' Object has no attribute

Asked

Viewed 347 times

0

I am trying to get a value of a very simple class, but I have a problem that I cannot solve. Follow 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 class Directors:

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 will be at your disposal. And if you give me some tips I would also appreciate it very much!

  • 1

    We have no way to test your code, as there are dependencies that are not explicit in the question, such as String, arcpy and Parameters, however, in a basic test, removing these dependencies, its code did not reproduce the cited error: https://repl.it/@acwoss/Sweetadorableinterface. Could then [Dit] and create a [mcve] that demonstrates the error?

1 answer

1

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

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

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

This will print the file path directores.py that he is importing.

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

  1. Use Python 3. It doesn’t make sense to start a project with Python 2.7 today - it’s a 10-year-old version of the language, and it comes out in a year and two months. You lose several improvements - mainly regarding the treatment of accentuated text. Even if you need Python 2.7 for other purposes on the same computer, the use of virtualenv allows you to have Python 2 and Python 3 projects in parallel on the same machine without any ambiguity
  2. Use your names consistently. The Python style guide recommends using "snake_case" names: that is, words in function and variable names are separated with "_" - and only class names in "Camelcase". You are not required to follow the style conventions - but mix underscore with Camel case within of the same names (seriously, it hurts my eyes just to look at)
  3. If you’re using strings prefixed with "r", you don’t have to fold \\ to separate directories - a single \ is enough. But better yet, in Python you can use the forward bar to separate directories - / even on Windows. It is a more universal form and its code is not system dependent. Better yet: use Python 3.7 (see tip 1) and use pathlib instead of strings for directory paths. https://docs.python.org/3/library/pathlib.html
  4. Do not use "getters" and "setters" if you will not do anything with the value within these methods. In Python all attributes are public except by convention. If all your method getInput_folder_SRTM will do is return self.Input_folder_SRTM, you do not have to call the method - access the direct attribute in the instance with directores.Input_folder_SRTM in 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 worry about the Property only in another part of your project and add getters and setters without changing any of the code that uses the attribute.
  5. 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 quite expensive to be due to older versions of the project scattered around the computer. With git, you end up having multiple versions of the same code in different folders, as you learn to use it. And with virtualenv you isolate exactly which packages and modules are installed for each Python project - avoiding interference between different versions.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.