Python - access module from package that is not imported through __init__.py?












2















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.










share|improve this question























  • 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


















2















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.










share|improve this question























  • 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
















2












2








2








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.










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 16:14









AndriusAndrius

5,8802490157




5,8802490157













  • 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





















  • 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



















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














2 Answers
2






active

oldest

votes


















0














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)





share|improve this answer
























  • 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



















0














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)





share|improve this answer


























  • 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











  • 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











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
});


}
});














draft saved

draft discarded


















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









0














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)





share|improve this answer
























  • 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
















0














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)





share|improve this answer
























  • 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














0












0








0







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)





share|improve this answer













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)






share|improve this answer












share|improve this answer



share|improve this answer










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



















  • 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













0














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)





share|improve this answer


























  • 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











  • 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
















0














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)





share|improve this answer


























  • 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











  • 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














0












0








0







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)





share|improve this answer















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)






share|improve this answer














share|improve this answer



share|improve this answer








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 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











  • 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



















  • 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











  • 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

















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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

ts Property 'filter' does not exist on type '{}'

Notepad++ export/extract a list of installed plugins