You will get a runtime error, and the program will crash.
---
In Python, if you attempt to import a module that doesn't exist, the interpreter will raise a runtime error called
ModuleNotFoundError
. This error immediately stops the execution of the program—unless it is properly handled using a try-except
block.---
import my_fake_module
ModuleNotFoundError: No module named 'my_fake_module'
---
To prevent the program from crashing, you can catch the error using t
ry-except:
try:
import my_fake_module
except ModuleNotFoundError:
print("Module not found, but the program continues running.")
In Python, importing a non-existent module leads to a runtime error. If not handled, it will crash the program.
Stay tuned for tomorrow’s question
Please open Telegram to view this post
VIEW IN TELEGRAM
❤5👍1🥰1
This media is not supported in your browser
VIEW IN TELEGRAM
👏3
In Python, when you import a module, it's only loaded once from disk and then cached in memory (RAM). So if you modify that module later, Python won’t reload it automatically — even if you import it again!
---
Let’s say you have a file called
mathutils.py
with:def add(a, b):
return a + b
You import it in
main.py
like this:import mathutils
Later, you update
mathutils.py
and add a new function:def subtract(a, b):
return a - b
If you now run
import mathutils
again in main.py
, Python will not see the new subtract()
function. It uses the cached version already loaded in memory.---
import importlib
import mathutils
importlib.reload(mathutils) # Forces Python to reload the updated module
# Now the new function is accessible
print(mathutils.subtract(10, 3)) # ➡️ Output: 7
---
To improve performance, Python loads modules from disk only once, and then stores them in memory (RAM).
If the module file changes, Python doesn’t detect it unless you explicitly tell it to reload.
- Re-importing a module doesn’t reload its changes.
- Use
importlib.reload()
to reload the updated version from disk.Please open Telegram to view this post
VIEW IN TELEGRAM
👍5👏1
This media is not supported in your browser
VIEW IN TELEGRAM
👍1
Forwarded from Python | Machine Learning | Coding | R
This channels is for Programmers, Coders, Software Engineers.
0️⃣ Python
1️⃣ Data Science
2️⃣ Machine Learning
3️⃣ Data Visualization
4️⃣ Artificial Intelligence
5️⃣ Data Analysis
6️⃣ Statistics
7️⃣ Deep Learning
8️⃣ programming Languages
✅ https://t.iss.one/addlist/8_rRW2scgfRhOTc0
✅ https://t.iss.one/Codeprogrammer
Please open Telegram to view this post
VIEW IN TELEGRAM
👍1
This media is not supported in your browser
VIEW IN TELEGRAM
🔥 Python Tip of the Day:
How to Accept Any Number of Arguments in a Function?
Ever wanted to pass as many values as you like to a function in Python? You can! Just use:
This `*args
my_function(1, 2, 3, 'Python', 42)
1
2
3
Python
42
Perfect when you don’t know how many inputs you’ll get!
---
Why `*args`?
- ✅ Flexible & clean
- ✅ Avoids unnecessary overloads
- ✅ Makes your code reusable & Pythonic
---
Follow us for daily Python gems
💡 https://t.iss.one/DataScienceQ
#PythonTips #ArgsInPython #CodingSmart #PythonicWay #DeveloperDaily
How to Accept Any Number of Arguments in a Function?
Ever wanted to pass as many values as you like to a function in Python? You can! Just use:
def my_function(*args):
for item in args:
print(item)
This `*args
syntax lets your function take any number of positional arguments— from zero to infinity!
---
✨ Example:
``
pythonmy_function(1, 2, 3, 'Python', 42)
Output:
1
2
3
Python
42
`
Perfect when you don’t know how many inputs you’ll get!
---
Why `*args`?
- ✅ Flexible & clean
- ✅ Avoids unnecessary overloads
- ✅ Makes your code reusable & Pythonic
---
Follow us for daily Python gems
💡 https://t.iss.one/DataScienceQ
#PythonTips #ArgsInPython #CodingSmart #PythonicWay #DeveloperDaily
Telegram
Python Data Science Jobs & Interviews
Your go-to hub for Python and Data Science—featuring questions, answers, quizzes, and interview tips to sharpen your skills and boost your career in the data-driven world.
Admin: @Hussein_Sheikho
Admin: @Hussein_Sheikho
__name__ == "__main__"
— What Does It Do?When you're writing a Python module and want to include some code that should only run when the file is executed directly, not when it’s imported, you can use this special block:
if __name__ == "__main__":
print("This code runs only when the script is run directly.")
---
-
python myscript.py
nameon sets
__name__
to "__main__"
, so the code inside the block runs.-
import myscript
→ Python sets
__name__
to "myscript"
, so the block is skipped.---
- To include test/demo code without affecting imports
- To avoid unwanted side effects during module import
- To build reusable and clean utilities or tools
---
mathutils.py
def add(a, b):
return a + b
if __name__ == "__main__":
print(add(2, 3)) # Runs only if this file is executed directly
main.py
import mathutils
# No output from mathutils when name!
Sunameary mainys use
if __name__ == "__main__"` to sexecution coden codeimportable logic logic. It’s Pythonic, clean, and highly recommended!
---
#PythonTips #LearnPython #CodingTricks #PythonDeveloper #CleanCode!
Please open Telegram to view this post
VIEW IN TELEGRAM
👍5🔥1
This media is not supported in your browser
VIEW IN TELEGRAM
🔥 Python Tip of the Day:
How to Accept *Any* Number of Arguments in a Function?
Ever wanted to pass as many values as you like to a function in Python? You can! Just use:
This `*args
Output:
Perfect when you don’t know how many inputs you’ll get!
❓Why
- ✅ Flexible & clean
- ✅ Avoids unnecessary overloads
- ✅ Makes your code reusable & Pythonic
Follow us for daily Python gems
💡 https://t.iss.one/DataScienceQ
#PythonTips #ArgsInPython #CodingSmart #PythonicWay #DeveloperDaily
How to Accept *Any* Number of Arguments in a Function?
Ever wanted to pass as many values as you like to a function in Python? You can! Just use:
def my_function(*args):
for item in args:
print(item)
This `*args
syntax lets your function take **any number of positional arguments** — from zero to infinity!
✨ Example:
my_function(1, 2, 3, 'Python', 42)
Output:
1
2
3
Python
42
Perfect when you don’t know how many inputs you’ll get!
❓Why
*args
?- ✅ Flexible & clean
- ✅ Avoids unnecessary overloads
- ✅ Makes your code reusable & Pythonic
Follow us for daily Python gems
💡 https://t.iss.one/DataScienceQ
#PythonTips #ArgsInPython #CodingSmart #PythonicWay #DeveloperDaily
Telegram
Python Data Science Jobs & Interviews
Your go-to hub for Python and Data Science—featuring questions, answers, quizzes, and interview tips to sharpen your skills and boost your career in the data-driven world.
Admin: @Hussein_Sheikho
Admin: @Hussein_Sheikho
🔥2
This media is not supported in your browser
VIEW IN TELEGRAM
🔥3