python-importlib

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

心不动则不痛 提交于 2019-12-04 07:21:42
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? This forces you to keep a foo directory, but I think it the best way to get this to work. Directory setup: bar ├── __init__.py └── baz.py foo └── __init__.py foo

Python 3.5+: How to dynamically import a module given the full file path (in the presence of implicit sibling imports)?

隐身守侯 提交于 2019-12-03 05:38:12
问题 Question The standard library clearly documents how to import source files directly (given the absolute file path to the source file), but this approach does not work if that source file uses implicit sibling imports as described in the example below. How could that example be adapted to work in the presence of implicit sibling imports? I already checked out this and this other Stackoverflow questions on the topic, but they do not address implicit sibling imports within the file being

Cannot import importlib

戏子无情 提交于 2019-12-02 15:01:44
问题 I am trying to use Listfield from djangotoolbox.fields but it is giving me an error saying : Traceback (most recent call last): File "/Library/Python/2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper fn(*args, **kwargs) File "/Library/Python/2.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run autoreload.raise_last_exception() File "/Library/Python/2.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception six.reraise(

Cannot import importlib

依然范特西╮ 提交于 2019-12-02 12:38:31
I am trying to use Listfield from djangotoolbox.fields but it is giving me an error saying : Traceback (most recent call last): File "/Library/Python/2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper fn(*args, **kwargs) File "/Library/Python/2.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run autoreload.raise_last_exception() File "/Library/Python/2.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception six.reraise(*_exception) File "/Library/Python/2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper

Deleted a module's function on interactive. How to re-import? importlib.reload not helping

不打扰是莪最后的温柔 提交于 2019-12-01 08:01:16
I've deleted a (package builtin) function on ipython: Python 3.6.4 |Anaconda custom (64-bit)| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bit (AMD64)] Type 'copyright', 'credits' or 'license' for more information IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help. In [1]: import math In [2]: math.cos(0) Out[2]: 1.0 In [3]: del math.cos In [4]: math.cos(0) --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-4-9cdcc157d079> in <module>() ----> 1 math.cos(0) AttributeError: module 'math'

Deleted a module's function on interactive. How to re-import? importlib.reload not helping

痞子三分冷 提交于 2019-12-01 06:05:00
问题 I've deleted a (package builtin) function on ipython: Python 3.6.4 |Anaconda custom (64-bit)| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bit (AMD64)] Type 'copyright', 'credits' or 'license' for more information IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help. In [1]: import math In [2]: math.cos(0) Out[2]: 1.0 In [3]: del math.cos In [4]: math.cos(0) --------------------------------------------------------------------------- AttributeError Traceback (most recent call

Import error: DLL load failed in Jupyter notebook but working in .py file

主宰稳场 提交于 2019-11-30 13:17:30
I installed BreakoutDetection the module in Anaconda environment. When I tried to import the module using import breakout_detection in jupyter notebook, I get the below error --------------------------------------------------------------------------- ImportError Traceback (most recent call last) <ipython-input-18-96c0fdb15b96> in <module>() ----> 1 import breakout_detection C:\Users\sgadiyar\AppData\Local\Continuum\Anaconda2\lib\site-packages\breakout_detection.py in <module>() 15 except ImportError: 16 return importlib.import_module('_breakout_detection') ---> 17 _breakout_detection = swig

Python 3.5+: How to dynamically import a module given the full file path (in the presence of implicit sibling imports)?

纵饮孤独 提交于 2019-11-29 23:55:54
Question The standard library clearly documents how to import source files directly (given the absolute file path to the source file), but this approach does not work if that source file uses implicit sibling imports as described in the example below. How could that example be adapted to work in the presence of implicit sibling imports? I already checked out this and this other Stackoverflow questions on the topic, but they do not address implicit sibling imports within the file being imported by hand. Setup/Example Here's an illustrative example Directory structure: root/ - directory/ - app

How to modify imported source code on-the-fly?

血红的双手。 提交于 2019-11-27 22:20:54
问题 Suppose I have a module file like this: # my_module.py print("hello") Then I have a simple script: # my_script.py import my_module This will print "hello" . Let's say I want to "override" the print() function so it returns "world" instead. How could I do this programmatically (without manually modifying my_module.py )? What I thought is that I need somehow to modify the source code of my_module before or while importing it. Obvisouly, I cannot do this after importing it so solution using

Python: How to import all methods and attributes from a module dynamically

纵饮孤独 提交于 2019-11-27 03:56:33
问题 I'd like to load a module dynamically, given its string name (from an environment variable). I'm using Python 2.7. I know I can do something like: import os, importlib my_module = importlib.import_module(os.environ.get('SETTINGS_MODULE')) This is roughly equivalent to import my_settings (where SETTINGS_MODULE = 'my_settings' ). The problem is, I need something equivalent to from my_settings import * since I'd like to be able to access all methods and variables in the module. I've tried import