I have written a module which re-defines the print function. Is it possible to preimport it in order to redefine print() (with from [module] import print) to demonstrate or test it with a program, without having to modify this program ?
1 Answer
Eureka ! ;-)
One can import a module (here, redefine the print function) and then chain a new program (./PROGRAM) in the current environment by using the command :
from MODULE import print exec(open('./PROGRAM').read()) So (in linux) one can do it from command line (or script it) with :
python3 -c "from MODULE import print; exec(open('./PROGRAM').read())" This question also contains useful information on this topic.
. script