Manogna

Manogna

5 Articles Published

Articles by Manogna

5 articles

How do I find the location of Python module sources?

Manogna
Manogna
Updated on 24-Mar-2026 12K+ Views

Finding the location of Python module sources helps with debugging and understanding how modules work. Python provides several methods to locate module files depending on the module type. Using __file__ Attribute For pure Python modules, the __file__ attribute shows the file location ? import os print("Module file:", os.__file__) print("Module location:", os.path.dirname(os.__file__)) Module file: /usr/lib/python3.8/os.py Module location: /usr/lib/python3.8 Using inspect Module The inspect module provides more detailed information about module sources ? import inspect import json # Get source file location print("JSON module file:", inspect.getfile(json)) print("JSON module source ...

Read More

How do I unload (reload) a Python module?

Manogna
Manogna
Updated on 24-Mar-2026 786 Views

The reload() function reloads a previously imported module without restarting Python. This is useful when you modify a module's source code and want to test changes in an interactive session. Python 2 vs Python 3 In Python 2, reload() was a built-in function. In Python 3, it was moved to the importlib module. Python 2 Syntax import mymodule # Edit mymodule.py and want to reload it reload(mymodule) Python 3 Syntax import importlib import mymodule # Edit mymodule.py and want to reload it importlib.reload(mymodule) Example Let's create a ...

Read More

How can I remove the ANSI escape sequences from a string in python?

Manogna
Manogna
Updated on 13-Mar-2026 9K+ Views

You can use regexes to remove the ANSI escape sequences from a string in Python. Simply substitute the escape sequences with an empty string using re.sub(). The regex you can use for removing ANSI escape sequences is: (\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]. Example Here's how to create a function to remove ANSI escape sequences − import re def escape_ansi(line): ansi_escape = re.compile(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]') return ansi_escape.sub('', line) # Test with a string containing ANSI escape sequences test_string = '\t\u001b[0;35mSomeText\u001b[0m\u001b[0;36m172.18.0.2\u001b[0m' result = escape_ansi(test_string) print(repr(result)) The output of the above ...

Read More

How to implement a custom Python Exception with custom message?

Manogna
Manogna
Updated on 13-Mar-2026 354 Views

In Python, you can create custom exceptions by inheriting from built-in exception classes. This allows you to define specific error types with meaningful messages for your application. Custom exceptions help make your code more readable and provide better error handling. Creating a Custom Exception Class To implement a custom Python exception with a custom message, you need to create a class that inherits from a built-in exception class like Exception, ValueError, or RuntimeError. The custom class should have an __init__ method to store the custom message. Example Here's how to create and use a custom exception ...

Read More

How can I write a try/except block that catches all Python exceptions?

Manogna
Manogna
Updated on 13-Mar-2026 272 Views

It is a general thumb rule that though you can catch all exceptions using code like below, you shouldn't − try: # do_something() pass except: print("Exception Caught!") However, this will also catch exceptions like KeyboardInterrupt and SystemExit that we may not be interested in handling. This can make it difficult to interrupt your program or cause other unexpected behaviors. Better Approach with Exception Re-raising A better approach is to catch all exceptions but re-raise them after logging or handling. Here's a complete ...

Read More
Showing 1–5 of 5 articles
« Prev 1 Next »
Advertisements