How to recursively import subpackages in Python?
up vote
0
down vote
favorite
I have an app structure that looks like this:
foo/
bar/
spam.py
baz/
eggs.py
I want to be able to import one package (foo), and also have all modules (spam and eggs) from all subpackages (bar and baz) available in the calling namespace.
Ex:
#!/usr/bin/python
import foo
foo.bar.spam.blah()
foo.baz.eggs.blah()
Currently, I've devised the following __init__.py template, which I have placed in all my package directories:
#!/usr/bin/python
from os.path import dirname, basename, isfile, isdir
import glob
__all__ =
paths = glob.glob(dirname(__file__) + "/*")
for p in paths:
name = basename(p)
if isfile(p) and p.endswith('.py') and not p.endswith('__init__.py'):
__all__.append(name[:-3])
__import__(name[:-3], globals(), locals(), , -1)
elif isdir(p):
if isfile(p + '/__init__.py'):
__all__.append(name)
__import__(name, globals(), locals(), , -1)
This seems to work, but I sort of have no idea what it's doing. I don't understand Pythons namespace nor import system. Is this code safe? I sort of threw it together using a bunch of Google searches. What potential points of failure are involved here?
I intended for this code to instruct a package to import all modules and subpackages within the package root... I sort of just guessed that if a package automatically imports its modules from the package namespace directly, then the modules would be accessible using package.module from the calling namespace. I think this is because the modules are added to the package namespace, which is then added to the calling namespace? Based on this I figured if I put this in all my packages and subpackages, it should ultimately provide access to everything while preserving the hierarchal structure that I have set up? Not sure if the code does precisely and exclusively what I was aiming for, or if it opens other doors that I didn't mean to open.
Could someone tell me:
A.) Is this code safe and/or structurally sound?
B.) what is the standard way of approaching this problem in Python?
python import namespaces python-import
add a comment |
up vote
0
down vote
favorite
I have an app structure that looks like this:
foo/
bar/
spam.py
baz/
eggs.py
I want to be able to import one package (foo), and also have all modules (spam and eggs) from all subpackages (bar and baz) available in the calling namespace.
Ex:
#!/usr/bin/python
import foo
foo.bar.spam.blah()
foo.baz.eggs.blah()
Currently, I've devised the following __init__.py template, which I have placed in all my package directories:
#!/usr/bin/python
from os.path import dirname, basename, isfile, isdir
import glob
__all__ =
paths = glob.glob(dirname(__file__) + "/*")
for p in paths:
name = basename(p)
if isfile(p) and p.endswith('.py') and not p.endswith('__init__.py'):
__all__.append(name[:-3])
__import__(name[:-3], globals(), locals(), , -1)
elif isdir(p):
if isfile(p + '/__init__.py'):
__all__.append(name)
__import__(name, globals(), locals(), , -1)
This seems to work, but I sort of have no idea what it's doing. I don't understand Pythons namespace nor import system. Is this code safe? I sort of threw it together using a bunch of Google searches. What potential points of failure are involved here?
I intended for this code to instruct a package to import all modules and subpackages within the package root... I sort of just guessed that if a package automatically imports its modules from the package namespace directly, then the modules would be accessible using package.module from the calling namespace. I think this is because the modules are added to the package namespace, which is then added to the calling namespace? Based on this I figured if I put this in all my packages and subpackages, it should ultimately provide access to everything while preserving the hierarchal structure that I have set up? Not sure if the code does precisely and exclusively what I was aiming for, or if it opens other doors that I didn't mean to open.
Could someone tell me:
A.) Is this code safe and/or structurally sound?
B.) what is the standard way of approaching this problem in Python?
python import namespaces python-import
1
The usual way to do this is to explicitly import the modules you want, keeping the hierarchical structure. If you want to flatten the structure, the usual way is to explicitly import the nested modules into the top-level__init__.py
file. Trying to automatically import nested modules seems like overkill. How many will you have, and how frequently do you add new ones?
– Ned Batchelder
Nov 21 at 22:34
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have an app structure that looks like this:
foo/
bar/
spam.py
baz/
eggs.py
I want to be able to import one package (foo), and also have all modules (spam and eggs) from all subpackages (bar and baz) available in the calling namespace.
Ex:
#!/usr/bin/python
import foo
foo.bar.spam.blah()
foo.baz.eggs.blah()
Currently, I've devised the following __init__.py template, which I have placed in all my package directories:
#!/usr/bin/python
from os.path import dirname, basename, isfile, isdir
import glob
__all__ =
paths = glob.glob(dirname(__file__) + "/*")
for p in paths:
name = basename(p)
if isfile(p) and p.endswith('.py') and not p.endswith('__init__.py'):
__all__.append(name[:-3])
__import__(name[:-3], globals(), locals(), , -1)
elif isdir(p):
if isfile(p + '/__init__.py'):
__all__.append(name)
__import__(name, globals(), locals(), , -1)
This seems to work, but I sort of have no idea what it's doing. I don't understand Pythons namespace nor import system. Is this code safe? I sort of threw it together using a bunch of Google searches. What potential points of failure are involved here?
I intended for this code to instruct a package to import all modules and subpackages within the package root... I sort of just guessed that if a package automatically imports its modules from the package namespace directly, then the modules would be accessible using package.module from the calling namespace. I think this is because the modules are added to the package namespace, which is then added to the calling namespace? Based on this I figured if I put this in all my packages and subpackages, it should ultimately provide access to everything while preserving the hierarchal structure that I have set up? Not sure if the code does precisely and exclusively what I was aiming for, or if it opens other doors that I didn't mean to open.
Could someone tell me:
A.) Is this code safe and/or structurally sound?
B.) what is the standard way of approaching this problem in Python?
python import namespaces python-import
I have an app structure that looks like this:
foo/
bar/
spam.py
baz/
eggs.py
I want to be able to import one package (foo), and also have all modules (spam and eggs) from all subpackages (bar and baz) available in the calling namespace.
Ex:
#!/usr/bin/python
import foo
foo.bar.spam.blah()
foo.baz.eggs.blah()
Currently, I've devised the following __init__.py template, which I have placed in all my package directories:
#!/usr/bin/python
from os.path import dirname, basename, isfile, isdir
import glob
__all__ =
paths = glob.glob(dirname(__file__) + "/*")
for p in paths:
name = basename(p)
if isfile(p) and p.endswith('.py') and not p.endswith('__init__.py'):
__all__.append(name[:-3])
__import__(name[:-3], globals(), locals(), , -1)
elif isdir(p):
if isfile(p + '/__init__.py'):
__all__.append(name)
__import__(name, globals(), locals(), , -1)
This seems to work, but I sort of have no idea what it's doing. I don't understand Pythons namespace nor import system. Is this code safe? I sort of threw it together using a bunch of Google searches. What potential points of failure are involved here?
I intended for this code to instruct a package to import all modules and subpackages within the package root... I sort of just guessed that if a package automatically imports its modules from the package namespace directly, then the modules would be accessible using package.module from the calling namespace. I think this is because the modules are added to the package namespace, which is then added to the calling namespace? Based on this I figured if I put this in all my packages and subpackages, it should ultimately provide access to everything while preserving the hierarchal structure that I have set up? Not sure if the code does precisely and exclusively what I was aiming for, or if it opens other doors that I didn't mean to open.
Could someone tell me:
A.) Is this code safe and/or structurally sound?
B.) what is the standard way of approaching this problem in Python?
python import namespaces python-import
python import namespaces python-import
asked Nov 21 at 22:25
Nolan
565
565
1
The usual way to do this is to explicitly import the modules you want, keeping the hierarchical structure. If you want to flatten the structure, the usual way is to explicitly import the nested modules into the top-level__init__.py
file. Trying to automatically import nested modules seems like overkill. How many will you have, and how frequently do you add new ones?
– Ned Batchelder
Nov 21 at 22:34
add a comment |
1
The usual way to do this is to explicitly import the modules you want, keeping the hierarchical structure. If you want to flatten the structure, the usual way is to explicitly import the nested modules into the top-level__init__.py
file. Trying to automatically import nested modules seems like overkill. How many will you have, and how frequently do you add new ones?
– Ned Batchelder
Nov 21 at 22:34
1
1
The usual way to do this is to explicitly import the modules you want, keeping the hierarchical structure. If you want to flatten the structure, the usual way is to explicitly import the nested modules into the top-level
__init__.py
file. Trying to automatically import nested modules seems like overkill. How many will you have, and how frequently do you add new ones?– Ned Batchelder
Nov 21 at 22:34
The usual way to do this is to explicitly import the modules you want, keeping the hierarchical structure. If you want to flatten the structure, the usual way is to explicitly import the nested modules into the top-level
__init__.py
file. Trying to automatically import nested modules seems like overkill. How many will you have, and how frequently do you add new ones?– Ned Batchelder
Nov 21 at 22:34
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421295%2fhow-to-recursively-import-subpackages-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421295%2fhow-to-recursively-import-subpackages-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
The usual way to do this is to explicitly import the modules you want, keeping the hierarchical structure. If you want to flatten the structure, the usual way is to explicitly import the nested modules into the top-level
__init__.py
file. Trying to automatically import nested modules seems like overkill. How many will you have, and how frequently do you add new ones?– Ned Batchelder
Nov 21 at 22:34