SCH
SCH

Reputation: 83

Pass a variable from one python file to another

I have 3 py files; 1 is the main file that runs function from the other 2 py files. Other 2 files will be get data from excel file. I will need user to input to determined whether to collect which set of data from excel. At beginning of the of my file1 (main) i will ask user to input 1 or 2, and store this value in to pre-defined variable"mode".

'UPDATED CODE AT BOTTOM'

But above will get me error about mode is not defined.

I've found some answers online, which i've put mode input on file 2, and import file 2 on file 1 and 3. It is working, but I do not know why it has to be a function, and what 'global' is doing here. Is there a better method of passing variables to other file?

Code that worked:

file 1:

import file 2
import file 3

if file2.mode == '1':
     function2 from file2
     function3 from file3
elif file2.mode == '2':
     function2 from file2
     function3 from file3

file 2:


def function():
    global mode
    mode = input(print("=>Input '1' for Eyebrow2D\n=>Input '2' for Eyebrow3D"))
function()

workfunction2():

file 3:

if file2.mode == '1':
     do something
elif file2.mode == '2':
     do other thing
workfunction3():

EDIT update some of the codes that will get me error

File 1:

from file2 import *

mode = input(print("=>Input '1' for option1\n=>Input '2' for option2"))
if mode == '1':
    get_excel_data()
    print(shape)
elif mode == '2':
    get_excel_data()
    print(shape)

File 2

from file1 import *
###I will get error here for mode not define
if mode == '1':
    data_xls = pd.read_excel('data.xlsx', sheet_name=2d)
    data_xls.to_csv('data.csv', encoding='utf-8')
    df = pd.read_csv(data.csv', header = 1, encoding = encoding)
elif mode == '2':
    data_xls = pd.read_excel('data.xlsx', sheet_name=3d)
    data_xls.to_csv('data2.csv', encoding='utf-8')
    df = pd.read_csv(data2.csv', header = 1, encoding = encoding)

shape = []
def get_excel_data():
    if mode == '1':
        for value in df["Shape"]:
            if type(value) == float:
                if math.isnan(value):
                    print("empty")
                    continue
            else:
                str(value).strip()
                excel_list.append(value)
    else:
        pass


Maybe i will need to use file1.mode in file 2?

Upvotes: 1

Views: 13676

Answers (2)

patrickgerard
patrickgerard

Reputation: 510

You should not do this like you mentioned.

You would have to do this in a way like this example:

import file1

question = input(int("how old are you?"))
if question > 18:
    file1.adult(question)
else:
    file1.children(question)

Upvotes: 1

MubtadaNaqvi
MubtadaNaqvi

Reputation: 197

No need for any global variable, all you need to do is define the functions and pass data as arguments. For example, the main should be like this

mode = input("1 or 2?")
if mode == '1':
     function_from_file2(mode)
     function_from_file3(mode)
elif mode == '2':
     do_something(mode)

and in other files

# file 2

def function_from_file2(mode):
    // add your functionally here 

The same goes for other files

Upvotes: 2

Related Questions