Python Forum
python import module or namespace from namepsace variable ? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: python import module or namespace from namepsace variable ? (/thread-3432.html)

Pages: 1 2


python import module or namespace from namepsace variable ? - harun2525 - May-23-2017

this is very important to me, i want import a module or package (namespace) from another namespace variable.

example

import mydir # imports namepace

module_foo = get_module_from_name_space("foo", namespace = mydir) # imports foo module from mydir. that means from mydir import foo expression but I must import foo in that way, with this function.

print(module_foo == mydir.foo) # prints True
thanks.

is there a function like get_module_from_name_space ?


RE: python import module or namespace from namepsace variable ? - Larz60+ - May-23-2017

you should be able to do this with importlib
You'll have to read the docs, I have used it, but it's been a while
If you look at my github account: https://github.com/Larz60p/PyHarry
module HarryHelp.py i use it there
you may also be able to use pkgutil


RE: python import module or namespace from namepsace variable ? - harun2525 - May-23-2017

@Larz60+,

sorry, i couldn't find the function I am looking for in your HarryHelp.py file

can you post this function to here ?


RE: python import module or namespace from namepsace variable ? - sparkz_alot - May-23-2017

Try here: importlib, there are even some examples.


RE: python import module or namespace from namepsace variable ? - harun2525 - May-23-2017

@sparkz_alot

I tried too many times with importlib module


RE: python import module or namespace from namepsace variable ? - buran - May-23-2017

given https://python-forum.io/Thread-python-How-to-import-All-modules-from-namespace I think it's a matter of proper organising the package (i.e. __init__.py, etc.) and understanding the import mechanism rather than using some complicated import scheme.
i even think the two threads should be merged


RE: python import module or namespace from namepsace variable ? - harun2525 - May-23-2017

the python import system soooooooooooooooo bad,


RE: python import module or namespace from namepsace variable ? - volcano63 - May-23-2017

Maybe this will help?

importlib keep on changing - and remain being scantily (understatement of the century Wall ) documented


RE: python import module or namespace from namepsace variable ? - harun2525 - May-23-2017

@volcano63

can you write a function like function (get_module_from_name_psace) in my first reply


RE: python import module or namespace from namepsace variable ? - buran - May-23-2017

(May-23-2017, 01:28 PM)harun2525 Wrote: the python import system soooooooooooooooo bad,

is it really bad or simply you don't understand it?

given the folder structure in the other thread a __init__.py and __all__ property in it would allow you to do star import. Not that is a good practice :-)