So I have the script that imports the standard module checkmarc.
import checkdmarc
I want to implement some mock checkmarc that will seat at the same directory as the script.
Now sometime it will be there and I want the script to pull that local module and if it's not there it should take the standard checkmarc.
I know I can do something like if the file in the path exists it'll take that, else do that, but I want to keep the import line just import checkmarc.
From reading: https://docs.python.org/3/whatsnew/2.5.html#pep-328-absolute-and-relative-imports
there's a section of exactly what I need but only for Python 2.4 and lower:
In Python 2.4 and earlier, it will first
look in the package’s directory to perform a relative import, finds
pkg/string.py, imports the contents of that file as the pkg.string
module, and that module is bound to the name string in the pkg.main
module’s namespace.
If there's an elegant way to do it, I'd love to hear.
I don't think you can do this if your custom module has the same name. You can however do something like this:
try:
import customcheckdmarc as checkdmarc
except ImportError:
import checkdmarc as checkdmarc
this will load your customecheckedmarc.py if it's there and will load default checkdmarc otherwise.
Related
I would like a way to detect if my module was executed directly, as in import module or from module import * rather than by import module.submodule (which also executes module), and have this information accessible in module's __init__.py.
Here is a use case:
In Python, a common idiom is to add import statement in a module's __init__.py file, such as to "flatten" the module's namespace and make its submodules accessible directly. Unfortunately, doing so can make loading a specific submodule very slow, as all other siblings imported in __init__.py will also execute.
For instance:
module/
__init__.py
submodule/
__init__.py
...
sibling/
__init__.py
...
By adding to module/__init__.py:
from .submodule import *
from .sibling import *
It is now possible for users of the module to access definitions in submodules without knowing the details of the package structure (i.e. from module import SomeClass, where SomeClass is defined somewhere in submodule and exposed in its own __init__.py file).
However, if I now run submodule directly (as in import module.submodule, by calling python3 -m module.submodule, or even indirectly via pytest) I will also, unavoidably, execute sibling! If sibling is large, this can slow things down for no reason.
I would instead like to write module/__init__.py something like:
if __???__ == 'module':
from .submodule import *
from .sibling import *
Where __???__ gives me the fully qualified name of the import. Any similar mechanism would also work, although I'm mostly interested in the general case (detecting direct executing) rather than this specific example.
What is being desired is will result in undefined behavior (in the sense whether or not the flattened names be importable from module) when we consider how the import system actually works, if it were actually possible.
Hypothetically, if what you want to achieve is possible, where some __dunder__ that will disambiguate which import statement was used to import module/__init__.py (e.g. import module and from module import *, vs import module.submodule. For the first case, module may trigger the subsequent (slow) import to produce a "flattened" version of the desired imports, while the latter case (import module.submodule) will avoid that and thus module will not contain any assignments of the "flattened" imports.
To illustrate the example a bit more, say one may import SiblingClass from module.sibling.SiblingClass by simply doing from module import SiblingClass as the module/__init__.py file executes from .sibling import * statement to create that binding. But then, if executing import module.submodule resulting in the avoidance of that flatten import, we get the following scenario:
import module.submodule
# module.submodule gets imported
from module import SiblingClass
# ImportError will occur
Why is that? This is simply due to how Python imports a file - the source file is executed in its entirety once to assign imports, function and class declarations to the designated names, and be registered to sys.modules under its import name. Importing the module again will not execute the file again, thus if the from .sibling import * statement was not executed during its initial import (i.e. import module.submodule), it will never be executed again during subsequent import of the same module, as the copy produced by the initial import assigned to its module entry in sys.module is returned (unless the module was reloaded manually, the code for the module will be executed again).
You may verify this fact by putting in a print statement into a file, import the corresponding module to see the output produced, and see that no further output will be produced on subsequent import of that module (related: What happens when a module is imported twice?).
Effectively, the desired functionality as described in the question cannot be implemented in Python.
A related thread on this topic: How to only import sub module without exec __init__.py in the package
This is not a complete solution, but standalone py.test (ignore __init__.py files) proposes setting a global flag to detect when in test. This corrects the problem for tests at least, provided the concerned modules don't call each other.
The path is app/base/admin/crud/__init__.py.
I want to import an entire folder as a package like this:
import app.base.admin.crud as cx
But it doesn't work and gives this error:
AttributeError: module 'app.base' has no attribute 'admin'
But when I import it's function like this from app.base.admin.crud import crud, it works.
What's going on here?
See the documentation about packages.
More specifically that part:
[...] when using syntax like import item.subitem.subsubitem, each item except for the last must be a package; the last item can be a module or a package but can’t be a class or function or variable defined in the previous item.
When using the import x.y.z statement alone (without from), you're actually importing a package for usage in your code as x.y.z.something(). Each part of that path must be a proper package (in other words, contain a __init__.py file)
I have a module that wraps another module to insert some shim logic in some functions. The wrapped module uses a settings module mod.settings which I want to expose, but I don't want the users to import it from there, in case I would like to shim something there as well in the future. I want them to import wrapmod.settings.
Importing the module and exporting it works, but is a bit verbose on the client side. It results in having to write settings.thing instead of just thing.
I want the users to be able to do from wrapmod.settings import * and get the same results as if they did from mod.settings import * but right now, only from wrapmod import settings is available. How to I work around this?
If I understand the situation correctly, you're writing a module wrapmod that is intended to transform parts of an existing package mod. The specific part you're transforming is the submodule mod.settings. You've imported the settings module and made your changes to it, but even though it is available as wrapmod.settings, you can't use that module name in an from ... import ... statement.
I think the best way to fix that is to insert the modified module into sys.modules under the new dotted name. This makes Python accept that name as valid even though wrapmod isn't really a package.
So wrapmod would look something like:
import sys
from mod import settings
# modify settings here
sys.modules['wrapmod.settings'] = settings # add this line!
I ended up making a code-generator for a thin wrapper module instead, since the sys.module hacking broke all IDE integration.
from ... import mod
# this is just a pass-through wrapper around mod.settings
__all__ = mod.__all__
# generate pass-through wrapper around mod.settings; doesn't break IDE integration, unlike manual sys.modules editing.
if __name__ == "__main__":
for thing in settings.__all__:
print(thing + " = mod." + thing)
which when run as a script, outputs code that can then be appended to the end of this file.
So, the following is an example. Here's a module (called feedy.py) in let's say, the core directory:
import feedparser
feed = feedparser.parse("http://site.com/feed")
[...]
A bad example, but anyway: my problem is in the main script (parent dir), I have to do
import feedparser
as well as
from core import feedy
However, is there a way to eliminate the need to import feedparser, since it's already imported in feedy.py?
Hope you understand,
Fike.
In principle, yes. However, it's not usually a good idea.
When you import a module, you effectively declare a local variable which is a reference to that module. So in feedy you have an object called feedparser, which happens to be the module feedparser, although you could at any time reassign it to be any other Python object.
When you import feedy, you can refer to any of feedy's exported variables as feedy.name. So in this case, feedy.feedparser is the module feedparser.
However, if you change the way feedy is implemented, so that it doesn't import (or export) feedparser, this will break your main script. In general you don't want to export everything you have defined, although it's fine for a quick hack.
I have a module that conflicts with a built-in module. For example, a myapp.email module defined in myapp/email.py.
I can reference myapp.email anywhere in my code without issue. However, I need to reference the built-in email module from my email module.
# myapp/email.py
from email import message_from_string
It only finds itself, and therefore raises an ImportError, since myapp.email doesn't have a message_from_string method. import email causes the same issue when I try email.message_from_string.
Is there any native support to do this in Python, or am I stuck with renaming my "email" module to something more specific?
You will want to read about Absolute and Relative Imports which addresses this very problem. Use:
from __future__ import absolute_import
Using that, any unadorned package name will always refer to the top level package. You will then need to use relative imports (from .email import ...) to access your own package.
NOTE: The above from ... line needs to be put into any 2.x Python .py files above the import ... lines you're using. In Python 3.x this is the default behavior and so is no longer needed.