Spyder, Python IDE startup code crashing GUI - python

I am using Spyder from the Anaconda scientific package set (3.x) and consistently work with very large arrays. I want to be able to see these arrays in my console window so I use these two commands:
set_printoptions(linewidth=1000)
to set the maximum characters displayed on a single line to 1000 and:
set_printoptions(threshold='nan')
to prevent truncation of large arrays. Putting these two lines of code into the startup option as such
set_printoptions(linewidth=1000),set_printoptions(threshold='nan')
causes Spyder to hang and crash upon a new session of ipython in the console. Is there a way to run these lines of code without having me type them all the time. Also, the console window only allows me to scroll up to a certain point then stops. This can be a problem when I want to view large arrays. Is there any way to increase the scroll buffer? (Note, I'm very new to Python having just switched over from MATLAB).

You may find Spyder's array editor better suited for large arrays than the qt console.

Related

Show matplotlib figures during debug in visual studio code (using ssh conection)

I am working with visual studio code (python) using ssh to access a remote server (where the code is located). I am able to write code, run and debug without any problem. However I am not able to generate matplotlib figures during debug in the same way as i can do it without the ssh connection.
I've tried to follow several suggestions from internet and other post here but none of them is fully working.
I've tried the option of " Jupiter> Debug current file in interactive window " but it does not work. I am not sure why, but the debug in the interactive window is not responding and i can not work during debug.
I've tried python debug and the only matplotlib backend that works is (matplotlib.use("WebAgg"). However for this backend it only works with one figure and the debug is block after the plot.
I have also tried to use X.Org (in my case xquartz over mac) but the figures are really slow and it is not useful.
Any idea on how to plot figures during debug in the same way as in local development?
Thanks!
I asked this question in GitHub and got the following answers:
One way to do this is to do the 'Debug your current file in the interactive window'.
There's a bunch of caveats though.
You need to have ipykernel installed into the python environment
you're using. If you run that command it should have asked you to do
so.
It's likely easier to use if you put cell markers around pieces of
code # %%. This would also allow you to prerun bits of your script
before you need to debug the part that's causing problems. This is
what it looks like when you do that:
When debugging in the interactive window, your code in your script
is running as an IPython cell. If you split it up, it might be
multiple cells or it could be one large cell (for the whole file).
As you step, the cell execution is moved forward one line at a time.
However the execution is paused in between steps. This means
anything you type in the interactive window won't be executed.
Instead you have to run extra code in the 'debugger console'. Shown
below:
This debugger console is where you can run normal matplotlib
commands.

Different responses from Spyder's Ipython and standard anaconda console

I have a very large script which I developed using Spyder. I have friends who are trying to run it using the standard anaconda console / command prompt. What is interesting is the the script works between computers if run on Spyder, but completely freaks out if run in the anaconda console, command prompt, or even Ipython outside of Spyder. When I say freak out, I mean it randomly starts again at the beginning of the script, reloads packages, spams the user interface text a few times then randomly crashs. There is never an error code. There is no while loop or for loop or anything which would cause it to restart randomly like this. I have had a number of python programmers look at the code directly and cannot understand either why it would even attempt to restart.
I have isolated the issue to the following line:
results = pd.DataFrame(MOR.predict(dat), columns = scores)
In this case, MOR is an imported pickled sklearn multioutput xgboost model. dat is a dataframe with a couple rows and the same required columns as the xgboost model. scores is simply a list of names which is equal to the number of output variables from the sklearn model.
Any ideas as to why this script would run perfectly in spyder but not the console? Note, its clear that this likely will not have a solved answer, but any speculations as to why spyder would behave different from these other consoles?

How to run code selection in Spyder without changing the active window to IPython console?

This is mostly a question regarding work-flow in Spyder, and not a pure programming question. If anyone thinks it has no place here, then please let me know and I'll remove it.
Here we go:
I'm running Spyder on a Windows 64-bit operating system. The interaction between the Spyder IDE and IPython console depends on how you choose to run your code, and I would like to specify how this interaction works.
Among several options to run parts of the code, the difference between the following two is what puzzles me:
Option 1 - Select parts of the code and hit F9
This runs the selected code. No surprise there.
Hit F9 again, and the same selection is executed once more.
Option 2 - Run the same selection using Cells (#%%) and Ctrl+Enter
The code is run just like before. But hit Ctrl-Enter again, and nothing happens. Probably because the IPython console now is the active window.
I would like to find some way of changing this so I can use Ctrl+Enter and run the cell contents multiple times without having to make the Spyder IDE the active window by manually clicking on it. I don't know if this is possible at all. Googling and messing around in the settings under Run / configure and Tools / Preferences has so far been useless.
Thank you for any suggestions!
P.S
In case you're wondering why this is such a big deal to me, I'm rendering multiple charts and exporting them to Power Point with minor changes each time the code is run. This way, new parts of the charts (lines, textboxes, etc) are revealed when I'm working my way through a finished Power Point presentation. And for my basic data science needs, this has turned out to be a pretty much optimal work-flow. Except, of course, that I have to manually select the Spyder IDE each time I run different selections of the code.
In the latest spyder version, the keyboard shortcut to run the highlighted lines of code is F9 but it doesn't seem to work.
Go change it from TOOLS>>PREFERENCES>>KEYBOARD SHORTCUTS then double click on any shortcut you want to change and change with your shortcut. But be very careful if you used the same shortcut that is used for any other work then for that it will remove.
I changed the shortcut for 'run selection' from F9 to ctrl + Enter
in Option 2, you should use Shift+Enter instead

pick_event command in Canopy

I recently upgraded computers and I switch from using Python to Canopy Express since all the packages I use in my coding comes included in Canopy. However, when I went to run a program I wrote that uses matplotlib to create interactive plots that I could click on data points using 'pick_event' in Canopy, I'm getting a few issues with the program that I didn't have in Python. When I run the program, Canopy displays the graph and then immediately moves to the next line of code. This creates a problem for me since I want the graph to be interactive and it doesn't give me a chance to choose my data points I want. I have found ways to leave the graph up (adding in a raw input after displaying the plot or not closing the plot in the program) but Canopy crashes when I try to move or click on the plot. Is there any way I can fix this issue so I can use 'pick_event' command?
Thanks for the help
It seems likely that your code is not written to optionally run in IPython's %pylab mode (or, equivalently in this context, %matplotlib mode), which starts a GUI event loop for you and in many cases makes interactions between prompt and GUI easier. You might consider adapting your code to be able to do this, but meanwhile you can test this hypothesis by disabling pylab mode in the Canopy Preference menu (Python tab).
This article may be relevant:
Using Wx/WxPython in Canopy's IPython panel

Compiler vs. IDLE Environment

I'm using mac osx mavericks (fully updated) and while looking for a pyscripter alternative, I decided to download CodeRunner from the app store.
I've noticed that certain programs I create in one application, don't always run the same in the other environment.
For example, there are times when I create a program using CodeRunner, but when I open the exact same program in the IDLE environment, it spits back an error. Usually complaining about the syntax or logic of the program. And the other way around can sometimes occur as well.
Is this normal behavior? Should I be saving the program in a different format? I assumed that since I'm coding in Python, the code would behave the same regardless of the environment I use.
The different programs probably have different definitions of tabs and whitespace.In IDLE, you can use Format->Tabify/Untabify menu options to change between tabs and spaces. When you click on these menu options, you prompted for the columns per tab, which will default to 4(At least on my machine). Hope this helps!

Categories