Reputation: 25
I ran the Python REPL tool and imported a Python Module. Can I can dump the contents of that Module into a file? Is this possible? Thanks in advance.
Upvotes: 0
Views: 1706
Reputation: 1755
You might get a start by getting a reference to your module object:
modobject = __import__("modulename")
Unfortunately those aren't pickleable. You might be able to iterate over dir(modobject)
and get some good info out catching errors along the way... or is a string representation of dir(modobject)
itself what you want?
Upvotes: 0
Reputation: 304443
Do you mean something like this?
http://www.datamech.com/devan/trypython/trypython.py
I don't think it is possible, as this is a very restricted environment.
The __file__
attribute is faked, so doesn't map to a real file
Upvotes: 0
Reputation: 882611
In what format do you want to write the file? If you want exactly the same format that got imported, that's not hard -- but basically it's done with a file-to-file copy. For example, if the module in question is called blah
, you can do:
>>> import shutil
>>> shutil.copy(blah.__file__, '/tmp/blahblah.pyc')
Upvotes: 1