Rajendra Dharmkar has Published 452 Articles

How to make unique objects accessible in other modules of Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 01-Oct-2019 11:28:14

234 Views

This is basically the idea of a singleton object. So if you have instantiated an obect and want to access it across different module, you can use 2 approaches, first being you assign that variable to the module you imported under a variable name. For example, you have a object ... Read More

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

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 01-Oct-2019 11:25:13

546 Views

The "from module import *" statement is used to import all submodules from a Python package/module. For example, if you want to import all modules from your module(say nyModule) and do not want to prefix "myModule." while calling them, you can do it as follows:>>> from myModule import *Note that ... Read More

Explain the visibility of global variables in imported modules in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 01-Oct-2019 11:24:41

877 Views

Globals in Python are global to a module, not across all modules. (Unlike C, where a global is the same across all implementation files unless you explicitly make it static.). If you need truly global variables from imported modules, you can set those at an attribute of the module where ... Read More

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

Rajendra Dharmkar

Rajendra Dharmkar

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

763 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 ... Read More

How to install and import Python modules at runtime?

Rajendra Dharmkar

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])     ... Read More

How to develop programs with Python Namespaced Packages?

Rajendra Dharmkar

Rajendra Dharmkar

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

160 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 ... Read More

How we can import Python modules without installing?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 01-Oct-2019 10:51:33

4K+ Views

Yes there are ways to import Python modules without installing. If you are not able to install modules on a machine(due to not having enough permissions), you could use either virtualenv or save the module files in another directory and use the following code to allow Python to search for ... Read More

How we can copy Python modules from one system to another?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 01-Oct-2019 10:50:57

4K+ Views

If you have your own Python modules you want to copy, you can simply copy them and run on other systems with Python installed. If you want to copy installed modules, the best way is to install the same version of Python on the second system. Then run$ pip freeze ... Read More

How I can install unidecode python module on Linux?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 01-Oct-2019 10:49:13

1K+ Views

To install unidecode or any other python module you need pip installed(python package manager). If you have Python 2 >=2.7.9 or Python 3 >=3.4 installed from python.org, you will already have pip and setuptools, but will need to upgrade to the latest version:On Linux or macOS:pip install -U pip setuptoolsOn ... Read More

How to use remote python modules?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 01-Oct-2019 10:47:54

2K+ Views

There are ways to import Python modules remotely. It is not recommended to do so though as it will slow down your app. You can use the knockout module to achieve this. To install knockout use:$ pip install knockoutNow in order to import modules remotely, you can use knockout like:>>> ... Read More

Advertisements