Home > Enterprise >  How to make another .py file recognize an input from another .py file?
How to make another .py file recognize an input from another .py file?

Time:05-24

So I have two .py files, fileA.py and fileB.py.

fileA.py will make fileB.py run.

But first, fileA.py will have a script to ask a folder directory as an input.

from pathlib import Path
import subprocess
import sys
from tkinter.filedialog import askdirectory

pathcase = askdirectory(title='path to folder')

scripts = [
    'fileB.py'
]

parent = Path(__file__).resolve().parent

for script in scripts:
    script_path = parent / script
    subprocess.call([sys.executable, script_path])

How to make fileB.py run and recognize the "pathcase" input?

fileB.py has:

#!/usr/bin/env pvpython
from paraview.simple import *
from fileA import pathcase
casefoam = OpenFOAMReader(registrationName='case.foam', FileName='{}/case.foam'.format(pathcase))

What is happening right now is that fileA.py runs fileB.py and asks again for an input everytime, making a loop.

CodePudding user response:

I would recommend keeping a config.py module to store all of your globally used variables, in this case, pathcase. Just import the config module in all modules of your application; the module then becomes available as a global name. It would work as the following:

config.py

n = 10

fileA

import config
config.n = 20

fileB

import config
import fileA
print(config.n)

This way, your code will stay elegant, readable, and should fix the problem you are running into. :)

  • Related