Python - access module from package that is not imported through __init__.py?
I'm using one package that with __init__.py
import only one variable from module, but whole module itself is not exposed. Is there a way to access that module?
Lets look in this case:
Whole package:
test_package/
├── __init__.py
└── test_me.py
Now contents:
__init__.py:
from .test_me import test_me
test_me.py:
STATIC = 'static'
class Test:
pass
test_me = Test()
Now if I import package test_package
. I can only access variable test_me
, which is an instance of Test
class. Though I can't access STATIC
variable, because module itself was not exposed.
Is there a way to access test_me
module in this case and not only one of its variables?
P.S. If I use sys
to append path directly to that package's module, it throws error that such module does not exist when I try to import it.
python python-3.x python-import
add a comment |
I'm using one package that with __init__.py
import only one variable from module, but whole module itself is not exposed. Is there a way to access that module?
Lets look in this case:
Whole package:
test_package/
├── __init__.py
└── test_me.py
Now contents:
__init__.py:
from .test_me import test_me
test_me.py:
STATIC = 'static'
class Test:
pass
test_me = Test()
Now if I import package test_package
. I can only access variable test_me
, which is an instance of Test
class. Though I can't access STATIC
variable, because module itself was not exposed.
Is there a way to access test_me
module in this case and not only one of its variables?
P.S. If I use sys
to append path directly to that package's module, it throws error that such module does not exist when I try to import it.
python python-3.x python-import
What do you want to do exactly? Do you want to access thetest_package.STATIC
after youimport test_package
?
– feliks
Jan 2 at 16:25
@feliks yes, in my real case I need to use function that lives inside that module. In example case I want STATIC variable. Idea is su access other attributes than were exposed. As in an example.
– Andrius
Jan 2 at 16:27
add a comment |
I'm using one package that with __init__.py
import only one variable from module, but whole module itself is not exposed. Is there a way to access that module?
Lets look in this case:
Whole package:
test_package/
├── __init__.py
└── test_me.py
Now contents:
__init__.py:
from .test_me import test_me
test_me.py:
STATIC = 'static'
class Test:
pass
test_me = Test()
Now if I import package test_package
. I can only access variable test_me
, which is an instance of Test
class. Though I can't access STATIC
variable, because module itself was not exposed.
Is there a way to access test_me
module in this case and not only one of its variables?
P.S. If I use sys
to append path directly to that package's module, it throws error that such module does not exist when I try to import it.
python python-3.x python-import
I'm using one package that with __init__.py
import only one variable from module, but whole module itself is not exposed. Is there a way to access that module?
Lets look in this case:
Whole package:
test_package/
├── __init__.py
└── test_me.py
Now contents:
__init__.py:
from .test_me import test_me
test_me.py:
STATIC = 'static'
class Test:
pass
test_me = Test()
Now if I import package test_package
. I can only access variable test_me
, which is an instance of Test
class. Though I can't access STATIC
variable, because module itself was not exposed.
Is there a way to access test_me
module in this case and not only one of its variables?
P.S. If I use sys
to append path directly to that package's module, it throws error that such module does not exist when I try to import it.
python python-3.x python-import
python python-3.x python-import
asked Jan 2 at 16:14
AndriusAndrius
5,8802490157
5,8802490157
What do you want to do exactly? Do you want to access thetest_package.STATIC
after youimport test_package
?
– feliks
Jan 2 at 16:25
@feliks yes, in my real case I need to use function that lives inside that module. In example case I want STATIC variable. Idea is su access other attributes than were exposed. As in an example.
– Andrius
Jan 2 at 16:27
add a comment |
What do you want to do exactly? Do you want to access thetest_package.STATIC
after youimport test_package
?
– feliks
Jan 2 at 16:25
@feliks yes, in my real case I need to use function that lives inside that module. In example case I want STATIC variable. Idea is su access other attributes than were exposed. As in an example.
– Andrius
Jan 2 at 16:27
What do you want to do exactly? Do you want to access the
test_package.STATIC
after you import test_package
?– feliks
Jan 2 at 16:25
What do you want to do exactly? Do you want to access the
test_package.STATIC
after you import test_package
?– feliks
Jan 2 at 16:25
@feliks yes, in my real case I need to use function that lives inside that module. In example case I want STATIC variable. Idea is su access other attributes than were exposed. As in an example.
– Andrius
Jan 2 at 16:27
@feliks yes, in my real case I need to use function that lives inside that module. In example case I want STATIC variable. Idea is su access other attributes than were exposed. As in an example.
– Andrius
Jan 2 at 16:27
add a comment |
2 Answers
2
active
oldest
votes
You need to import them through __init__.py
, so change its contents to:
from .test_me import test_me, STATIC
Now the following will work:
import test_package
print(test_package.STATIC)
Well of course it will work. I know that. The question was how to access it if it was not imported. If its even possible? The package is not mine, so I cant just modify it. Or I would need to fork that project and customize it. Which I dont want to do it.
– Andrius
Jan 2 at 16:36
add a comment |
If you add the package directory to your path, Python can import any file in that directory as if it were a module by itself.
import sys
sys.path.extend(test_package.__path__)
import test_me
print(test_me.STATIC)
Doesn't seem to work (or am I missing something?). If I do exactly as you described, then I getNameError: name 'test_package' is not defined
. If I first importtest_package
normally and then try to do your way, then when I try to doimport test_me
, I getImportError: No module named 'test_me'
– Andrius
Jan 3 at 7:36
@Andrius I tested it and it worked for me. Is your test setup exactly as you show in the question? Which version of Python are you using?
– Mark Ransom
Jan 3 at 14:41
Well i copy pasted the content, so it should be the same. Python 3
– Andrius
Jan 3 at 14:48
@Andrius I apologize, when I tested I used a hard-coded path. I didn't realize thattest_package.__path__
is a list and not a string. I've updated the answer.
– Mark Ransom
Jan 3 at 16:10
add a comment |
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',
autoActivateHeartbeat: false,
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%2f54009626%2fpython-access-module-from-package-that-is-not-imported-through-init-py%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to import them through __init__.py
, so change its contents to:
from .test_me import test_me, STATIC
Now the following will work:
import test_package
print(test_package.STATIC)
Well of course it will work. I know that. The question was how to access it if it was not imported. If its even possible? The package is not mine, so I cant just modify it. Or I would need to fork that project and customize it. Which I dont want to do it.
– Andrius
Jan 2 at 16:36
add a comment |
You need to import them through __init__.py
, so change its contents to:
from .test_me import test_me, STATIC
Now the following will work:
import test_package
print(test_package.STATIC)
Well of course it will work. I know that. The question was how to access it if it was not imported. If its even possible? The package is not mine, so I cant just modify it. Or I would need to fork that project and customize it. Which I dont want to do it.
– Andrius
Jan 2 at 16:36
add a comment |
You need to import them through __init__.py
, so change its contents to:
from .test_me import test_me, STATIC
Now the following will work:
import test_package
print(test_package.STATIC)
You need to import them through __init__.py
, so change its contents to:
from .test_me import test_me, STATIC
Now the following will work:
import test_package
print(test_package.STATIC)
answered Jan 2 at 16:31
feliksfeliks
1,015316
1,015316
Well of course it will work. I know that. The question was how to access it if it was not imported. If its even possible? The package is not mine, so I cant just modify it. Or I would need to fork that project and customize it. Which I dont want to do it.
– Andrius
Jan 2 at 16:36
add a comment |
Well of course it will work. I know that. The question was how to access it if it was not imported. If its even possible? The package is not mine, so I cant just modify it. Or I would need to fork that project and customize it. Which I dont want to do it.
– Andrius
Jan 2 at 16:36
Well of course it will work. I know that. The question was how to access it if it was not imported. If its even possible? The package is not mine, so I cant just modify it. Or I would need to fork that project and customize it. Which I dont want to do it.
– Andrius
Jan 2 at 16:36
Well of course it will work. I know that. The question was how to access it if it was not imported. If its even possible? The package is not mine, so I cant just modify it. Or I would need to fork that project and customize it. Which I dont want to do it.
– Andrius
Jan 2 at 16:36
add a comment |
If you add the package directory to your path, Python can import any file in that directory as if it were a module by itself.
import sys
sys.path.extend(test_package.__path__)
import test_me
print(test_me.STATIC)
Doesn't seem to work (or am I missing something?). If I do exactly as you described, then I getNameError: name 'test_package' is not defined
. If I first importtest_package
normally and then try to do your way, then when I try to doimport test_me
, I getImportError: No module named 'test_me'
– Andrius
Jan 3 at 7:36
@Andrius I tested it and it worked for me. Is your test setup exactly as you show in the question? Which version of Python are you using?
– Mark Ransom
Jan 3 at 14:41
Well i copy pasted the content, so it should be the same. Python 3
– Andrius
Jan 3 at 14:48
@Andrius I apologize, when I tested I used a hard-coded path. I didn't realize thattest_package.__path__
is a list and not a string. I've updated the answer.
– Mark Ransom
Jan 3 at 16:10
add a comment |
If you add the package directory to your path, Python can import any file in that directory as if it were a module by itself.
import sys
sys.path.extend(test_package.__path__)
import test_me
print(test_me.STATIC)
Doesn't seem to work (or am I missing something?). If I do exactly as you described, then I getNameError: name 'test_package' is not defined
. If I first importtest_package
normally and then try to do your way, then when I try to doimport test_me
, I getImportError: No module named 'test_me'
– Andrius
Jan 3 at 7:36
@Andrius I tested it and it worked for me. Is your test setup exactly as you show in the question? Which version of Python are you using?
– Mark Ransom
Jan 3 at 14:41
Well i copy pasted the content, so it should be the same. Python 3
– Andrius
Jan 3 at 14:48
@Andrius I apologize, when I tested I used a hard-coded path. I didn't realize thattest_package.__path__
is a list and not a string. I've updated the answer.
– Mark Ransom
Jan 3 at 16:10
add a comment |
If you add the package directory to your path, Python can import any file in that directory as if it were a module by itself.
import sys
sys.path.extend(test_package.__path__)
import test_me
print(test_me.STATIC)
If you add the package directory to your path, Python can import any file in that directory as if it were a module by itself.
import sys
sys.path.extend(test_package.__path__)
import test_me
print(test_me.STATIC)
edited Jan 3 at 16:10
answered Jan 2 at 21:56
Mark RansomMark Ransom
226k30286509
226k30286509
Doesn't seem to work (or am I missing something?). If I do exactly as you described, then I getNameError: name 'test_package' is not defined
. If I first importtest_package
normally and then try to do your way, then when I try to doimport test_me
, I getImportError: No module named 'test_me'
– Andrius
Jan 3 at 7:36
@Andrius I tested it and it worked for me. Is your test setup exactly as you show in the question? Which version of Python are you using?
– Mark Ransom
Jan 3 at 14:41
Well i copy pasted the content, so it should be the same. Python 3
– Andrius
Jan 3 at 14:48
@Andrius I apologize, when I tested I used a hard-coded path. I didn't realize thattest_package.__path__
is a list and not a string. I've updated the answer.
– Mark Ransom
Jan 3 at 16:10
add a comment |
Doesn't seem to work (or am I missing something?). If I do exactly as you described, then I getNameError: name 'test_package' is not defined
. If I first importtest_package
normally and then try to do your way, then when I try to doimport test_me
, I getImportError: No module named 'test_me'
– Andrius
Jan 3 at 7:36
@Andrius I tested it and it worked for me. Is your test setup exactly as you show in the question? Which version of Python are you using?
– Mark Ransom
Jan 3 at 14:41
Well i copy pasted the content, so it should be the same. Python 3
– Andrius
Jan 3 at 14:48
@Andrius I apologize, when I tested I used a hard-coded path. I didn't realize thattest_package.__path__
is a list and not a string. I've updated the answer.
– Mark Ransom
Jan 3 at 16:10
Doesn't seem to work (or am I missing something?). If I do exactly as you described, then I get
NameError: name 'test_package' is not defined
. If I first import test_package
normally and then try to do your way, then when I try to do import test_me
, I get ImportError: No module named 'test_me'
– Andrius
Jan 3 at 7:36
Doesn't seem to work (or am I missing something?). If I do exactly as you described, then I get
NameError: name 'test_package' is not defined
. If I first import test_package
normally and then try to do your way, then when I try to do import test_me
, I get ImportError: No module named 'test_me'
– Andrius
Jan 3 at 7:36
@Andrius I tested it and it worked for me. Is your test setup exactly as you show in the question? Which version of Python are you using?
– Mark Ransom
Jan 3 at 14:41
@Andrius I tested it and it worked for me. Is your test setup exactly as you show in the question? Which version of Python are you using?
– Mark Ransom
Jan 3 at 14:41
Well i copy pasted the content, so it should be the same. Python 3
– Andrius
Jan 3 at 14:48
Well i copy pasted the content, so it should be the same. Python 3
– Andrius
Jan 3 at 14:48
@Andrius I apologize, when I tested I used a hard-coded path. I didn't realize that
test_package.__path__
is a list and not a string. I've updated the answer.– Mark Ransom
Jan 3 at 16:10
@Andrius I apologize, when I tested I used a hard-coded path. I didn't realize that
test_package.__path__
is a list and not a string. I've updated the answer.– Mark Ransom
Jan 3 at 16:10
add a comment |
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.
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%2f54009626%2fpython-access-module-from-package-that-is-not-imported-through-init-py%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
What do you want to do exactly? Do you want to access the
test_package.STATIC
after youimport test_package
?– feliks
Jan 2 at 16:25
@feliks yes, in my real case I need to use function that lives inside that module. In example case I want STATIC variable. Idea is su access other attributes than were exposed. As in an example.
– Andrius
Jan 2 at 16:27