Check If a Python Module Exists Without Importing It

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

1K+ 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

HTML window.screenX Property

AmitDiwan
Updated on 01-Oct-2019 11:19:00

160 Views

The HTML Window screenX property returns the horizontal coordinates of the window relative to the screen. It is supported by all browsers.SyntaxFollowing is the syntax −window.screenXLet us see an example of HTML Window screenX Property −Example Live Demo    body {       color: #000;       height: 100vh;       background-color: #8BC6EC;       background-image: linear-gradient(135deg, #8BC6EC 0%, #9599E2 100%);       text-align: center;    }    .btn {       background: #db133a;       border: none;       height: 2rem;       border-radius: 2px;       width: ... Read More

Install and Import Python Modules at Runtime

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

2K+ 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.

Develop Programs with Python Namespaced Packages

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

268 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

HTML window.screenTop Property

AmitDiwan
Updated on 01-Oct-2019 11:16:08

242 Views

The HTML Window screenTop property returns the vertical coordinates of the window relative to the screen. It is not supported by firefox browser.SyntaxFollowing is the syntax −window.screenTopLet us see an example of HTML Window screenTop Property −Example Live Demo    body {       color: #000;       height: 100vh;       background-color: #8BC6EC;       background-image: linear-gradient(135deg, #8BC6EC 0%, #9599E2 100%);       text-align: center;    }    .btn {       background: #db133a;       border: none;       height: 2rem;       border-radius: 2px;       ... Read More

HTML window.screenLeft Property

AmitDiwan
Updated on 01-Oct-2019 11:13:05

191 Views

The HTML Window screenLeft property returns the horizontal coordinates of the window relative to the screen. It is not supported by firefox browser.SyntaxFollowing is the syntax −window.screenLeftLet us see an example of HTML Window screenLeft Property −Example Live Demo    body {       color: #000;       height: 100vh;       background-color: #8BC6EC;       background-image: linear-gradient(135deg, #8BC6EC 0%, #9599E2 100%);       text-align: center;    }    .btn {       background: #db133a;       border: none;       height: 2rem;       border-radius: 2px;       ... Read More

HTML Window Name Property

AmitDiwan
Updated on 01-Oct-2019 11:10:33

368 Views

The HTML Window name property returns and modify the name of the window.SyntaxFollowing is the syntax −1. Returning namewindow.name2. Adding namewindow.name=”text”Let us see an example of HTML Window name Property −Example Live Demo    body {       color: #000;       height: 100vh;       background-color: #8BC6EC;       background-image: linear-gradient(135deg, #8BC6EC 0%, #9599E2 100%);       text-align: center;    }    .btn {       background: #db133a;       border: none;       height: 2rem;       border-radius: 2px;       width: 40%;       display: ... Read More

HTML window.outerWidth Property

AmitDiwan
Updated on 01-Oct-2019 11:08:13

174 Views

The HTML Window outerWidth property returns the width of the browser window including all interface elements.SyntaxFollowing is the syntax −window.outerWidthLet us see an example of HTML Window outerWidth Property −Example Live Demo    body {       color: #000;       height: 100vh;       background-color: #8BC6EC;       background-image: linear-gradient(135deg, #8BC6EC 0%, #9599E2 100%);       text-align: center;    }    .btn {       background: #db133a;       border: none;       height: 2rem;       border-radius: 2px;       width: 40%;       display: block; ... Read More

HTML window.outerHeight Property

AmitDiwan
Updated on 01-Oct-2019 11:05:08

167 Views

The HTML Window outerHeight property returns the height of the browser window including all interface elements.SyntaxFollowing is the syntax −window.outerHeightLet us see an example of HTML Window outerHeight Property −Example Live Demo    body {       color: #000;       height: 100vh;       background-color: #8BC6EC;       background-image: linear-gradient(135deg, #8BC6EC 0%, #9599E2 100%);       text-align: center;    }    .btn {       background: #db133a;       border: none;       height: 2rem;       border-radius: 2px;       width: 40%;       display: block; ... Read More

HTML window.innerWidth Property

AmitDiwan
Updated on 01-Oct-2019 11:02:45

318 Views

The HTML Window innerWidth property returns the width of the content area of a window in an HTML document.SyntaxFollowing is the syntax −window.innerWidthLet us see an example of HTML Window innerWidth Property −Example Live Demo    body {       color: #000;       height: 100vh;       background-color: #8BC6EC;       background-image: linear-gradient(135deg, #8BC6EC 0%, #9599E2 100%);       text-align: center;    }    .btn {       background: #db133a;       border: none;       height: 2rem;       border-radius: 2px;       width: 40%;     ... Read More

Advertisements