1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] If I do a magic command, %run file, does that run the script as main? If so how do I...

Discussão em 'Python' iniciado por Stack, Novembro 25, 2025.

  1. Stack

    Stack Membro Participativo

    I have a .ipynb file and want to only import certain functions from it to another .ipynb file.

    if I use:

    if __name__ == "__main__":


    would the script run what is under that if statement when I call:

    %run file

    For instance I have:

    def someFuncToUseInOtherFile(df):
    x = do some process
    return x
    def someFuncToUseInDifferentFile():
    y = do some process
    return y

    #code not to run when script called from other file
    if __name__ == "__main__":
    df = load dataframe
    x = ....


    #other file
    %run file


    currently this runs the whole script but I just want to import the one function not both.

    I have tried importing directly like a .py file, including using sys.path.append:

    from file import someFuncToUseInOtherFile --> module not found error

    import sys
    sys.path.append(folder path)
    from file import someFuncToUseInOtherFile --> module not found error


    is there a way to get around this?

    Continue reading...

Compartilhe esta Página