Logging input and output in Spyder's console - python

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?

The way to do what you want is by using the %logstart command, as described here:
Log IPython output?

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!

Related

Sourcing a python script in Rstudio

So I can run my python script eq_odds.py (which takes a file as input) in my terminal as follows: eq_odds.py data/health.csv
I need to use this output in my R code, therefore I want to source this python code directly in Rstudio.
The sourcing went well, I just don't know how to get the python code running with the input file in RStudio.
Has anyone got a clue?
The python script and data can be found at https://github.com/gpleiss/equalized_odds_and_calibration

Is there a way to open Anaconda prompt, and insert a few commands that would run a python script, only by using VBA?

I am working on a project using VBA, and have found that the best way to perform a certain function within it is with a python script (Pulling data from a web service via a user-inputted serial number, and then populating info associated with that serial in an excel sheet)(Probably the WHOLE thing should've been done in python, but I am not familiar with it, and am with VBA somewhat, so this is the path that was chosen). I have had some success with this by using pyinstaller to make the .py into a .exe, however this caused other issues, such as the script not functioning properly, and taking a while to run.
It is simple enough to run the actual python script via the anaconda3 prompt window. However, this will be used by others, so I would like it to be automatic such that, they only need to click on a VBA macro button, which then automatically runs the python script.
Any thoughts on how this can be achieved?
Thank you!

save Python variables after running it from terminal

I am using python and Visual Studio code to develop my code. I can run my scripts successfully (Using Run Python File in Terminal). However, this is very inconvenient since after each run I go back to the terminal window and do not have access to python, therefore, I will lose the variables in the previous run so I have three options: 1- Print all the variables I want to screen (This is clearly not scalable). 2- Run the file again. 3- Run the Python file in an interactive window(Ipython).
Option 3 is exactly like running the file in a Jupyter Notebook which is very convenient since you can create a cell below the current cell and keep using the variables after one run.
I would like to know what is the best method to run the python script from the terminal (Pycharm, VS code,...) and can work with variables after (not like every time you forgot to print something you run the file again?
If you set a breakpoint then debug your code (f5), on the bottom of VSCode there is a Debug Console, you can type in debug command there (print(), type(), etc). That seems like it might be what you are looking for.
Try shift + enter on each line this will open the terminal with python
Or try to run .ipynb files instead of .py files in vscode

How to debug python called by local process?

Hi I'm trying to debug python script called from another process, thus I would need to attach it to the process.
I actually read this doc and I'm struggling at the "Attach to a local script" part.
It is said that I should:
In the terminal, start Python with the script
Switch to the Debug view, select Python: Attach
However, it does not give out the detail in launch.json.
Furthermore, it is not clear that which file I should start or stop. To me it seems like I should start the script in command line and restart it in debugger.
So what I would like to do is to debug a python script called from excel, the VBA codes are as follow
Sub Button1_Click()
RunPython("import Test; Test.testprint")
End Sub
The call is through xlwings add-in.
And in Test.py:
def testprint()
print("abcd\n")
This is a test script, the actual script would try to do some file operations. But what I really like to do is enabling debug from a call from Excel-VBA.
Thus, would I please have an example of launch.json, script and detailed operation steps. Thanks a lot.
My platform:
OS: Windows 10,
Editor: VS-Code,
Python: 3.6.5,
Anaconda: 4.5.4,
ptvsd: 5.0.0a4
I've been struggling with it for whole day. Thanks to anyone who could help.

iteratively and interactively writing and testing python code

I'd like to iteratively write and test my python code in the Python console. I can't find a way to easily load what i've got written in the editor (somefile.py) into the Python console in Pycharm. Is there a trick to doing so?
Pycharm will let me run the entire script but that's not useful because i want to build up my state in the shell by experimenting with the functions and data i've got in the environment (kind of like how a lisp programmer would use a REPL).
Pycharm have this alt-shift-E option.
This way you can write in the editor, select the lines you want to run and run them by alt-shift-E.
I use it all the time, although I find a lot of people not fameliar with this option.

Categories