Python Forum
how to run another function from main file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to run another function from main file
#1
Hi,
I have main.py & read_data.py and ready csv file and write. I want to call read_data.py and execute. Byt it giving error as below.

NameError: name 'inFile' is not defined
[/python]
main.py

import logging
from datetime import datetime
import pandas as pd
from read_data import data
def main():
    if __name__ == '__main__':
        data.read_data(inFile= 'my.csv', colNames ='col1', mode = 'NOM')
read_data.py:

import pandas as pd
import logging
class MyData:  
    def __init__(self):  
        self.mode = None
        
       
    def read_data(self, inFile, colNames, mode):
        self.infile = inFile
        print("The infile name",inFile)       
        try:
            self.df = pd.read_csv(inFile)
            print(self.df.head(2))
            self.df.to_csv("final.csv",index=False)
            logging.debug("Read data successfully")
        except OSError as e:
            logging.debug("File read fail check fail reason below")
            logging.debug(e.errno)
       

data = MyData()
data.read_data(inFile, colNames, mode)
my.csv:
C1   C2  C3
A    20  2020/05/03 20:45:12
B    5  2020/06/23 10:45:12
Reply
#2
On line 22 of read_data.py, you're referring to variables that don't exist. You really shouldn't put those statements (lines 21 and 22) in the module, because they'll be run every time the module is imported. Get rid of line 22 there and move line 21 into main.py. Note as well that your main function isn't called anywhere..
Reply
#3
Your csv is wrong formated, delimiter should be tab(you have 3 spaces or 4 four spaces)

my.csv

C1	C2	C3
A	20	2020/05/03 20:45:12
B	5	2020/06/23 10:45:12
main.py

import logging
from datetime import datetime
import pandas as pd
from read_data import data
import sys

working_dir = sys.argv[0].rpartition("/")[0]

def main():
    if __name__ == '__main__':
        data.read_data(f'{working_dir}/my.csv', colNames ='col1', mode = 'NOM', delimiter = "\t")
        
main()
read_data.py

import pandas as pd
import logging
import sys

working_dir = sys.argv[0].rpartition("/")[0]

class MyData:  
    def __init__(self):  
        self.mode = None
         
        
    def read_data(self, inFile, colNames, mode, delimiter):
        self.infile = inFile
        print("The infile name",inFile)       
        try:
            self.df = pd.read_csv(inFile, delimiter = "\t")
            print(self.df.head(2))
            self.df.to_csv(f"{working_dir}/final.csv",index=False, sep = "\t")
            logging.debug("Read data successfully")
        except OSError as e:
            logging.debug("File read fail check fail reason below")
            logging.debug(e.errno)
        
 
data = MyData()
Reply
#4
Here's a simpler example of what is happening with your code. I have two files; junk.py and junk2.py.
#junk2.py
x = 5
print('junk2.py', __name__)

#junk.py
from junk2 import x
print('junk.py', __name__)
When I run junk.py I see this:
Output:
junk2.py junk2 5 junk.py __main__
I never typed a command to run junk2.py, but the code in the file was executed. Why?

When you import a module for the first time, Python executes the code inside the module. This is done so Python will know what objects, functions and classes are defined in the module. Executing code that defines a function is not the same as calling a function. It adds the function to the module namespace. Executing code that defines a class does not make an instance of the class. It adds the class to the module namespace. Code that is not a function or class definition executes normally.

When junk.py imports junk2.py the code inside junk2.py is executed and that is why junk2.py junk2 is printed and why x is assigned the value 5. This is exactly the same thing that would happen if I executed junk2.py by typing python junk2.py at the command prompt.

Your program crashes because it executes this code when main.py executes from read_data import data
data = MyData()
data.read_data(inFile, colNames, mode)
inFile, colNames and mode are not defined in the current namespace, so python raises a NameError exception.

In addition to the NameError there are a few questionable design decisions. Is MyData meant to be a singleton? If so, your design does not provide adequate protection against making multiple MyData objects. I don't think that was you plan though. I think you just chose the wrong place to make a MyData object.

When you write a class or a function you should always write the code with the intention of making it something you can reuse. Make the code as generic as you can while still making it useful. read_data.py should only define the class and leave using the class up to modules that import read_data. read_data.py should not force you to make one instance of MyData or force what name is used for the instance. Those details should be left to the user of your class.
data = MyData() belongs in main.py.

And then there is this:
def main():
    if __name__ == '__main__':
        data.read_data(f'{working_dir}/my.csv', colNames ='col1', mode = 'NOM', delimiter = "\t")
main()
I think that you are somewhat confused about the power of main. "__main__" is a string that may be assigned as the value of __name__. That is it. That is the full limit of main's power. Using main as a function name does not make that function special. Using main as a module name does not make the module special. So when you execute main.py Python is not going to automatically call function main(). You have to do that yourself.

This is the most common way to use __name__.
def main():
    data = read_data.MyData()
    data.read_data(inFile= 'my.csv', colNames ='col1', mode = 'NOM')

if __name__ == '__main__':
    main()
It is also common to see __name__ used like this:
if __name__ == '__main__':
    data = read_data.MyData()
    data.read_data(inFile= 'my.csv', colNames ='col1', mode = 'NOM')
Or even not using __name__ at all.
read_data.MyData().read_data(inFile= 'my.csv', colNames ='col1', mode = 'NOM')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 13 680 Yesterday, 07:07 AM
Last Post: Bronjer
  python run all py files from main py file mg24 6 1,308 Oct-12-2022, 04:41 AM
Last Post: mg24
  Function global not readable by 'main' fmr300 1 1,335 Jan-16-2022, 01:18 AM
Last Post: deanhystad
  Problem with user defined main menu function stefzeer 3 2,388 Mar-27-2020, 06:12 AM
Last Post: buran
  main def function puertas12 1 1,896 Jun-11-2019, 11:39 AM
Last Post: snippsat
  Print out the tuple from the function main whatloop 2 2,377 Mar-25-2019, 10:27 AM
Last Post: whatloop
  Use Variables Generated from Functions in different files to use on the main file AykutRobotics 3 2,925 Jan-01-2019, 04:19 PM
Last Post: AykutRobotics
  main function scope issues wak_stephanie 1 2,459 Aug-29-2018, 02:53 AM
Last Post: Larz60+
  I'm having trouble printing a return value in my main function RedSkeleton007 2 3,097 Apr-08-2018, 04:17 PM
Last Post: IAMK
  Cannot call main function but I don't know why eml 2 2,769 Mar-09-2018, 12:14 PM
Last Post: eml

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020