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] Import Variable from Python FilePath

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

  1. Stack

    Stack Membro Participativo

    I'm writing a clean up script from one of our applications and I need a few variables from a python file in a separate directory.

    Now normally I would go:

    from myfile import myvariable
    print myvariable


    However this doesn't work for files outside of the directory. I'd like a more targeted solution than:

    sys.path.append('/path/to/my/dir)
    from myfile import myvariable


    As this directory has a lot of other files, unfortunately it doesn't seem like module = __import__('/path/to/myfile.py') works either. Any suggestions. I'm using python 2.7

    EDIT, this path is unfortunately a string from os.path.join(latest, "myfile.py")

    Continue reading...

Compartilhe esta Página