Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 python import module or namespace from namepsace variable ?
#1
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 ?
Quote
#2
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
Quote
#3
@larz60+,

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

can you post this function to here ?
Quote
#4
Try here: importlib, there are even some examples.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Quote
#5
@sparkz_alot

I tried too many times with importlib module
Quote
#6
given https://python-forum.io/Thread-python-Ho...-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
Quote
#7
the python import system soooooooooooooooo bad,
Quote
#8
Maybe this will help?

importlib keep on changing - and remain being scantily (understatement of the century Wall ) documented
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Quote
#9
@volcano63

can you write a function like function (get_module_from_name_psace) in my first reply
Quote
#10
(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 :-)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Importing module from a package results in import error goghvv 2 96 Mar-27-2020, 07:13 PM
Last Post: goghvv
  Import Module and Method ( dynamic ) ? JohnnyCoffee 0 70 Mar-23-2020, 04:23 PM
Last Post: JohnnyCoffee
  how to import a module which is installed to personal folder using pip install --pre? geekgeek 2 133 Mar-09-2020, 02:38 PM
Last Post: geekgeek
  module run on import? mcmxl22 3 149 Mar-02-2020, 04:37 PM
Last Post: mcmxl22
  Cannot import list from another module? t4keheart 1 120 Feb-17-2020, 10:25 PM
Last Post: t4keheart
  Beginner question: lxml's findall in an xml namespace aecklers 0 121 Jan-22-2020, 10:53 AM
Last Post: aecklers
  How to import entire module ? tonycstech 4 449 Dec-01-2019, 01:53 AM
Last Post: quyenca
  using module variable jacklee26 4 217 Nov-13-2019, 01:36 AM
Last Post: jacklee26
  How I can use multiprocessing with upickled module variable? AlekseyPython 3 407 Oct-31-2019, 06:09 AM
Last Post: AlekseyPython
  When I import a Module it wont run PyNovice 17 802 Oct-26-2019, 11:14 AM
Last Post: PyNovice

Forum Jump:


Users browsing this thread: 1 Guest(s)