how can I show all warnings in the iPython console in Spyder? - ipython

The iPython console in Spyder (when run from within a tab in Spyder) seems to strip warnings by default. This behavior is different from the iPython console run on its own.
It is easy to reproduce. Create a file containing only the following:
import sys
print sys.argv
Note that this is invalid Python 3 because print does not have parentheses. Run this within iPython (not within Spyder) and you will see the following:
In [305]: run test.py test args
File "C:\Users\sdewey\Documents\intro to ML with python\test.py", line 13
print sys.argv
^
SyntaxError: Missing parentheses in call to 'print'
If you run the same thing within the iPython tab within Spyder, you'll get a blank response:
In [9]: run test.py test args
In [10]:
Note that if you run a program which does not have errors, you will see the result in Spyder as usual. For example if you fix the parentheses here, you will see the printed argv in the console. Only error output seems to be impacted, not standard output.
I obtained this copy of Spyder through Anaconda 4.3.1. This is Spyder 3.1.2, Python 3.6. I've looked through the Spyder preferences, but haven't seen anything that addresses what to do with failures.
My working hypothesis is that this has something to do with error output vs standard output. This is a Windows installation so I don't think there are different streams but I could be mistaken.

(Spyder developer here) You need to update the qtconsole package to its version 4.3 to fix this error.
If you are using Anaconda, you need to open a terminal (cmd.exe) and run this command
conda update qtconsole
If not, you need to run
pip install -U qtconsole

Related

Can I execute a file (or some lines of python) within a running jupyterlab kernel for a notebook?

I've got a notebook that has got a bit unwieldy and I'm doing some refactoring which isn't fun.
I was wondering if it would be possible to execute code in this notebook from the command line for debugging.
Ideally, I would run something like:
run-in-jupyter $notebook file.py
and see the output from the command line. There is an interpreter in jupyterlab that can do this, so this make me think that it is possible.
I have a brief search but couldn't find much
How to run an .ipynb Jupyter Notebook from terminal? I explicitly don't want to do this (I want to run commands in an existing instace)
There is this library but this seems quite involved and some of the results I found on the internet where people not being able to use the library
jupyter console (pip install jupyter-console) connects to a running jupyter kernel from the kernel. Details on running kernels can be found amongst jupyter's run time files, on my box these live in ~/.local/share/jupyter/runtime. You can find the path to the kernel data file corresponding to an open workbook with %config IPKernelApp.connection_file which will look something like ~/.local/share/jupyter/runtime/kernel-55da8a07-b67d-4584-9ec6-f24e4a26cbbd.json.
You can then connect from the command line with
jupyter console --existing ~/.local/share/jupyter/runtime/kernel-55da8a07-b67d-4584-9ec6-f24e4a26cbbd.json
You can pipe commands into it as shown
echo h=87 | jupyter console --existing 55da8a07-b67d-4584-9ec6-f24e4a26cbbd 'h=57' --simple-prompt -y

Wrong Python interpreter being used by VS Code

I am on Ubuntu 20.04 and have both Python2 and Python3 installed natively. I have also installed Python through miniforge, a variant of miniconda. In VSCode I have both the MS Python extension and Pylance installed.
I use the miniforge python for my coding. This works perfectly fine in PyCharm.
However in VSCode, when I try to execute the same file I get errors. After investigating it seems that VSCode is picking native Python2 - even though I have the miniforge Python selected. In this picture it can be seen that the status bar at the bottom states Python interpreter selected is Python3. But the output window shows that the python interpreter is Python2.
A more confusing thing is when I use VSCode for Jupyter notebook files then it picks up the interpreter correctly and I have no issues.
I have checked both User and Workspace settings, and they all point to Python3. How can I fix this for standard .py files?
I prefer VSCode to PyCharm, but will need to use PyCharm till this is resolved.
It seems that your system console cannot see python3. You need to put the python3 in the PATH variable, before python2. Like:
PATH=path/to/python3:path/to/python2:$PATH
Also, make sure that the environment containing python3 is activated before command prompt appears. It can be done in bash_profile by adding a line like
conda activate my_env_with_python3
Try changing the settings "Python:Python path", "Python:default interpreter path" and "Python:conda path" also.
I have just bumped into something similar. The Run code option resulted in the file being run with the default interpreter instead of the venv-based one with necessary packages installed.
The fix was simply to use "Run python file" instead:
The run-code behavior must be customizable, something is mentioned e.g. here: Run Code vs Run Python File in Terminal for VSCODE but I didn't bother.

Jupyter - ending multiline magic command in console

In a Jupyter notebook, a "cell magic" command (prefixed with two percent signs) ends at the end of the cell and is automatically invoked:
But if I try the same thing in the Jupyter console, the command never ends, after any number of blank lines:
That only happens if I invoke IPython as jupyter console, though. If I invoke IPython directly as ipython, the cell magic command completes after one blank line:
For each example, the versions of Python and IPython are identical: Python 3.5.2 and IPython 6.1.0 on one machine (installed with pip), Python 3.4.5 and IPython 5.1.0 on another (installed with Conda).
Is this a bug? Or is there Jupyter default configuration somewhere for IPython that differs from IPython's own default configuration?
Yes, it is a bug, the cell magics are suppose to ends after 2 new lines.
The Jupyter console is way under maintained – we are not aware of many people using it, and it has plenty of issues.
Technically I believe the jupyter console does not use (or respect) the "is_complete" message from the protocol that should tell it wether the snippet of code should be executed or if a newline should be inserted.
You can try to open a bug report, but the fix will probably not be implemented quickly unless someone does a PR.

Display of music21, musicXML PNG objects using iPython Notebook/Enthought Canopy

I am experimenting with the music21 library, in preparation for a Machine Learning project that involves genre classification and categorization. I and following some tutorials available here. I am using MuseScore as my MusicXML program, and I am trying to run the whole thing from iPython.
Although I can run the some of the turtorials from the terminal, some elements don't seem to run well from inside iPython. For example:
In [3]: sBach.show()
Out[3]: <music21.ipython21.objects.IPythonPNGObject at 0x10da0aa10>
The line above shows that the PNG object is created, but not displayed. The expected output for 3 above is the following:
Experimenting with the following iPython command, I get a placeholder for an image, but not image.
In [6]: %load_ext music21.ipython21.ipExtension
In [7]: sBach.show()
I can't find any problem with my MusicXMLPath. This tutorial refers to the use of musc21 with Anaconda, but all my developments is done with Enthought, so I prefer not to run another virtual environment to use music21 with iPython.
Is there any way to run music21 in an Enthought/iPython notebook?
I have been grappling with this issue myself. ... Have you set your musicxmlPath in music21? If you have not, it's done via environment.set(key, value). You can query for available keys with environment.keys(). I hope this isn't too simple an answer, but it cleared up the problem for me.
This should be in the iPython music21 documentation somewhere, my apologies: iPython in music21 requires Lilypond to be installed for images to be generated within the notebook itself. There hasn't been (and won't be until MuseScore 2.0 is released) a way using MusicXML to generate PNG images of scores directly.
Edit: 2015 July; music21 2.0 w/ MuseScore 2 will generate the PNG images with MuseScore if it is installed and fallback to Lilypond if it is not installed.
If not yet tried, some steps to isolate the cause of the problem:
1) Update to the latest Canopy (Edit: currently 1.4.1) (might help this, will help generally, won't hurt).
2) Change the Pylab backend in Canopy's IPython shell to "Inline (SVG)", via Preferences / Python. (The default Qt backend in that shell conflicts with music21's use of the tkinter library.)
3) Test your script in that shell rather than in the notebook.
4) Ensure that Canopy User Python is your default Python in a Terminal window, as described here.
5) Test your scripts inside of plain ipython terminal (from Terminal, type ipython).
6) Test in ipython terminal in pylab mode (ipython qtconsole --pylab=inline).
7) Test your scripts inside of ipython notebook running in a regular browser (from Terminal, type ipython notebook, and/or ipython notebook --pylab=inline).
Had similar issues before. It's the same problem when people try to use plot function in ipython/jupyter notebook. You need to call
%matplotlib inline
For me the issue was solved by uninstalling the snap version of musescore and installing it from ppa:mscore-ubuntu/mscore3-stable via https://launchpad.net/~mscore-ubuntu/+archive/ubuntu/mscore3-stable

Python & Eclipse - Get curent filename error

I decided to start using Eclipse to code in python, but I am having problems getting the filename of the current script. To get the current filenam I use:
#!/usr/bin/env python2.7
import os
os.path.basename(__file__)
My code works flawlessly when using the bash terminal or Geany, but with Eclipse Interactive Console I get:
name '__file__' is not defined
Any ideas?
UPDATE
When I run my code using python filename.py it works perfectly, but when I try running it from the python or ipython consoles I get the same error
Ok, found an alternative method that works with PyDev's interactive console from:
How to properly determine current script directory in Python?
The solution is:
#!/usr/bin/env python2.7
import inspect
filename = inspect.getframeinfo(inspect.currentframe()).filename.split('/')[-1]