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
#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


Messages In This Thread
RE: how to run another function from main file - by deanhystad - Aug-09-2020, 04:41 AM

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 1,027 Apr-24-2024, 05:47 AM
Last Post: Bronjer
  python run all py files from main py file mg24 6 1,390 Oct-12-2022, 04:41 AM
Last Post: mg24
  Function global not readable by 'main' fmr300 1 1,368 Jan-16-2022, 01:18 AM
Last Post: deanhystad
  Problem with user defined main menu function stefzeer 3 2,419 Mar-27-2020, 06:12 AM
Last Post: buran
  main def function puertas12 1 1,922 Jun-11-2019, 11:39 AM
Last Post: snippsat
  Print out the tuple from the function main whatloop 2 2,409 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,980 Jan-01-2019, 04:19 PM
Last Post: AykutRobotics
  main function scope issues wak_stephanie 1 2,482 Aug-29-2018, 02:53 AM
Last Post: Larz60+
  I'm having trouble printing a return value in my main function RedSkeleton007 2 3,126 Apr-08-2018, 04:17 PM
Last Post: IAMK
  Cannot call main function but I don't know why eml 2 2,804 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