Spyder console value different than in the Editor - python

For a work/study project I have been transitioning from JupiterLab and NOtebook to Spyder, to take advantage of Spyder's included debugger (missing in the other 2 IDE).
While running my very first few lines of code as a tryout, i noticed the code in the console displays some parts that are different from what I wrote in the editor.
Specifically, I try to slice a test List as L_f = L[:k]
why the console executes/displays L_f = L[:k**+1**] ? where is that "+1" coming from?
Spyder console vs editor

no worries, solved with ChatGPT. Spyder default folded some code, compiler was looking different than Editor because in the latter 2 rows were actually folded.

Related

PyCharm-like console in Visual Code Studio

I wanted to give VSC a try for developing some Python programs, where I only used PyCharm before. One of the most helpful features for me in Pycharm was the PyDev Console, where I can quickly try small snippets of code (think 3-10 lines), and adjust it to work the way I want it to.
I see VSC has a console, but it's much more like the regular IDLE console, where it's kind of hard to write these snippets of code (fixing something 2 lines prior for example is pretty much impossible).
I've been searching for an extension that'll give me a PyCharm-like console experience in VSC, but have been unable to find one. Is is out there? Or is there another way to get to the same result (like setting up a custom console based on the same PyDev console)?
Have you tried Jupyter Notebook and Interactive? There are provided by the Jupyter Extension which is bound with Python Extension.
Open the Command Palette (Ctrl+Shift+P), with the command of:
Jupyter: Create New Blank Notebook
Jupyter: Create Interactive Window
You can refer to the official docs for more details.

Return function in VSC vs Spyder

I find this mildly irritating as I am unable to see the reason.
The return statement (within a function) in VS Code (+ bash terminal) doesn't return any value, unless I explicit say print(function()), like given in the code below.
This doesn't happen in Spyder, it promptly returns the value in the console.
Why is this so? What am I missing?
The main reason is that in Spyder you use IPython console. It is an interactive shell which provides extra features. Since in your bash terminal the Python console is used instead of IPython, it does not return the value of the function when it is prompted.
In this question, the differences between IPython and Python consoles are discussed.
It is possible to use IPython in VS Code as well https://code.visualstudio.com/docs/python/jupyter-support-py
You may have to enable Python Data Science features in settings though (considered still experimental as of September 2020):
open the settings (press Ctrl+,) then search for datascience options
Personally, I find "Send Selection To Interactive Window" option very useful.
"python.dataScience.sendSelectionToInteractiveWindow": true,
Determines if selected code in a python file go to the terminal or to the Python interactive window when hitting Shift+Enter

VSCode Jupyter: is there a way to make python interactive window work as default console?

I've recently switched to VSCode, and am wondering if there's a way to make the Python Interactive Window from the Jupyter support in VSCode work like the console in Spyder where I just have to select code and press ctrl+enter to send it, without having to create cells everytime.
For now I'm resigned to work with the Terminal until my code is clean and then create a cell when I have reusable code, and would like to just work directly with the PIW.
You can always change the default console setting by:
Opening the Command Palette (⇧⌘P)
Typing "Preferences:Open Settings (JSON)
Edit this line:
"python.dataScience.sendSelectionToInteractiveWindow": false
You should be able to do this with the latest python extension. The select the code you want to execute and press shift-enter. Is that not working?
For me (now) interactive mode runs after setting "jupyter.sendSelectionToInteractiveWindow": true
#FranciscoRZ. You should have seen a popup option for this, but if it didn't come up for you it can just be manually set in VSCode options. Just change this guy here:
Python->Data Science: Send Selection To Interactive Window
That should get you what you are looking for.
OP's Note: Accepting this answer because it will be the right anwser starting with the February release of VS Code Python
If you have the notebook saved as a python percentage script (which is more git friendly)
each "cell" will be delimited by # %% and the default run command is "Shift+Ctrl".
Later once you are working in the interactive window, If you want a particular cell you wrote on the fly to be in you script, there is one button which says "Paste code into file" right next to the recently executed cell in the interactive window.
And in case you are using the notebook for the sake of being able to later export it to html or pdf, once executed in the interactive window, there is an export button as well.

Console speed: PyCharm vs Spyder

I have installed both PyCharm and Spyder (from Anaconda2).
However, when I run the exact same code (printing a very large array) from the python console, the console opened from Spyder printed out the array in less than five seconds, whereas the console opened from PyCharm took one minute to process and then printed the array.
I am wondering what is the reason for the difference in "processing time"? I like the auto-complete feature of PyCharm, but from my experience, it is slower than Spyder. Is there a solution?
(Spyder maintainer here) To avoid this problem in Spyder we only allow 500 lines to be shown in the console at a time (Note: This limit is configurable by the user)
So I'd say Pycharm's console doesn't have this functionality (although I can't tell that for sure).
Spyder has an integrated Jupyter Notebook and can show plots inline - comes with autocompletion that is just as good (if not much better) than PyCharm's terminal...
It comes with a live debugger that's much easier on the eye (side-by-side) and can execute code real damn fast!!!
Spyder gives you the option to autocomplete words in the console itself - Pycharm just uses the system command prompt/Terminal.

notepad++ run selected code in python console seamlessly

I often use R to analyze data, and really enjoy Notepad++ along with NppToR. Especially, NppToR enables to run a part of code without much hassle. I just highlight a snippet of R code to run and press F8. Then the code magically runs in R console.
Now, I am required to use python to analyze data. I know that ipython is great to work interactively, but it is always very annoying to copy a snippet of python code and manually paste that into ipython console. Also, indentation is often mixed and thus the entire lines are failed to run. Note that I want to run 'selected' lines of codes, not the entire file.
So I am looking for a program/plugin/macro similar to NppToR, but working with python/ipython console. I have searched the web, but couldn't find such one. Some plugins are nice, but not exactly what I want. For example, Python Script enables extending Notepad++ itself, but not outside. Various other 'Run' extensions enables the entire file to be run in python.
You can customize the editor IPython uses for the edit command, and configure IPython to use Notepad++. IPython 0.11 creates a .ipython/profile_default folder in your user folder (in my case C:/Users/zk/.ipython/profile_default). To configure IPython to use Notepad++ create .ipython/profile_default/ipython_config.py with the following:
c = get_config()
# use DOS style path, C:/PROGRA~2 on my 64-bit system points to C:/Program Files (x86)
c.TerminalInteractiveShell.editor = 'C:\PROGRA~2\NOTEPA~1\NOTEPA~1.exe'
# set editor for Qt Console, if you wish to use that
c.IPythonWidget.editor = 'C:\PROGRA~2\NOTEPA~1\NOTEPA~1.exe'
You can then start up IPython and use the edit command to run Notepad++ from IPython, saving and closing Notepad++ will execute the file in IPython.
If you don't mind installing PyQt4 and pyzmq (and I believe pygments, at least for IPython 0.12-dev), IPython's Qt console works really well (frankly the nicest interactive environment you can get for Python). You can paste directly into IPython and it'll adjust indentation for you (shifting over padded code).

Categories

Resources