Python Script Referencing or Calling

Question:

I have 2 python files (.py) named fileA and fileB.

# file A has the following codes:

import Pandas as pd
x = input (x)
z = input (y)
y = x + z

if y == 8:
    fileB() 
# Call or run file B
# ::::::::::::::::::::::::::::::::::::::::::::::::::::
# file B has the following codes:
# x and y are from file A
a = x + y     
c = a + z
print (c)

Question: I want to import file B into A and run file A

# I have tried to use the following:

# into file A
from  fileB import* 

# and also from fileA import* #
# into file B so I can use some variables in file A.

# But I get errors as name x and y are not defined for fileB
Asked By: Samson Shoyombo

||

Answers:

If you create a function in file B that does the maths it schould work:

def funB(x, z):
    a = x + y     
    c = a + z
    return c

then you can import that funcion in file A like:

from fileB import funB
if ...:
    print(funB(x, y))
Answered By: Mav Rick
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.