How to include content from a page .py on another page .py

1

How do I make a include or open a python page in another, for example in php :

include 'content/filename.php';

Load the Html it loads the file .py no, I already tried the command open

fp = open('content/header.py')
html = fp.read()
fp.close()
    
asked by anonymous 13.11.2016 / 15:56

2 answers

-1

Just type the "include" command:

import os

def include(filename):
    if os.path.exists(filename): 
        execfile(filename)


include('meuarquivo.py')

For python 2 and 3 compatibility, you will need:

Python 2 and 3: alternative 1

from past.builtins import execfile

execfile('meuarquivo.py')

Python 2 and 3: alternative 2

exec(compile(open('meuarquivo.py').read()))

I hope I have helped.

    
13.11.2016 / 17:07
2

In fact you did not even have to give as much back as Paul quotes above. You can do a function that stores these values and in the other file you pull it:

from arquivo1 import funcao
a,b,c = funcao()

In addition you can leave these values stored in variables and pull them through the other file as if they were normal functions.

    
13.11.2016 / 22:22