1

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?

2
  • 4
    Files and variables are the wrong abstraction for passing things around. One passes values to functions or objects. While one can "pass variables between files", it will be a convoluted way to end up at passing values to functions/objects. Commented Feb 19, 2021 at 9:23
  • You have used the python tag, but not all your snippets are valid python. Please update your question with valid code. Commented Feb 19, 2021 at 9:24

2 Answers 2

2

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

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

3 Comments

I think your function will work, pass the value into function. But I will also add a 'if mode == 1 if mode ==2' in file 2, will this 'mode' get value from file 1? or it can only be passed into function?
the value of "mode" in file 2 will be the same as the main function. you can also save mode's value in file 2 like this, but all depends upon the functionality/requirement
# file 2 MODE_1 = '' def function_from_file2(mode): MODE_1 = mode
1

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)

1 Comment

It's perfectly possible, albeit not advisable, to do this as described in the question.

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.