0

I'm creating a Python module mymodule.py and I need to run a function on import. The function should not be run by the user, and is only necessary to initiate the module properly.

Since it's a module, this won't work:

if __name__ == '__main__': _main() 

I want to follow PEPs and I was simply wondering if there an equivalent of C's main() in a Python module?

...or if I should just write the initialisation code inline (not as a function), or call the function inline.

4
  • Why not to write code fragment inline, because inline code fragment will be processed on import? Commented May 29, 2016 at 13:18
  • What's wrong with calling it regardless of if __name__ == '__main__'? Is that not PEP-friendly? Does it not do what you want in certain situations? Commented May 29, 2016 at 13:19
  • 2
    "run a function on import" and C's main() are not equivalent. Commented May 29, 2016 at 13:40
  • Agree with @gevorg , just write the code fragment inline . If you don't want to expose the variables then simply define a function and call it inline. Commented May 29, 2016 at 13:44

3 Answers 3

4

Code that is not indented into a function will be run as the file is loaded (once). That's probably what you want:

def some_function(): pass def other_function(): pass init_value = 0 # This code here is run immediately. buffer = None 
Sign up to request clarification or add additional context in comments.

4 Comments

Yes, but then those variables will get exposed in the module, dir(mymodule). I don't really want that.
Then put the code in a _main function (to avoid leaking the variables) and call it from the toplevel of the module.
In that case you can define function and call it inline.
You don't have to define variables. You could call one of the functions. My point is simply that the code is being executed. It can be whatever code you think you need.
1

Running a function on import is not equivalent to C's main(). C's main() is run when executing the program.

Everything in a Python module which is top-level (i.e. not in a function) is executed when the module is imported. For example, if this is your module content:

def _on_import(): pass # do something _on_import() 

then _on_import() is executed when importing the module.

When your module looks like this:

def main(): pass # do something if __name__ == '__main__': main() 

then main() is executed when running the python module as a script (e.g. if your module is in file foo.py and you run python foo.py). This is basically equivalent to C's main().

Comments

0

Yeah have like a top level function that is like a main, something like this:

 def interact(): # Your code which handles all the rest of the functions # For example print('Welcome to this program!') filename = input('Please enter the data source file: ') load_data(filename) ...... 

And then at the bottom of your script do what you were doing:

 if __name__ == '__main__': interact() 

if you want to import other files into the program, like mymodule.py, just do this:

 from mymodule import * 

Alternatively, you can test functions out like this:

 if __name__ == "__main__": print test_the_function(123, 456) 

When mymodule is imported, the code is run as before, but when we get to the if statement, Python looks to see what name the module has. Since the module is imported, we know it by the name used when importing it, so __name__ is mymodule. Thus, the print statement is never reached.

The beautiful thing about python is that it doesn't work like Cs main(). You just start typing, and you've written your first program. The simplicity of python is what makes it so beautiful, I wouldn't try and compare it with C, it just won't work.

You can also check this website out, if has some useful information about the python main():

https://wiki.python.org/moin/Asking%20for%20Help/Do%20you%20need%20a%20int%20main()%20like%20you%20do%20in%20c%2B%2B

1 Comment

Yes but the OP wants the function to be called even on import . This won't work when we import the module.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.