[Python] How to reuse python code in one file from a different file

Discussão em 'Python' iniciado por Stack, Fevereiro 19, 2025.

  1. Stack

    Stack Membro Participativo

    My question is very simple. I have a piece of code that I want to reuse, but I don't want to copy it every time I use it. Is there any way I can import it and run it in my main file?

    For example, in file code1.py:

    a=1


    I want to run in code2.py:

    import code1
    b=a+1
    print(b)


    The output says a is not defined. I don't know where I got it wrong. I am a beginner in Python, so this will help me a lot in the future, thanks.

    Continue reading...

Compartilhe esta Página