8

We use all the time python's import mechanism to import modules and variables and other stuff..but, is there anything that works as export? like:

we import stuff from a module:

from abc import *

so can we export like?:

to xyz export *

or export a,b,c to program.py

I know this question isn't a typical type of question to be asked here..but just in curiosity..I checked over the python console and there is nothing that exists as 'export'..maybe it exists with some different name..?

khan
  • 7,005
  • 15
  • 48
  • 70
  • 6
    How exactly would that work? – NPE Jan 04 '13 at 19:25
  • 2
    Why would you want that? –  Jan 04 '13 at 19:26
  • like instead of exporting any variable or a method from a python file or module..why cant we export from that file to our destination so that even if our code goes public..no one knows where the variables or methods are coming from? this is just one aspect..in my opinion..if there is an import then there should exist an export. :-) – khan Jan 04 '13 at 19:27
  • You can build stand alone applications from python scripts. Is this what you are asking? – Daniel Jan 04 '13 at 19:28
  • no..my question is more related to the logic and probably the pythonic semantics. – khan Jan 04 '13 at 19:30
  • 1
    That makes no sense at all, sorry. (And if you worry about your code "going public", stop and think first: If you make it unreadable by obfuscating dependencies, it'll be unreadable first and foremost to *you* and anyone else working on it, possibly to the point that you won't even get anything done. Not to mention that it would be trivial to just follow the procedure Python follows to execute those exports.) –  Jan 04 '13 at 19:31
  • 2
    Let's say you have scriptA.py and scriptB.py, and you want to "export" A to B. You're suggesting that when you run scriptB.py, it should somehow magically realize that scriptA.py has an "export" command in it. Without reading every file, there's no way for scriptB.py to know what any other script says. Running scriptA.py, which would export to scriptB.py would be fine and dandy, but as you're not running scriptB.py at that point, why bother "exporting" anything to it? – ernie Jan 04 '13 at 19:31
  • 4
    i can't even imagine the kind of nightmare that would be developing in python, if any script at any time could haphazardly inject itself into other modules. – Mike Corcoran Jan 04 '13 at 19:32
  • 2
    Perhaps you're looking for [monkey patching](http://stackoverflow.com/questions/2375403/how-does-one-monkey-patch-a-function-in-python)? – ceyko Jan 04 '13 at 19:35
  • There is a second option that you can call the .pyc file that is created when you call a module. The issue with this is that it is not portable as part of the .pyc file contains linkers. – Daniel Jan 04 '13 at 19:35
  • 5
    Sounds similar to the infamous [COME FROM](https://en.wikipedia.org/wiki/COME_FROM). – BrenBarn Jan 04 '13 at 19:38
  • No, everybody, imagine: I have two scripts named a.py and b.py..I export some variables to b.py from a.py and then import back these variables as processed from b.py to a.py...this is kind of like holding my left ear with the right hand since i can import methods from b.py which are supposed to process a.py's variables..another scenario is that I have a bunch of variables in a.py and a bunch of processing methods in b.py so i simply execute both scripts in serial and a.py exports its methods in b.py which sends back its results to a.py..i know it all sounds weird..but just imagining. – khan Jan 04 '13 at 19:42
  • so a.py executes b.py passing in some params which b.py returns results to a.py? Are we getting closer? – rlemon Jan 04 '13 at 19:45
  • or in this case..there may exist a gateway mechanism in b.py that would simply say yes or no to incoming variables..i know none of these exist. – khan Jan 04 '13 at 19:46
  • `export` is to `import` as http://en.wikipedia.org/wiki/COMEFROM is to `goto` – Joe Jan 04 '13 at 19:47
  • @BrenBarn I can't believe this actually exists in python. khan did you look at [this python module](http://entrian.com/goto/)? Of course, please heed the warning to *never* actually use it. – ceyko Jan 04 '13 at 19:48
  • 5
    if you have a.py and b.py, and a.py needs crap from b.py - a.py should import what it needs from b.py. not expect that someone else is going to load b.py before a.py so b.py can export its values to a.py in time for a.py to be able to do what it needs to do. see the problem here yet? – Mike Corcoran Jan 04 '13 at 19:53
  • Oh yeah, definitely, i see the loophole. I wasn't aware of the counter-logics you guys presented. :-) Thanks. – khan Jan 04 '13 at 19:56

1 Answers1

9

First, import the module you want to export stuff into, so you have a reference to it. Then assign the things you want to export as attributes of the module:

# to xyz export a, b, c
import xyz
xyz.a = a
xyz.b = b
xyz.c = c

To do a wildcard export, you can use a loop:

# to xyz export *
exports = [(k, v) for (k, v) in globals().iteritems() if not k.startswith("_")]
import xyz
for k, v in exports: setattr(xyz, k, v)

(Note that we gather the list of objects to be exported before importing the module, so that we can avoid exporting a reference to the module we've just imported into itself.)

This is basically a form of monkey-patching. It has its time and place. Of course, for it to work, the module that does the "exporting" must itself be executed; simply importing the module that will be patched won't magically realize that some other code somewhere is going to patch it.

kindall
  • 178,883
  • 35
  • 278
  • 309
  • How to 'export' to any module in general that calls "import my_exporter_module"? [without "*"] I've done that by putting entries in builtins(), but I would like to put something with the same name as my_exporter_module. Kind of a module that when imported becomes an object of some class. – dawid Dec 16 '20 at 09:28