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 ?
Asked
Active
Viewed 431 times
0

Camion
- 1,264
- 9
- 22
-
No. Each process is separate. – user2864740 Jul 14 '20 at 19:49
-
Why should it have to be another process ? I wasn't thinking about making it another process. More about chaining like when to chain shell scripts with `. script` – Camion Jul 14 '20 at 19:51
1 Answers
1
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.

Camion
- 1,264
- 9
- 22