0

Hy... lots of questions about relative paths, but none in ubuntu. I think ubuntu is my problem, so please help.. I tried every path combination and have no more ideas what to try to make it work. Program is inporting one module into main window.

This is both..

import sys

sys.path.append("\home\zoran\Documents\Moduli")

import brojeviModul

def vrati(a, b, c):
    global br
    br = 10
    a += br
    b += br
    c += br
    return min(a , b, c)

def main():

    prviBroj = int(input("Unesite prvi broj: "))
    drugiBroj = int(input("Unesite drugi broj: "))
    treciBroj = int(input("Unesite treci broj: "))

    genPrva = brojeviModul.generirajBroj(prviBroj)
    genDruga = brojeviModul.generirajBroj(drugiBroj)
    genTreca = brojeviModul.generirajBroj(treciBroj)

    vratiPrvi = vratiZbroj(genPrva)
    vratiDrugi = vratiZbroj(genDruga)
    vratiTreci = vratiZbroj(genTreca)

    minimalnaVrijednost = vrati(vratiPrvi, vratiDrugi, vratiTreci)

    print("Najmanja vracena vrijednostje{}.".format(minimalnaVrijednost))

if __name__ == "__main__":
main()

And second...

def main():
    global broj

    broj = 100

    veciOdSto = int(input("Unesite troznamenkasti broj veći od 100:"))

    def generirajBroj(veciOdSto):
        if veciOdSto < 101 or veciOdSto > 999:
            print("Broj ne valja")
            return 150
        else:
            import random
            genBroj = random.randint(broj, veciOdSto)
            print("Generiran je slučajni broj: {}".format(genBroj))
            return genBroj

    def vratiZbroj(veciOdSto):
        if veciOdSto > 100 and veciOdSto < 999:
            prvaZnamenka = veciOdSto % 10
            drugaZnamenka = (veciOdSto // 10) % 10
            trecaZnamenka = veciOdSto //100
            return prvaZnamenka + drugaZnamenka + trecaZnamenka

if __name__ == "__main__":
    main()

And error:

 Traceback (most recent call last):
 File "/home/zoran/Desktop/glavni.py", line 34, in <module>
   main()
  File "/home/zoran/Desktop/glavni.py", line 21, in main
  genPrva = brojeviModul.generirajBroj(prviBroj)
AttributeError: module 'brojeviModul' has no attribute 'generirajBroj'

I even added this main class to my module because I find it in one sample..

4
  • 1
    Please fix your indentation, this code is not usable in its current state. Commented Dec 3, 2018 at 19:32
  • Your indentation is off which makes it hard to follow. But im guessing generirajBroj is a nested function in main(). Indentation will be critical to this issue, I feel. Commented Dec 3, 2018 at 19:34
  • However, I'm pretty sure the issue has nothing to do with relative/absolute paths Commented Dec 3, 2018 at 19:35
  • I fixed indentation. My apologize Commented Dec 3, 2018 at 19:47

2 Answers 2

1

The functions in the second module are defined in your main function, not in the module. Reindent your module, like so:

def main():
    global broj
    broj = 100
    veciOdSto = int(input("Unesite troznamenkasti broj veći od 100:"))

def generirajBroj(veciOdSto):
    if veciOdSto < 101 or veciOdSto > 999:
        print("Broj ne valja")
        return 150
    else:
        import random
        genBroj = random.randint(broj, veciOdSto)
        print("Generiran je slučajni broj: {}".format(genBroj))
        return genBroj

def vratiZbroj(veciOdSto):
    if veciOdSto > 100 and veciOdSto < 999:
        prvaZnamenka = veciOdSto % 10
        drugaZnamenka = (veciOdSto // 10) % 10
        trecaZnamenka = veciOdSto //100
        return prvaZnamenka + drugaZnamenka + trecaZnamenka

if __name__ == "__main__":
    main()

Also, paths are using forward slashes in unix.

Sign up to request clarification or add additional context in comments.

5 Comments

I edited the path and still not working. Now I edited the indentation here, so please rewiev my code.
The error is actually quite clear whats wrong: AttributeError: module 'brojeviModul' has no attribute 'generirajBroj' The brojeviModul has no function called generirajBroj. What is in brojevModul ?
brojeviModul is the name of the .py file (module)
@ZoranBajcer Sorry, long day. Edited the original answer
I set my both module and main on desktop and set path to ("brojeviModul.py") and it works.. no idea why it was not working before but now it is...
0

Add your scripts to one of the directory within PYTHONPATH ENV variable or add the corresponding directory to $PYTHONPATH . Ref - https://leemendelowitz.github.io/blog/how-does-python-find-packages.html

1 Comment

This really isn't necessary since the script clearly finds the (Or at least, a) module already

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.