Articles on Trending Technologies

Technical articles with clear explanations and examples

How do I import all the submodules of a Python namespace package?

Sumana Challa
Sumana Challa
Updated on 24-Mar-2026 1K+ Views

Namespace packages are a type of package in Python that allows you to split sub-packages and modules within a single package across multiple, separate distribution packages. Unlike normal packages, namespace packages don't require an __init__.py file. Automatically importing all submodules within a namespace package serves several purposes, like auto-registration without manually importing, and loading all available plugins in a system. Using pkgutil.iter_modules() The pkgutil.iter_modules() function finds and lists submodules and subpackages within a given package. It returns an iterator containing ModuleInfo objects, each with information about a found module or package ? import pkgutil import ...

Read More

How to create python namespace packages in Python 3?

Sumana Challa
Sumana Challa
Updated on 24-Mar-2026 3K+ Views

Namespace packages are a special type of package introduced in Python 3.3 that allows you to split package contents across multiple directories. If you don't include at least an empty __init__.py file in your package, then your package becomes a namespace package. In Python, a namespace package allows you to spread Python code among several projects. This is useful when you want to release related libraries as separate downloads. Currently, there are three methods for developing namespace packages: Native namespace packages (PEP 420) pkgutil-style namespace packages ...

Read More

Do recursive functions in Python create a new namespace each time the function calls itself?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 248 Views

Yes, recursive functions in Python create a new namespace each time the function calls itself. This is true for any function call, not just recursive ones. However, when objects are passed as parameters, they are passed by reference. The new namespace gets its own copy of this reference, but it still refers to the same object as in the calling function. If you modify the content of that object, the change will be visible in the calling function. How Python Handles Function Calls When the Python interpreter encounters a function call, it creates a frame object that ...

Read More

What does the if __name__ ==

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 83K+ Views

This article explains what the Python expression if __name__ == '__main__' means and how it's used to control code execution. A Python program uses the condition if __name__ == '__main__' to run specific code only when the program is executed directly by the Python interpreter. The code inside the if statement is not executed when the file is imported as a module. Understanding the __name__ Variable The variable __name__ is a special built−in variable in Python. Python has many special variables that begin and end with double underscores, called "dunder" variables (from Double Underscores). In this case, ...

Read More

How to develop programs with Python Namespaced Packages?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 321 Views

In Python, a namespace package allows you to spread Python code among several projects. This is useful when you want to release related libraries as separate downloads while maintaining a unified import structure. Directory Structure Example With the directories Package-1 and Package-2 in PYTHONPATH, you can organize your code as follows − Package-1/ namespace/ __init__.py module1/ __init__.py Package-2/ namespace/ ...

Read More

How to install and import Python modules at runtime?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 2K+ Views

Python allows you to install and import modules at runtime using subprocess to call pip and importlib to dynamically import modules. This is useful for optional dependencies or when you want to handle missing packages gracefully. Modern Approach Using subprocess The recommended way to install packages programmatically is using subprocess instead of calling pip directly ? import subprocess import importlib import sys def install_and_import(package): try: return importlib.import_module(package) except ImportError: print(f"Installing {package}...") ...

Read More

How do we use easy_install to install Python modules?

Pranathi M
Pranathi M
Updated on 24-Mar-2026 6K+ Views

Easy Install was a Python package management tool bundled with setuptools that allowed automatic downloading, compiling, installing, and managing of Python packages. Introduced in 2004, it was groundbreaking for automatically handling dependencies and installing packages from PyPI. However, easy_install is now deprecated and has been replaced by pip as the standard Python package installer. Installing pip using easy_install If you have an older system with only easy_install available, you can use it to install pip ? easy_install pip Basic easy_install Usage To install a package, you simply specify the package name after the ...

Read More

How can I import modules for a Python Azure Function?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 1K+ Views

Azure Functions for Python provides several methods to import and use external modules. While early versions had limitations, modern Azure Functions offers better support for dependency management. Method 1: Using requirements.txt (Recommended) The most straightforward approach is to create a requirements.txt file in your function app root directory − # requirements.txt requests==2.28.2 pandas==1.5.3 numpy==1.24.3 Azure Functions will automatically install these dependencies during deployment. Then import them normally in your function code − import azure.functions as func import requests import pandas as pd def main(req: func.HttpRequest) -> func.HttpResponse: ...

Read More

How to check if a python module exists without importing it?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 1K+ Views

There are several ways to check if a Python module exists without importing it. This is useful when you want to conditionally use a module or handle missing dependencies gracefully. Using importlib.util.find_spec() (Recommended) The modern approach uses importlib.util.find_spec() which is available in Python 3.4+ − import importlib.util def module_exists(module_name): spec = importlib.util.find_spec(module_name) return spec is not None print(module_exists('os')) # Built-in module print(module_exists('nonexistent_module')) True False Using pkgutil.iter_modules() You can iterate over all available modules to check if a specific module ...

Read More

What are the most useful Python modules from the standard library?

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 7K+ Views

The Python Standard Library is a collection of built-in modules that come with every Python installation, eliminating the need to rewrite common functionality. These modules can be imported at the start of your script to access their features. A module is a file containing Python code. For example, a file named 'math.py' would be a module called 'math'. Modules help organize code into reusable components and make programs more manageable. Here's an example of a simple module with an add function ? def add(b, c): # Adding two numbers and returning the ...

Read More
Showing 7781–7790 of 61,297 articles
« Prev 1 777 778 779 780 781 6130 Next »
Advertisements