Python Forum
python import module or namespace from namepsace variable ?
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 ?
Reply
#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
Reply
#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 ?
Reply
#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
Reply
#5
@sparkz_alot

I tried too many times with importlib module
Reply
#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
Reply
#7
the python import system soooooooooooooooo bad,
Reply
#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.
Reply
#9
@volcano63

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  is import cointegration_analysis a recognized module mitcht33 1 385 Nov-06-2023, 09:29 PM
Last Post: deanhystad
  problem in import module from other folder akbarza 5 1,259 Sep-01-2023, 07:48 AM
Last Post: Gribouillis
  can not import anaconda pandas module. PySpark pandas module is imported!! aupres 0 680 Aug-06-2023, 01:09 AM
Last Post: aupres
Video doing data treatment on a file import-parsing a variable EmBeck87 15 2,667 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  import module error tantony 5 3,359 Dec-15-2022, 01:55 PM
Last Post: Lauraburmrs
  Import a module one step back of the path prathampatel9 1 1,036 Sep-21-2022, 01:34 PM
Last Post: snippsat
Question How can I import a variable from another script without executing it ThomasFab 12 7,558 May-06-2022, 03:21 PM
Last Post: bowlofred
  Import a module for use in type hint? Milosz 0 1,455 Nov-08-2021, 06:49 PM
Last Post: Milosz
  Can't install nor import delorean module Tek 3 2,741 Oct-27-2021, 03:32 AM
Last Post: Tek
  'namespace' shorthand for function arguments? shadowphile 5 2,541 Aug-11-2021, 09:02 PM
Last Post: shadowphile

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020