I learnt data manipulation and analysis through Stata and I used the log command to record all the commands written and the output generated. Doing so I could reproduce my findings, check previous results and share it with others in pdf or txt. What to use with Python? Is there a difference if I use a Python Jupyter Notebook or Spyder?
Asked
Active
Viewed 4,376 times
1

Carlos Cordoba
- 33,273
- 10
- 95
- 124

Filippo Sebastio
- 1,112
- 1
- 12
- 23
-
How are you executing python code currently? Do you have python installed locally? Are you running the python REPL interpreter? – TZubiri Nov 29 '18 at 06:05
-
Yes, it is installed locally with anaconda, I use Spyder or Jupyter, I m not sure about the REPL, is that for replication? – Filippo Sebastio Nov 29 '18 at 06:07
-
https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop – TZubiri Nov 29 '18 at 06:08
-
yes, then, I write a script in spyder or Jupyter and then I run it. How do I record the output of the script? E.g. Spyder has a different window for the output - so If I had to share my script with someone he can only see the script, not the output (a table or a regression analysis) – Filippo Sebastio Nov 29 '18 at 06:14
-
If you want him to reproduce the findings, then he will have to run the script to generate the table. If you want him to see the table quickly, you can take a screenshot of the result yourself and send it as an image. – TZubiri Nov 29 '18 at 06:19
2 Answers
0
Stop using the python interpreter and start storing your commands in a text file before running them. This way you will be able to share, reuse, and revise your python code.
By executing the following command:
python code.txt
Python will read all of the lines in your file sequentially.
This is the main way python programs are executed, as a convention, python code files end with .py , so if you see a file with a .py extension, you can try executing it yourself using this method!

TZubiri
- 886
- 11
- 30
-
Thanks, but this method won't help with storing outpust, say a graph created with matplotlib, right? Or the output of a scikit-learn regression command. I just don't think it is efficient to take a screenshoot to all the outputs - it would be nice to have scripts and outputs automatically generated in the same document (txt or pdfor whatever..) – Filippo Sebastio Nov 29 '18 at 06:25
-
So this question answers how you can reproduce the results, maybe you could ask another question asking how to share the results. You should include details about the output you are generating. Depending on the type of output you generate, there will be a way to store the raw data in some serialized form. For example, if you have a plot, you can share a text file with all of the coordinates, or each coordinate for a given frequency of a formula. This heavily depends on your code, though, so please include these details in your next question. – TZubiri Nov 29 '18 at 06:38
-
Sorry for the confusion, I understand now there is no a single solution. Thanks for the help. – Filippo Sebastio Nov 29 '18 at 06:47
-
There is a single solution, but it depends on details you haven't posted yet. You haven't posted the details because you didn't deem them necessary for answering your original question "How to REPRODUCE your findings with Python" You now need to post these details because your question has changed. Your question has changed because your original question was solved. I encourage you to create another question if this is the case. This way we can ensure that we leave generic answers for generic questions for people in a similar position without making a really big super specific question. – TZubiri Nov 29 '18 at 07:07
0
The way to do what you want is by using the %logstart
command, as described here:

Carlos Cordoba
- 33,273
- 10
- 95
- 124
-
this is very useful, thanks! a friend of mine also recommended me to use the logging function. I will look into it and provide more details after testing it. That's the advice I got: http://angelaambroz.com/blog/posts/2018/Mar/13/writing_better_python/ – Filippo Sebastio Nov 30 '18 at 01:48