3

I was wondering if it's possible for me to include another python file in the actual one? I'm running an app.py from the terminal with Flask and I want, when I click on submit that python run 5 lines of code, like nothing. But when I do the variable from the first python file can't be read on the second and even if I put the same variable into the second python file then it still doesn't work.

I want to run a "for", this is the code

    for line in fin:
        line = re.sub('APP:NAME', name, line)
        line = re.sub('APP:USERNAME', username, line)
        line = re.sub('APP:TEXT', tweet, line)
        fout.write(line)

I checked all the forums and I didn't find the solution.

Thank you

2
  • I tried "import text_social" but PyCharm returns the import in grey Commented Aug 23, 2020 at 8:41
  • so your question is how to take a value of variable from one file to another? Commented Aug 23, 2020 at 8:51

1 Answer 1

3

Method 1

I think your need is to include a python file to another python file in a same directory. You can import a python file as shown below

import filename

here, the filename is the filename of the file in the same location without file extension. and you can call functions inside that file by calling like this.

filename.function()

Method 2

if you which to rename that filename and use it as something else:

import filename as fn

here, the filename is the filename of the file in the same location without file extension. and you can call functions inside that file by calling like this.

fn.functionName()

Method 3

or you can simply use

from filename import *

and call the functions in that file as normal functions in the current python file like

functionname()

Method 4 (Recommended)

or you can simply import only what is required from the file instead of entire file

from filename import functionname

and call the functions in that file as normal functions in the current python file like

functionname()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.