python-importlib

What is the default binding to the `__import__` attribute of the module `builtin`?

时光总嘲笑我的痴心妄想 提交于 2019-12-25 08:59:16
问题 From Python in a Nutshell Custom Importers An advanced, rarely needed functionality that Python offers is the ability to change the semantics of some or all import and from statements. Rebinding __import__ You can rebind the __import__ attribute of the module builtin to your own custom importer function—for example, one using the generic built-in-wrapping technique shown in “Python built-ins” on page 174. In "You can rebind the __import__ attribute of the module builtin ", should "the module

Using my package throws ModuleNotFoundError: No module named 'my-package' in Travis

被刻印的时光 ゝ 提交于 2019-12-24 06:51:08
问题 I am trying to use Travis CI for continuous integration but have been dealing with a lot of issues since it is my first time with Travis. Lately, I am running in to the following build fail: ModuleNotFoundError: No module named 'my-package' Which happens when in the following line of code in my db.py file: import importlib db_conf = importlib.import_module("my-package.conf.db_conf") I should mention that I am using importlib since my repository has dashes in its name and based on suggestion

Error when using importlib.util to check for library

非 Y 不嫁゛ 提交于 2019-12-23 07:39:45
问题 I'm trying to use the importlib library to verify whether the nmap library is installed on the computer executing the script in Python 3.5.2 I'm trying to use importlib.util.find_spec("nmap") but receive the following error. >>> import importlib >>> importlib.util.find_spec("nmap") Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: module 'importlib' has no attribute 'util' Can someone tell me where I'm going wrong? EDIT I was able to get the function to

Import class from module dynamically

坚强是说给别人听的谎言 提交于 2019-12-21 07:13:18
问题 I have class called 'my_class' placed in 'my_module' . And I need to import this class. I tried to do it like this: import importlib result = importlib.import_module('my_module.my_class') but it says: ImportError: No module named 'my_module.my_class'; 'my_module' is not a package So. As I can see it works only for modules, but can't handle classes. How can I import a class from a module? 回答1: It is expecting my_module to be a package containing a module named 'my_class' . If you need to

Problems when converting from imp to importlib in python 3.4

天大地大妈咪最大 提交于 2019-12-12 10:53:50
问题 I've made a Python application which can load plugins. These plugins are loaded based on a name and path. I am currently using pluginModule = imp.load_source(pluginModuleName, pluginModulePath) and then getting a class instance in the module this way # Load the module class and initialize it. if hasattr(pluginModule, pluginClassName): try: pluginClassInst = getattr(pluginModule, pluginClassName)() except Exception as e: errorMsg = ('In plugin module [{}], {}'.format(os.path.basename

Python use importlib to import a module from a package directory

和自甴很熟 提交于 2019-12-11 23:25:27
问题 I'm trying to import PySide2 dynamically with importlib , because statically with import isn't sufficient for my application. I need importlib because in the end it will be used with pyinstaller to create a single executable that can import PySide2 dynamically, not just from the single executable. I've copied an entire PySide2 package directory that was downloaded with pip. This PySide2 version is older than the one that I'm using by default, and when the "PySide2" directory is present in my

Pickling objects imported with importlib.util

孤街浪徒 提交于 2019-12-10 19:26:23
问题 I ran into a problem while using Python's pickle. I need to load some Python modules by giving their file paths to importlib.util, like so: import importlib.util spec = importlib.util.spec_from_file_location('custom', 'C:\path\to\.py\file.py') module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) I would like to instantiate some objects from the loaded module and serialize them for later use, but when I try: pickle.dump(module.ObjectFromModule(), open('C:\object

How can I check on runtime that a python module is valid without importing it?

白昼怎懂夜的黑 提交于 2019-12-10 19:23:06
问题 I have a package containing subpackages only one of which I need imported during runtime - but I need to test they are valid. Here is my folder structure: game/ __init__.py game1/ __init__.py constants.py ... game2/ __init__.py constants.py ... For now the code that runs on boot does: import pkgutil import game as _game # Detect the known games for importer,modname,ispkg in pkgutil.iter_modules(_game.__path__): if not ispkg: continue # game support modules are packages # Equivalent of "from

Python how to alias module name (rename with preserving backward compatibility)

会有一股神秘感。 提交于 2019-12-06 02:23:57
问题 I have a python package named foo , which i use in imports: import foo.conf from foo.core import Something Now i need to rename the foo module into something else, let's say bar , so i want to do: import bar.conf from bar.core import Something but i want to maintain backward compatibility with existing code, so the old ( foo. ) imports should work as well and do the same as the bar. imports. How can this be accomplished in python 2.7? 回答1: This forces you to keep a foo directory, but I think

How to implement an import hook that can modify the source code on the fly using importlib?

会有一股神秘感。 提交于 2019-12-04 08:09:40
问题 Using the deprecated module imp , I can write a custom import hook that modifies the source code of a module on the fly, prior to importation/execution by Python. Given the source code as a string named source below, the essential code needed to create a module is the following: module = imp.new_module(name) sys.modules[name] = module exec(source, module.__dict__) Since imp is deprecated, I would like to do something similar with importlib . [EDIT: there are other imp methods that need to be