Found 10784 Articles for Python

What are Python namespaces all about?

Rajendra Dharmkar
Updated on 30-Jul-2019 22:30:20

152 Views

Namespace is a way to implement scope. In Python, each package, module, class, function and method function owns a "namespace" in which variable names are resolved. When a function,  module or package is evaluated (that is, starts execution), a namespace is created. Think of it as an "evaluation context". When a function, etc., finishes execution, the namespace is dropped. The variables are dropped. Plus there's a global namespace that's used if the name isn't in the local namespace.Each variable name is checked in the local namespace (the body of the function, the module, etc.), and then checked in the global ... Read More

What does the if __name__ ==

Sarika Singh
Updated on 23-Aug-2023 12:53:52

82K+ Views

This article explains what the Python code expression if __name__ == '__main__' means. A Python programme uses the condition if __name__ == '__main__' to only run the code inside the if statement when the program is run directly by the Python interpreter. The code inside the if statement is not executed when the file's code is imported as a module. What is __main__? The word "__name__" denotes a unique variable in Python. Python has a large number of special variables that begin and end with double underscores. They are referred to as dunder to keep it brief (from Double Underscores). ... Read More

How to develop programs with Python Namespaced Packages?

Rajendra Dharmkar
Updated on 01-Oct-2019 11:17:05

162 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. For example, with the directories Package-1 and Package-2 in PYTHONPATH, Package-1/namespace/__init__.py Package-1/namespace/module1/__init__.py Package-2/namespace/__init__.py Package-2/namespace/module2/__init__.py the end-user can import namespace.module1 and import namespace.module2.On Python 3.3, you don't have to do anything, just don't put any __init__.py in your namespace package directories and it will just work. This is because Python 3.3 introduces implicit namespace packages.On older versions, there's a standard module, called pkgutil, with which you can 'append' modules to a given namespace. You ... Read More

How to install and import Python modules at runtime?

Rajendra Dharmkar
Updated on 01-Oct-2019 11:18:18

1K+ Views

You can use pip to install packages at runtime and importlib.import_module(moduleName) to import module by using its name as a string. For example,import pip import importlib def import_with_auto_install(package):     try:         return importlib.import_module(package)     except ImportError:         pip.main(['install', package])     return importlib.import_module(package) # Example if __name__ == '__main__':     scrapy = import_with_auto_install('scrapy')     print(scrapy)The above script installs the scrapy module and imports it when installation of the module completes.

How do we use easy_install to install Python modules?

Pranathi M
Updated on 16-Sep-2022 08:15:57

5K+ Views

Easy Install is a python module that is bundled with setuptools (easy_install) that allows you to download, compile, install, and manage Python packages automatically. It was included in setuptools in 2004 and is now deprecated. It was remarkable at the time for automatically installing dependencies and installing packages from PyPI using requirement specifiers. Pip was released later in 2008 as a replacement for easy install, albeit it was still primarily based on setuptools components. Installing Python modules should be done with pip rather than easy install. You can use easy_install to install pip if you have it. The following line ... Read More

How can I import modules for a Python Azure Function?

Rajendra Dharmkar
Updated on 17-Feb-2020 07:41:36

921 Views

As of writing this, Python support for Azure Functions is experimental. So right now there is no way to directly get a module from a package manager to be installed on your instance. You'll need to bring your own modules with code. No modules are available by default on Azure Functions. You can add them by uploading it via the portal UX or kudu (which is handy for lots of files).If you don't mind using virtualenv, there is an alternative.Create your python script on Azure Functions.Open a Kudu console and cd to your script location.Create a virtualenv in this folder ... Read More

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

Rajendra Dharmkar
Updated on 01-Oct-2019 11:21:15

765 Views

To check if you can import something in Python 2, you can use imp module with try...except. For example, import imp try:     imp.find_module('eggs')     found = True except ImportError:     found = False print foundThis will give you the output:FalseYou can also use iter_modules from the pkgutil module to iterate over all modules to find if specified module exists. For example, from pkgutil import iter_modules def module_exists(module_name):     return module_name in (name for loader, name, ispkg in iter_modules()) print module_exists('scrapy')This will give the output:TrueThis is because this module is installed on my PC.Or if you ... Read More

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

Sarika Singh
Updated on 14-Nov-2022 07:30:09

6K+ Views

The Python Standard Library is a collection of script modules that may be used by a Python program, making it unnecessary to rewrite frequently used commands and streamlining the development process. By "calling/importing" them at the start of a script, they can be used. A module is a file that contains Python code; an ‘coding.py’ file would be a module with the name ‘coding’.We utilise modules to divide complicated programmes into smaller, more manageable pieces. Modules also allow for the reuse of code. In the following example, a module called ‘coding’ contains a function called add() that we developed. The ... Read More

How do I calculate the date six months from the current date using the datetime Python module?

Sarika Singh
Updated on 14-Nov-2022 08:09:49

2K+ Views

Python does not have a data type for dates, but we may import the datetime module to work with dates as date objects. This article tells about how to display the current date by importing the datetime module. Using relativedelta() function The relativedelta type is intended to be applied to an existing datetime and can either indicate a period of time or replace specific elements of that datetime. Example The Python datetime module can be used to get the date six months from the current date. The code is shown below − from datetime import date from dateutil.relativedelta import relativedelta ... Read More

How to prohibit a Python module from calling other modules?

Rajendra Dharmkar
Updated on 30-Jul-2019 22:30:20

159 Views

You can use "Sandboxed Python". A "Sandboxed Python" would let you permit or forbid modules, limit execution slices, permit or deny network traffic, constrain filesystem access to a particular directory (floated as "/"), and so on. It is also referred to as RestrictedExecution. There are many ways to implement sandboxing on Python. You could Modify the CPython Runtime, Use Another Runtime, Use Operating System Support, etc to implement such a sandbox. You can read more about sandboxing at: https://wiki.python.org/moin/SandboxedPythonPypi has a package called RestrictedPython(https://pypi.python.org/pypi/RestrictedPython) that is a defined subset of the Python language which allows to provide a program input ... Read More

Advertisements