April 29, 2024, 6:48 p.m. | Raju Nekadi

DEV Community dev.to

Solving the mystery of If __name__ == '__main__'.


Let's assume you have a python script first.py



# first.py

print('Hola from first.py')

if __name__ == '__main__':
print('Hola first module __name__:' __name__')

# output
# Hola from first.py
# Hola first module __name__: __main__


If we run first.py using the command Python

python first.py, the __name__ variable for first.py will automatically become __main__.



# second.py

print('Hola from second.py')

if __name__ == '__main__':
print('Hola second module __name__:' __name__')

# output
# Hola from …

command python python script script

Software Engineer for AI Training Data (School Specific)

@ G2i Inc | Remote

Software Engineer for AI Training Data (Python)

@ G2i Inc | Remote

Software Engineer for AI Training Data (Tier 2)

@ G2i Inc | Remote

Data Engineer

@ Lemon.io | Remote: Europe, LATAM, Canada, UK, Asia, Oceania

Artificial Intelligence – Bioinformatic Expert

@ University of Texas Medical Branch | Galveston, TX

Lead Developer (AI)

@ Cere Network | San Francisco, US