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

DAREW0L7 [moroccangaming10]
5 years ago | edited 5 years ago

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 :


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:

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
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
from machinelearning import utilisateur
File “C:\Users\darewolf\Desktop\Nouveau dossier (2)\machinelearning.py”, line 19, in
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)

3replies
2voices
186views
dloser
5 years ago

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.

DAREW0L7 [moroccangaming10]
5 years ago | edited 5 years ago

0

i didn’t understand all what u said but i have added ... so can u like correct the code so i can uderstand all what u said

dloser
5 years ago

0

What did you not understand?

I cannot correct it if I don’t know what you want exactly.

You must be logged in to reply to this discussion. Login
1 of 4

This site only uses cookies that are essential for the functionality of this website. Cookies are not used for tracking or marketing purposes.

By using our site, you acknowledge that you have read and understand our Privacy Policy, and Terms of Service.

Dismiss