Ipython - %edit <functionname > throws error when notepad is opened - python

When I try to edit a function ( printval in my example ) defined in Enthought canopy Ipython using the command,
%edit printval .
I get the following error message on a pop up with a notepad opened in the background.
The filename,directory name ,or volume label syntax is incorrect.
The notepad is blank after I click the pop up message. This error is happening in windows Ipython.
Also I invoked Ipython using the following command
ipython qtconsole --pylab=inline --colors=Linux
Can anyone help to find what can be the problem .

Works for me. Are you sure that the function name printval is already in your ipython namespace when you give the command %edit printval? You can check this by seeing whether printval? refers you to your function as expected, or gives an error. If printval isn't in your ipython namespace, then ipython has no way to know what you mean by printval.
For example, if you have already done from mymodule import printval,
then %edit printval should work.
If this doesn't address the issue, please provide a minimal example including all steps to reproduce the problem.
UPDATE after this followup information from user: " File: c:\users\tester\<ipython-input-5-cc5d372a3507>"
This shows that the function was not defined in a file as typically done, but rather interactively at the ipython prompt. This complicates the answer, thus:
When a function is defined in a (non-temporary) file, then %edit functionname works as desired in all versions of ipython - it opens that file for editing.
However when the function is defined interactively at an ipython prompt, as in this user's case, then the actual behavior depends on the version of ipython and which ipython/jupyter interface you are using. What one would like is for a temporary file to be opened, the function definition be copied into that file for editing, and then after the editor closes, for the function definition to be re-executed (i.e. for the function to be re-defined) based on those edits.
Indeed, this desired behavior is implemented in simple text-mode ipython terminal (e.g. if you type ipython at a Canopy Command Prompt).
However, this desired behavior has never been implemented in ipython/jupyter QtConsole (e.g. if you type jupyter qtconsole at a Canopy Command Prompt), and as far as I can tell, it is not currently planned by the jupyter team, most of whose effort seems to be focused on the jupyter notebook and jupyter lab projects.
Because Canopy is a GUI application, Canopy's ipython prompt is a QtConsole, not an ipython terminal prompt. Therefore Canopy does not implement the behavior that you want, and will not in the foreseeable future.
The (minor) good news is that in the most recent version of QtConsole, the error message is more explicit. It says "InteractivelyDefined" which at least gives a hint that the function that you were editing was defined interactively and is therefore not directly editable.
The workaround, of course, is to manually copy-paste the function definition source code into the Canopy editor (which automatically strips out the ipython prompts from the clipboard, making this very simple to do), edit the function there, and run that file.

Related

How to start a debug session using variables from interactive window, on Python for VSCODE?

I have my '.py' file, and I'm running its functions on the Python Interactive Window (the notebook-like feature of VSCODE).
All the variables and functions are stored only in this Jupyter environment. They're not even accessible on the terminal.
Img that shows the variables on my Jupyter env
When I click "Debug Cell" (as you see on the img), it starts debugging using the Jupyter Environment, with all variables, but it doesn't stop on the breakpoints I've put. Like it doesn't get linked with VSCODE.
If I type F5 or click on "Run and Debug", it starts the Debug Session running the entire .py file, which is not what I want.
In this case, it can access the breakpoints, but not my variables.
I just want to debug a specific part of my code using the variables that I have already created. I must have been missing something really simple.
The only answer I can find on the internet is "you should create a launch.json file", but it doesn't fix my situation.
Does anyone know what I'm missing?
When I click "Debug Cell" in the python file, it executes the result in the "Interactive" window and can stay at the breakpoint I set, and I can also see the value of the variable executed in the code in the "Interactive" window :
Since the debugging function of python code and "Interactive" function are provided by the python extension, and the kernel function of Jupyter also depends on the Jupyter extension, please try to reinstall these two extensions and reload VS Code.
My environment:
VS Code: 1.53.1 (user setup)
python: 3.9.1
VS Code extensions: Python(2021.1.502429796); Jupyter(2020.12.414227052); Pylance(2021.2.2).
OS: Windows_NT x64 10.0.19041

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

iPython automatically at launch in Visual Studio Code on Os X

I would like that iPython run automatically when I launch VSC instead of typing ipython and press enter in the terminal. The answer here How to set ipython/jupyter as the default python terminal for vscode? doesn't work as it is for windows but it shouldn't be really different. Also, is there something similar to the 'Execute' button in Spyder instead of typing %run filename ? Thanks !
I presume you mean you want to run the "Python Interactive Window" and not just an iPython console on startup
There is currently no way to run it on startup. At least no way without writing another extension that would run a command when opening a workspace. It would be simple for us to add one though. Probably a workspace setting. Can you log an issue here:
https://github.com/Microsoft/vscode-python/issues/new
For you second question, 'Execute' in spyder, we have 'Run Current File in Python Interactive Window'. This works on any python file. You can get to it through the context menu on a file or through the command palette.
Sadly the nice workflow of spyder is not provided by any official extension at the moment (as far as I know).
But you can implement the basics easily on your own by writing an extension. Even with no experience in TypeScript you can quickly build an extension which starts an IPython console as soon as you open a python file. I also managed to execute a startup script which implements the runfile method. VS Code also allows keybindings for your functions, so that you can almost work like you can with spyder.
Spyder modified the IPython terminal quite a bit though, so it won't feel exactly the same. But after all, everything there is open source so you could implement it yourself, which is what I'm trying to do in my free time.

Find the exact .py window/script that corresponds to the IPython console in Spyder

I would like to know if there is way to figure out (from the IPython console) the .py script that was used to run/execute the python commands interactively and thus got printed into the Ipython console.
For eg.
From the below screenshot, looking at the 3+3 in the Ipython console, I can see that it came in when command from untitled2.py was executed.
However when the scripts get long, and IPython output gets long and you are often shifting scripts on the left side, it can be hard to keep track.
So i was wondering if there is a way i can quickly execute some command or view some setting on Ipython console that can tell me that the above line came from untitled.py.
(Spyder maintainer here) There's no way to know from which Editor you're executing some portion of code, sorry.
However, you could use dedicated consoles (under Preferences > Run > Execute in a dedicated console) to have one console per file you execute, as long as you use F5 to run each one of them.
You could edit each .py script, so the first thing it does is to print its name and functionality.
untitled2.py:
print('untitled2.py')
...

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