I wrote up the example code for Python logging module and discovered odd behavior. The logging messages duplicated each time I executed: %run main.py. At first the only way to clear the duplication was to exit iPython (CTRL-D). User unutbu posted an explantion and solution. Ok. Solved.
But as a matter of development and practice using iPython, Can I reset iPython (in a way that would remove these instances of the logger handler object) without restarting iPython?
This question has been asked before, but evidently they didn't have this test code to contend with.
Reset ipython kernel
How to clear variables in ipython?
The simple option was uneffective:
In [29]: %reset
Once deleted, variables cannot be recovered. Proceed (y/[n])? y
In [30]: %run main2
2018-05-12 1335:00 - WARNING - 3. This is a warning!
2018-05-12 1335:00 - WARNING - 3. This is a warning!
2018-05-12 1335:00 - WARNING - 3. This is a warning!
2018-05-12 1335:00 - WARNING - 3. This is a warning!
2018-05-12 1335:00 - WARNING - 3. This is a warning!
After restart, you will only see one of these lines, and not one for each previous execution of the %run magic.
Another suggestion didn't work as expected (maybe something changed since 2016?):
In [28]: import IPython
...: app = IPython.Application.instance()
...: app.kernel.do_shutdown(True)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-28-59afc6e6c255> in <module>()
1 import IPython
2 app = IPython.Application.instance()
----> 3 app.kernel.do_shutdown(True)
AttributeError: 'TerminalIPythonApp' object has no attribute 'kernel'
I'm running Arch with i3, and another suggestion of CTRL-. doesn't work.
Related
I'm learning Python 3.7, and Sublime Text3. Most programs run fine using control + b, but when I try to import a module from the standard library like random, I get the following error(Please note that I am not running the program from OneDrive, but from my own computer!)
Traceback (most recent call last):
File "C:\Users\chris\OneDrive\Documents\Python Programs\guess.py", line 1, in <module>
import random
File "C:\Users\chris\OneDrive\Documents\Python Programs\random.py", line 1
>>> import os
^
SyntaxError: invalid syntax
Obviously the OneDrive thing is a problem ,but what can I do?
Many thanks.
The part of the error message that you want to focus on here is the last part of the stack trace, which is showing you the line that has the problem and where the problem is, i.e.:
>>> import os
^
SyntaxError: invalid syntax
Based on this, your input file has the characters >>> in front of the import statement which, as the message indicates, is not valid Python syntax. Removing those characters should solve the problem for you. The line immediately above the error is showing you the name of the file that has the problem (in this case random.py).
The >>> is the prompt that the interactive Python interpreter gives you when you run it, as it's prompting you for what text to enter. That's not something that should appear in your code at all.
Check the random.py file, as the error message states, it's a syntax error due to the the >>> repl symbol in there.
While working the examples in a book on Parallel Programming in Python, I encountered the following error in code that uses the multiprocessing queue:
File "C:\pyDev\multiproc\queue-test.py", line 4, in <module>
queue = multiprocessing.Queue()
File "C:\Anaconda3\Lib\multiprocessing\context.py", line 100, in Queue
from .queues import Queue
File "C:\Anaconda3\Lib\multiprocessing\queues.py", line 20, in <module>
from queue import Empty, Full
builtins.ImportError: cannot import name 'Empty'
After some experimentation, I determined that all it takes to generate this error is the following code (which is the entirety of the queue-test.py file mentioned in the error message above).
import multiprocessing
if __name__ == "__main__":
queue = multiprocessing.Queue()
I'm running Python 3.5.1 with Anaconda 4.1.0 on a machine with Windows 7. I have ported the code above and the example from the book to another machine with Python 2.7.11 with Anaconda 2.5.0, with Windows 10 and it works fine on that machine.
Thinking that perhaps there was a corrupt file or other issue with my Python installation, I tried reinstalling Anaconda and it did not help. I performed a Google search and did not find this particular error. Other stackoverflow postings such as the one found here: ImportError: Cannot import name X don't appear to be relevent because this involves part of the Python standard library and not code and classes I wrote myself.
I encountered almost the same error in my code and finally figure out where went wrong. Hope it will help you somehow.
I named my python script as "queue.py", and then I run it, I got error info. below just like yours:
Traceback (most recent call last):
File "F:/02_Coding/01_Projects/PyHomeWork/Day23_Process/queue.py", line 19, in <module>
queue = multiprocessing.Queue()
File "E:\02_CodingSoftware\02_Installed\Anaconda3\lib\multiprocessing\context.py", line 101, in Queue
from .queues import Queue
File "E:\02_CodingSoftware\02_Installed\Anaconda3\lib\multiprocessing\queues.py", line 20, in <module>
from queue import Empty, Full
ImportError: cannot import name 'Empty'
I use the below method to create a Queue:
if __name__ =="__main__":
queue = multiprocessing.Queue()
Finally, I notice that I should not named the file in a name of "queue.py", it seems it will cause a misleading to python when it interpreter the script. And after I rename the script, the error is gone. What a stupid mistake, LoL.
So my suggestion is maybe you can check whether under your folder is there a script named as "queue.py" or any custom module would conflict with that in libraries.
Hope you can solve the issue. Best
PsychoPy looks like just what I need. But I want to use my own development environment (a straightforward IPython prompt combined with the editor of my choice) instead of the provided IDE.
The trouble is that you seem to have to quit Python and relaunch after every PsychoPy run. If for example I cd to the ...../demos/coder/stimuli directory and type run gabor.py it runs fine, but if I then type run gabor.py again I get this exception from pyglet:
C:\snap\PsychoPy2\lib\site-packages\pyglet\window\win32\__init__.pyc in _create(self)
259 if not self._wgl_context:
260 self.canvas = Win32Canvas(self.display, self._view_hwnd, self._dc)
--> 261 self.context.attach(self.canvas)
262 self._wgl_context = self.context._context
263
C:\snap\PsychoPy2\lib\site-packages\pyglet\gl\win32.pyc in attach(self, canvas)
261 self._context = wglext_arb.wglCreateContextAttribsARB(canvas.hdc,
262 share, attribs)
--> 263 super(Win32ARBContext, self).attach(canvas)
C:\snap\PsychoPy2\lib\site-packages\pyglet\gl\win32.pyc in attach(self, canvas)
206 raise RuntimeError('Share context has no canvas.')
207 if not wgl.wglShareLists(share._context, self._context):
--> 208 raise gl.ContextException('Unable to share contexts')
209
210 def set_current(self):
ContextException: Unable to share contexts
Is there some sort of pyglet.cleanup() I can call (analogous to pygame.quit()) to allow PsychoPy scripts to run more than once in the same session? Or other way of avoiding this problem?
I'm using the Standalone PsychoPy distro version 1.81.02, untouched. The problem is not specific to IPython---it also can also be demonstrated from the plain Python prompt if you disable sys.exit and type execfile('gabor.py') twice:
C:\snap\PsychoPy2\Lib\site-packages\PsychoPy-1.81.02-py2.7.egg\psychopy\demos\coder\stimuli>python
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import sys; sys.exit = lambda x:x
>>> execfile('gabor.py')
0.6560 WARNING Movie2 stim could not be imported and won't be available
1.6719 WARNING Monitor specification not found. Creating a temporary one...
>>>
>>> execfile('gabor.py')
Traceback (most recent call last):
[snip]
File "C:\snap\PsychoPy2\lib\site-packages\pyglet\gl\win32.py", line 208, in attach
raise gl.ContextException('Unable to share contexts')
pyglet.gl.ContextException: Unable to share contexts
I don't know how to undo all the pyglet/psychopy initialisation - neither are really designed for you to do this, so there would be some work here. But I'm not sure it's a good idea anyway to run scripts the way you are doing.
The PsychoPy app itself gets around the by launching each script in a new process. It means that you know the namespace is clean on each run. Running your script on top of the previous one can lead to some really hard-to-find bugs because you don't know in what state the previous script left the memory, graphics card and namespace.
cheers
Jon
Got some error message in buffer Warning when try to evaluate my code
Error (websocket): in callback `on-open': Cannot send message to a closed websocket: cl-struct-websocket-frame, text, "{\"header\":{\"msg_id\":\"14735853-0ed2-4ef4-8f5a-312a198dd251\", \"username\":\"username\"\, \"session\":\"ab484abf-d4e6-4f83-2c2-de1b65264780\", \"msg_type\":\"execute_request\"}, \"metadata\":{}, \"content\":{\"code\":\"__import__('sys').stdout.write(__import__('os').getcwd())\", \"silent\":true, \\"user_variables\":[], \"user_expressions\":{}, \"allow_stdin\":false}, \"parent_header\":{}}", nil, t
Error (websocket): in callback `on-open': Wrong type argument: arrayp, nil
Error (websocket): in callback `on-close': Wrong type argument: arrayp, nil
And on the ipython server side I saw this message
2014-03-05 23:24:40.760 [NotebookApp] WARNING | Cross Origin WebSocket Attempt.
2014-03-05 23:24:40.761 [tornado.application] ERROR | Uncaught exception in /kernels/2691bbe3-ce95-439b-a9ce-97ec52c80988/shell
Traceback (most recent call last):
File "/home/haroldwu/Python-test/lib/python3.3/site-packages/tornado/websocket.py", line 322, in wrapper
return callback(*args, **kwargs)
File "/home/haroldwu/Python-test/lib/python3.3/site-packages/IPython/html/base/zmqhandlers.py", line 114, in open
raise web.HTTPError(404)
tornado.web.HTTPError: HTTP 404: Not Found
What I have done:
Upgrade all package of my python virtualenv and global environment
Upgrade all system package (Archlinux latest 64-bit)
Upgrade all emacs package
Search the web (but found no useful result)
Ipython notebook worked on my emacs weeks ago, but somehow it dosen't work now...
I have no experience on tornado.
Thanks for your help!!! :)
I'd guess that emacs is not sending the Origin header which is checked by the Notebook WS code. See my comment https://github.com/ipython/ipython/issues/5525 .
Quick workaround is to comment out the same origin check in your /home/haroldwu/Python-test/lib/python3.3/site-packages/IPython/html/base/zmqhandlers.py:114 .
You can always add logging of parsed_origin, origin and origin_header in same_origin() function in zmqhandlers.py and fix the emacs package accordingly.
I am using Enthought's Canopy environment on a 64 bit Linux OS. Everything works fine in the Ipython console which is attached with the editor. But when I ipython in the terminal and try to use magic functions, I get the following error.
---------------------------------------------------------------------------
error Traceback (most recent call last)
<ipython-input-3-29a4050aa687> in <module>()
----> 1 get_ipython().show_usage()
/home/shahensha/Development/Canopy/appdata/canopy-1.0.3.1262.rh5-x86_64/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in show_usage(self)
2931 def show_usage(self):
2932 """Show a usage message"""
-> 2933 page.page(IPython.core.usage.interactive_usage)
2934
2935 def extract_input_lines(self, range_str, raw=False):
/home/shahensha/Development/Canopy/appdata/canopy-1.0.3.1262.rh5-x86_64/lib/python2.7/site-packages/IPython/core/page.pyc in page(strng, start, screen_lines, pager_cmd)
188 if screen_lines <= 0:
189 try:
--> 190 screen_lines += _detect_screen_size(screen_lines_def)
191 except (TypeError, UnsupportedOperation):
192 print(str_toprint, file=io.stdout)
/home/shahensha/Development/Canopy/appdata/canopy-1.0.3.1262.rh5-x86_64/lib/python2.7/site-packages/IPython/core/page.pyc in _detect_screen_size(screen_lines_def)
112 # Proceed with curses initialization
113 try:
--> 114 scr = curses.initscr()
115 except AttributeError:
116 # Curses on Solaris may not be complete, so we can't use it there
/home/shahensha/Development/Canopy/appdata/canopy-1.0.3.1262.rh5-x86_64/lib/python2.7/curses/__init__.pyc in initscr()
31 # instead of calling exit() in error cases.
32 setupterm(term=_os.environ.get("TERM", "unknown"),
---> 33 fd=_sys.__stdout__.fileno())
34 stdscr = _curses.initscr()
35 for key, value in _curses.__dict__.items():
error: setupterm: could not find terminfo database
So, I installed a bare bones iPython shell which is not the one provided by Canopy and tried the same magic functions in there and it works fine.
Have I done something wrong with the installation? Please help
Thanks a lot
shahensha
This is not a solution, but just an observation. My desktop is MacOS-X and I connect to a Centos machine to run Enthought Canopy both 64 bit. I get the same error message as OP if I ssh from iterm2, but not if I use the Terminal app.
I am not sure what the underlying reason is, but may be someone can verify if a similar situation is true for linux. Interestingly I can use either iterm2 or Terminal on the local canopy without any issues.
Update:
I just noticed that the TERM environment variable in iterm2 was set to "xterm" while the Terminal app was showing "xterm-256color". Issuing the command export TERM="xterm-256color" before running the Canopy ipython in terminal solves the issue for me in iterm2.
Problem reproduction:
$ python -c 'import curses; curses.setupterm()'
Traceback (most recent call last):
File "<string>", line 1, in <module>
_curses.error: setupterm: could not find terminfo database
This irc log gave me the idea that this error was to do with libncursesw.
My Canopy version is 1.0.3.1262.rh5-x86_64. I have installed it to ~/src/canopy.
In ~/src/canopy/appdata/canopy-1.0.3.1262.rh5-x86_64/lib we can see that my canopy install has libncursesw.so.5.7.
My machine (Debian Wheezy 64bit) has libncursesw.so.5.9 (in /lib/x86_64-linux-gnu/libncursesw.so.5.9). I made canopy use this. You can toggle the problem on / off by using LD_PRELOAD and pointing at the .so file.
Solution
Replace libncurses.so.5.7 with libncurses.so.5.9:
CANOPYDIR=$HOME/src/canopy
CANOPYLIBS=$CANOPYDIR/appdata/canopy-1.0.3.1262.rh5-x86_64/lib/
SYSTEMLIBS=/lib/x86_64-linux-gnu
cp $SYSTEMLIBS/libncurses.so.5.9 $CANOPYLIBS
ln -sf $CANOPYLIBS/libncurses.so.5.9 $CANOPYLIBS/libncurses.so.5
It appears that Canopy User Python is not your default. See this article:
https://support.enthought.com/entries/23646538-Make-Canopy-s-Python-be-your-default-Python-i-e-on-the-PATH-
Update: Not true here -- instead, see batu's workaround answer.