I am using a notebook (in Google Colab) in python 3 and really need to execute some python2 code with some data that is generated in my notebook !
So what I did was
- Generate the data in my cells AND save it to a file (
data.txt) - Write a python 2 script (
myScript.py) with amain()function that parses the file fromsys.argv[1]into data and than calls my python2 functions and do all the stuff then it ends withreturn results(which is a dictionary) - In my notebook I run
!python2 myScript.py ./data.txt
(They are of course all in the same directory)
The command runs with no errors but no output ! How do I catch the results that are returned in a variable that I could use later ?
Not important but could be helpful :
Is there a better way to actually achieve what I am willing to achieve ?
myvar = !python2 myScript.py ./data.txtmyScript.py should write to stdout using print or similar. ` def main(): print('Hello World') ` If you have a dictionary, you could either use eval() or print it csv like and parse back to a dictmyvar = !python2 myScript.py ./data.txt, I used that insight to dump my dictionary as JSON then loading it from my notebook. Thank you for your help