0

My problem is quite easy but I can not find a good answer because the search engines are ambiguous on the term "module". What I want do to is roughly this :

Module : a.py

x = 2

Module : b.py

import a

Now, I want to be able to access x from b without using qualified name (i.e. without typing a.x, just with x). In my situation I cannot use :

from a import x

because I don't know which elements a will contains. I can not use

from a import *

neither. Is there any simple way to merge or join the modules (I mean the Object Modules) ?

b4hand
  • 9,550
  • 4
  • 44
  • 49
ibi0tux
  • 2,481
  • 4
  • 28
  • 49

1 Answers1

1

This is not a good idea, but you can use:

globals().update(vars(a))

to add all names defined in the a module to your local namespace. This is almost the same as from a import *. To emulate from a import * exactly, without using from a import * itself, you'd have to use:

globals().update(p for p in vars(a).items() if p[0] in getattr(a, '__all__', dir(a)))

You normally just would use x = a.x or from a import x.

If you are using zipimport you don't have to do any of this. Just add the path to the archive to your sys.path:

import sys
sys.path.insert(0, '/path/to/archive.zip')
from test import x
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • In fact i don't want to import all names from **a**, just some of them (depending on their type). I'll try your solution, but i thought there was a "safer" alternative to updating globals() – ibi0tux Feb 07 '13 at 14:26
  • @ibi0tux: It is not at all clear why using `x = a.x` and such is not something you can do. Dynamically adding names to your namespace requires manipulating `globals()` or using `exec`, and I cannot recommend the latter when `globals()` will do. – Martijn Pieters Feb 07 '13 at 14:27
  • Actually the module is imported with zipimport and i have something like : `a = zipimport . zipimporter ( "archive.zip" )` `mod_a = a . load_module ( "test" ) # considering archive.zip contains a module test` `print mod_a . value # considering value is defined in module test` Is there any way to make the import directly in global ? – ibi0tux Feb 07 '13 at 14:35
  • 1
    @ibi0tux: See, why didn't you post than in your question to start with? You can just add the zip module to your `sys.path` and import normally. – Martijn Pieters Feb 07 '13 at 14:36
  • Does this mean i don't need to use zipimport ? – ibi0tux Feb 07 '13 at 14:39
  • You don't *have* to use `zipimport`. – Martijn Pieters Feb 07 '13 at 14:40
  • hm .. this does resolve the problem. I tried something but it still need to do an `from X import *` to get names in global – ibi0tux Feb 07 '13 at 14:55
  • @ibi0tux: But *why* can't you use `from X import namea, nameb, namec`? What is preventing you from doing that? – Martijn Pieters Feb 07 '13 at 14:57
  • In my situation i'm importing a zip containing python, but i don't know what contains the zip before the import, and i want to import classes only – ibi0tux Feb 07 '13 at 15:03
  • But why do you have to add them to the global namespace? Can't you add them to a `dict()` or a `list()` after testing for classes? – Martijn Pieters Feb 07 '13 at 15:04
  • I prefer adding them to the global namespace to use the names without any prefix. – ibi0tux Feb 07 '13 at 15:29
  • Is it possible to use `__import__` instead of `import` or is it a bad practice ? – ibi0tux Feb 07 '13 at 15:37
  • `__import__` is a hook method; it's like calling `__getattribute__` directly on an object instead of using `getattr()`, or working your way through `__dict__` and `__class__` and it's `__bases__` instead of using `dir()`. Use `importlib` or `import` instead. – Martijn Pieters Feb 07 '13 at 15:42