Class 5 Lab 4 ‐ Main Identification - Justin-Boyd/Python-Class GitHub Wiki

Step 1

  • Create a new Python file in PyCharm by right-clicking the project you created and selecting New > Python File. Name this file message.py.

Step 2

  • Define a new function that prints the file name in message.py.
def main():
    print("Hello from:", __name__)

Step 3

  • Create a second Python file in PyCharm by right-clicking the project you created and selecting New > Python File.

Step 4

  • Import to the file the previous file.
import message

Step 5

  • Create a new function.
def main():

Step 6

  • Within the function, invoke the main function of the imported file.
message.main()

Step 7

  • Outside of the function, create a condition that checks if the current file is directly executed.
if __name__ == '__main__':

Step 8

  • Within the condition, print the file’s name and invoke the main function of the file.
print(__name__)
main()

Step 9

  • Run the file

Final Code

import message

def main():
   message.main()

if __name__ == '__main__':
   print(__name__)

   main()