Privacy & Cookies

This website uses cookies. By continuing to use this site you are agreeing to our use of cookies.
Forum Index

Sections

View

Filter topics in this section

Stats

Threads7,309
Posts58,575
Authors6,686

i doing a program that modify the code of an other program and i got an error it shows me that i cant import a function

Programming & Technology > Programming Discussion > i doing a program that modify the code of an other program and i got an error it shows me that i cant import a function


    • 2445
    • 24

    0
    i doing a program that modify the code of an other program and i got an error it shows me that i cant import a function

    when i choose the second choice it shows me that error i have do some search and i cant find something

    program 1 :
    -------------------------------------------------------------------------------------
    Code:
    
    def system():
        choix=input("1 ou 2")
        if choix=="1":
            global utilisateur
            global robot
            utilisateur=input("si j'ecris: ")
            robot=input("il me repond: ")
            fichier = open("learning.py", "a")
            fichier.write("\n   elif val1==utilisateur :\n      print(robot)")
            fichier.close()
        elif choix=="2":
            global admin
            from learning import machine
            print("console : salut")
            admin=input("")
            machine()
        else:
            print("error")
    system()
    

    -----------------------------------------------------------------------------------


    program 2:
    -----------------------------------------------------------------------------------
    Code:
    from machinelearning import utilisateur
    from machinelearning import robot
    from machinelearning import admin
    def machine():
        if admin==utilisateur :
            print(robot)
        elif admin==utilisateur:
            print(robot)
        elif val1==utilisateur:
            print(robot)
    

    ----------------------------------------------------------------------------------


    error:
    ----------------------------------------------------------------------------------------------------------------------------------------------------------------
    Traceback (most recent call last):
    File "C:\Users\darewolf\Desktop\Nouveau dossier (2)\machinelearning.py", line 19, in <module>
    system()
    File "C:\Users\darewolf\Desktop\Nouveau dossier (2)\machinelearning.py", line 13, in system
    from learning import machine
    File "C:\Users\darewolf\Desktop\Nouveau dossier (2)\learning.py", line 1, in <module>
    from machinelearning import utilisateur
    File "C:\Users\darewolf\Desktop\Nouveau dossier (2)\machinelearning.py", line 19, in <module>
    system()
    File "C:\Users\darewolf\Desktop\Nouveau dossier (2)\machinelearning.py", line 13, in system
    from learning import machine
    ImportError: cannot import name 'machine' from 'learning' (C:\Users\darewolf\Desktop\Nouveau dossier (2)\learning.py)
    ----------------------------------------------------------------------------------------------------------------------------------------------------------------
Viewing 3 replies - 1 through 3 (of 3 total)
  • dloser
    dloser's profile picture

    • 13210
    • 1495

    0
    Try using [code]...[/code] blocks around your code, so we can read it better.

    In any case, there is a lot of strange stuff going on here, so not sure where to start.

    What I can say is that having circular dependencies like this is "never" a good thing. File 1 depends on 2 and 2 depends on 1. That's asking for problems. In this case you can see that system() gets called a second time because of the first import in "program 2", so it will never get past that point. This is also the reason for the error you get when it tries to import learning.py a second time.

    Also, the values you read are never actually used. You don't write the actual values to the file and after "program 1" terminates, the values are lost. Importing "program 1" in the next run has no use because the values are no longer there and the variables you want to use are not even defined in that case.

    Perhaps it helps to add some debug print statements in your code to see how the flow of the code is and what the values of variables are.
  • moroccangaming10
    moroccangaming10's profile picture

    • 2445
    • 24

    0
    i didn't understand all what u said but i have added
    Code:
    ...
    so can u like correct the code so i can uderstand all what u said
  • dloser
    dloser's profile picture

    • 13210
    • 1495

    0
    What did you not understand?

    I cannot correct it if I don't know what you want exactly.
Viewing 3 replies - 1 through 3 (of 3 total)
You must be logged in to reply to this topic