pylab.plot exists but can't be imported? - python

EDIT: This example comes directly from the "ein" documentation for ipython notebooks in emacs: https://github.com/tkf/emacs-ipython-notebook
With fresh installation of ipython on Mac OS Yosemite:
ipython
Python 2.7.9 (default, Feb 12 2015, 17:01:13)
Type "copyright", "credits" or "license" for more information.
IPython 3.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: %pylab
Using matplotlib backend: MacOSX
Populating the interactive namespace from numpy and matplotlib
In [2]: dir(pylab)
long spew, but I verified that 'plot' is in the list
In [3]: from pylab import plot
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-3-8618f02f2b7e> in <module>()
----> 1 from pylab import plot
ImportError: cannot import name plot
No idea what's wrong. Googled the exact error message and didn't see anything helpful.

Related

messy code when using emacs ipython Inferior Python

I am using the newest emacs25.1.1 with the newest archlinux.
However, when I edit a python scripts and send it to the Inferior ipython by using Ctrl-Enter, the inferior Python show me a ugly messy code termial indicater like:
Python 3.5.2 (default, Nov 7 2016, 11:31:36)
Type "copyright", "credits" or "license" for more information.
IPython 5.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
[JIn [1]: import numpy as np[26D
[J
[6n[JIn [2]: [8D[8C[8D[J[6n[JIn [2]: [8D[8C
When press Enter in this Inferior python, it shows:
error in process filter: Args out of range: "
^[[6n^[[0m^[[0m^[[J^[[0;38;5;28mIn [^[[0;38;5;28m: ^[[8D^[[8C[[0m^[[0m", 128
I change the emacs locale from:
LANG=zh_CN.UTF-8
LC_CTYPE="zh_CN.UTF-8"
LC_NUMERIC=en_US.UTF-8
LC_TIME=en_US.UTF-8
LC_COLLATE="zh_CN.UTF-8"
LC_MONETARY=en_US.UTF-8
LC_MESSAGES="zh_CN.UTF-8"
LC_PAPER=en_US.UTF-8
LC_NAME="zh_CN.UTF-8"
LC_ADDRESS="zh_CN.UTF-8"
LC_TELEPHONE="zh_CN.UTF-8"
LC_MEASUREMENT=en_US.UTF-8
LC_IDENTIFICATION="zh_CN.UTF-8"
LC_ALL=
To:
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC=en_US.UTF-8
LC_TIME=en_US.UTF-8
LC_COLLATE="en_US.UTF-8"
LC_MONETARY=en_US.UTF-8
LC_MESSAGES="en_US.UTF-8"
LC_PAPER=en_US.UTF-8
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT=en_US.UTF-8
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
with the command $"export LANGUAGE="en_US:en" && emacs"$, no help to resolve the messy code.
I also try python-mode.et or elpy package in emacs, all show the same
messy terminal code.
Further,python2.7 also tried, no work:
Python 2.7.12 (default, Nov 7 2016, 11:55:55)
Type "copyright", "credits" or "license" for more information.
IPython 5.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
[JIn [1]: import numpy as np[26D
[J
[6n[JIn [2]: [8D[8C[8D[J[6n[JIn [2]: [8D[8C
Is the newest ipython problem?
At last the message in emacs paste here:
error in process filter: ansi-color-filter-apply: Args out of range: "Python 3.5.2 (default, Nov 7 2016, 11:31:36)
Type \"copyright\", \"credits\" or \"license\" for more information.
IPython 5.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
[6n[0m
[0m[J
[0m", 408
error in process filter: Args out of range: "Python 3.5.2 (default, Nov 7 2016, 11:31:36)
Type \"copyright\", \"credits\" or \"license\" for more information.
IPython 5.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
[6n[0m
[0m[J
[0m", 408
error in process filter: ansi-color-filter-apply: Args out of range: "[0m[0m[J[0;38;5;28mIn [[0;38;5;10;1m1[0;38;5;28m]: [0;38;5;28;1mimport[0m [0;38;5;32;1mnumpy[0m [0;38;5;28;1mas[0m [0;38;5;32;1mnp[26D[0m
[J[0m", 177
error in process filter: Args out of range: "[0m[0m[J[0;38;5;28mIn [[0;38;5;10;1m1[0;38;5;28m]: [0;38;5;28;1mimport[0m [0;38;5;32;1mnumpy[0m [0;38;5;28;1mas[0m [0;38;5;32;1mnp[26D[0m
[J[0m", 177
error in process sentinel: elpy-rpc--default-error-callback: peculiar error: "exited abnormally with code 1"
error in process sentinel: peculiar error: "exited abnormally with code 1"
error in process filter: ansi-color-filter-apply: Args out of range: #("[JIn [1]: import numpy as np[26D
[J
[6n[0m[0m[J[0;38;5;28mIn [[0;38;5;10;1m2[0;38;5;28m]: [8D[8C[0m[0m[8D[J[0m[6n[0m[0m[J[0;38;5;28mIn [[0;38;5;10;1m2[0;38;5;28m]: [8D[8C[0m" 0 38 (fontified nil) 38 39 (fontified nil)), 283
error in process filter: Args out of range: #("[JIn [1]: import numpy as np[26D
[J
[6n[0m[0m[J[0;38;5;28mIn [[0;38;5;10;1m2[0;38;5;28m]: [8D[8C[0m[0m[8D[J[0m[6n[0m[0m[J[0;38;5;28mIn [[0;38;5;10;1m2[0;38;5;28m]: [8D[8C[0m" 0 38 (fontified nil) 38 39 (fontified nil)), 283
I've used recomendation from https://github.com/jorgenschaefer/elpy/issues/992#issuecomment-249165923. In short - add the following code:
(setenv "IPY_TEST_SIMPLE_PROMPT" "1") into emacs configuration file.
From the output and error message, it would appear that for some reason, your setup is not handling the ansi escape sequences used by ipython. Assumiing you are using pythong-mode, ensure you have the following
(setq python-shell-interpreter "ipython"
python-shell-interpreter-args "-i")
To eliminate other possible settings in your init file, I would comment out everything except the above and try again. If it works, then there is something in your setup which is messing with the ability of emacs to handle the ansi control sequences being output by ipython. If it still doesn't work, then you may need to log a bug report.
there are also a few things you can do to work around the issue.
Try adding the --no-color-info switch to ipython. This will reduce the number of control characters ipython outputs, which my limit the problem.
Consider using just python rather than ipython as the inferior process. The problem you are seeing is due to ipython using ansi control characters to provide a nicer shell. However, emacs is really your shell and you may be better off letting emacs take care of the interface and just use a simpler inferior shell
The Emacs Wiki has a fairly detailed description on how to setup a powerful python development environment. Using a simple ipython/python inferior process is the most basic approach. You may do better putting in time with some of the other packages to get a more complete development environment.

iPython: DLL load failed: The specified module could not be found; plain Python fine

I keep getting this (well known) error in iPython. Yet, the same import works fine in plain Python. (Python 3.3.5, see details below)
iPython:
Python 3.3.5 (v3.3.5:62cf4e77f785, Mar 9 2014, 10:37:12) [MSC v.1600 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.
IPython 2.0.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import test1
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-7-ddb30f03c287> in <module>()
----> 1 import test1
ImportError: DLL load failed: The specified module could not be found.
Python (not only it loads fine, it also works):
$ python
Python 3.3.5 (v3.3.5:62cf4e77f785, Mar 9 2014, 10:37:12) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import test1
>>>
Now, Dependency Walker on test1.pyd shows this
[ ? ] LIBGCC_S_DW2-1.DLL Error opening file. The system cannot find the file specified (2).
[ ? ] LIBSTDC++-6.DLL Error opening file. The system cannot find the file specified (2).
[ ? ] PYTHON33.DLL Error opening file. The system cannot find the file spec
I even overwrote sys.path in iPython with the one from plain Python. The file test1.pyd is in C:\Test.
['c:\\Test',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5\\python33.zip',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5\\DLLs',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5\\lib',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5\\lib\\site-packages',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5\\lib\\site-packages\\FontTools',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5\\lib\\site-packages\\win32',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5\\lib\\site-packages\\win32\\lib',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5\\lib\\site-packages\\Pythonwin']
Why would the import work in plain Python but not in iPython?
I have encountered the same problem. After hours looking and thinking I found out the cause. The difference is environment variables between interpreters (plain python and ipython or pycharm etc.). I think your can use %env in ipython to check the environment variables. In plain python, use (works in python 3.7):
import os
os.environ
Then if there are differences, maybe you should set the right one before your run.
Actually there are multiple ways to set envs. For example
os.environ['key']='value' #Both key and value are strings
or
os.putenv('key', 'value')
Here key is the name of the environment variable, and value is the value you want to set it to.
Hope this helps you.~~~///(^v^)\\~~~

Why can't I access imported functions in Django's shell with ipython?

I have installed ipython in my virtualenv, so python manage.py shell gives my ipython. However, I can't access imports from inside function definitions:
$ python manage.py shell
Python 2.7.5 (default, Sep 6 2013, 09:55:21)
Type "copyright", "credits" or "license" for more information.
IPython 1.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: from re import search
In [2]: def my_search(pattern, string):
return search(pattern, string)
...:
In [3]: my_search('x', 'y')
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
/home/wilfred/.envs/drawbridge/lib/python2.7/site-packages/django/core/management/commands/shell.pyc in <module>()
----> 1 my_search('x', 'y')
/home/wilfred/.envs/drawbridge/lib/python2.7/site-packages/django/core/management/commands/shell.pyc in my_search(pattern, string)
1 def my_search(pattern, string):
----> 2 return search(pattern, string)
3
NameError: global name 'search' is not defined
This works fine if I start ipython directly. Why doesn't it work from the Django shell?
This is a known bug that was fixed in django 1.6. There are also some workarounds for earlier versions suggested in the issue discussion, take a look.
Also see:
Variables scope in inline django shell, vs python shell
Getting NameError with Django 1.5 and IPython

Matplotlib won't plot: dbus issue?

I have a rather strange scenario. I'm running on a very old installation, so I've compiled most of the tools I use.
First, this is a server. It is exclusively used by SSH-ing into it. But whoever set this server up made some strange choices. Instead of creating separate users, there is one user on login called fphyiscs, with a home directory of /home/fphysics. (Access is controlled strictly via public key/private key authentication. So everybody logs in as "fphysics", but there are no passwords.) Then there are "profile" directories in this one. For instance, mine is /home/fphysics/joelfred. There is a script that is run by fphysics on login that looks through the directories in /home/fphysics and interprets these as "profile". You can then choose which "profile" you want to use. The script then sources a file called "ENVS" in this profile directory.
I thought this was weird. But I know better than to argue with a sysadmin. So I work within this system. I set up my ENVS to source a more conventional .bash_profile, instead of just setting up environment variables as they probably intended. I set my HOME environment variable in ENVS: export HOME=/home/fphysics/joelfred. The software on this system is so outdated that I've compiled tons of my own stuff, using a prefix of $HOME/opt. So my path now includes $HOME/opt/bin. I sym-linked /home/fphysics/.Xauthority to /home/fphyiscs/joelfred/.Xauthority, so x sessions work over SSH. I honestly don't remember all the hacky things I've done to make this work more normally, and so I can have access to more current software. (For instance, GCC is v4.1.2, copyright 2006.)
I've since tried to compile and install Matplotlib, with the GTK3Agg backend. This means, of course, that I had to compile a lot of x libraries, GTK, pygobject, etc., etc. At some point along the line, I had to compile DBUS. Now, I have some interesting/annoying Matplotlib behavior. This, for instance, works as expected:
fphysics#facet-srv01 ~$ ipython
Python 2.7.3 (default, Apr 30 2013, 00:58:45)
Type "copyright", "credits" or "license" for more information.
IPython 0.13.2 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import matplotlib.pyplot as plt
** (ipython:1648): WARNING **: Couldn't connect to accessibility bus: Failed to connect to socket /tmp/dbus-oWyQu0DxxF: Connection refused
In [2]: import numpy as np
In [3]: x=np.linspace(-10,10,100)
In [4]: y=x*x
In [5]: plt.plot(x,y)
Out[5]: [<matplotlib.lines.Line2D at 0xacb2eec>]
In [6]: plt.show()
This works as expected - I get a plot of a parabola. I get a window that blocks my ipython terminal. The window, as far as I can tell, has full functionality. When I close it, I get returned to my ipython terminal. Note the strange dbus error though - I have NO IDEA where it gets the /tmp/dbus-oWyQu0DxxF idea. I've looked and I don't get it.
Okay. So now I want to turn on interactive mode:
fphysics#facet-srv01 ~$ ipython
Python 2.7.3 (default, Apr 30 2013, 00:58:45)
Type "copyright", "credits" or "license" for more information.
IPython 0.13.2 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import matplotlib.pyplot as plt
** (ipython:4181): WARNING **: Couldn't connect to accessibility bus: Failed to connect to socket /tmp/dbus-oWyQu0DxxF: Connection refused
In [2]: import numpy as np
In [3]: plt.ion()
In [4]: x=np.linspace(-10,10,100)
In [5]: y=x*x
In [6]: plt.plot(x,y)
Out[6]: [<matplotlib.lines.Line2D at 0xb0f1fcc>]
I get nothing. Weird. I try a show:
In [7]: plt.show()
Nothing. I try a draw:
In [8]: plt.draw()
I get a window with a title of "Figure 1", but it's gray. I try another draw:
In [9]: plt.draw()
Now I get a window with my parabola. But no buttons work. I can't even close the window. So I try a close:
In [10]: plt.close()
Nothing happens. Okay. I'll exit my ipython terminal:
In [11]: exit
Window goes away, no errors. But what happens if I don't do a show or a draw or just exit? I get weird errors:
fphysics#facet-srv01 ~$ ipython
Python 2.7.3 (default, Apr 30 2013, 00:58:45)
Type "copyright", "credits" or "license" for more information.
IPython 0.13.2 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import matplotlib.pyplot as plt
** (ipython:7262): WARNING **: Couldn't connect to accessibility bus: Failed to connect to socket /tmp/dbus-oWyQu0DxxF: Connection refused
In [2]: import numpy as np
In [3]: plt.ion()
In [4]: x=np.linspace(-10,10,100)
In [5]: y=x*x
In [6]: plt.plot(x,y)
Out[6]: [<matplotlib.lines.Line2D at 0xb636bf6c>]
In [7]: exit
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "/home/fphysics/joelfred/opt/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/home/fphysics/joelfred/opt/lib/python2.7/site-packages/matplotlib/_pylab_helpers.py", line 87, in destroy_all
manager.destroy()
File "/home/fphysics/joelfred/opt/lib/python2.7/site-packages/matplotlib/backends/backend_gtk3.py", line 415, in destroy
self.canvas.destroy()
AttributeError: FigureManagerGTK3Agg instance has no attribute 'canvas'
Error in sys.exitfunc:
Traceback (most recent call last):
File "/home/fphysics/joelfred/opt/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/home/fphysics/joelfred/opt/lib/python2.7/site-packages/matplotlib/_pylab_helpers.py", line 87, in destroy_all
manager.destroy()
File "/home/fphysics/joelfred/opt/lib/python2.7/site-packages/matplotlib/backends/backend_gtk3.py", line 415, in destroy
self.canvas.destroy()
AttributeError: FigureManagerGTK3Agg instance has no attribute 'canvas'
So I dug into dbus - the only error I can see that may be affecting things. I have two installations apparently:
fphysics#facet-srv01 ~$ $HOME/opt/bin/dbus-daemon --version
D-Bus Message Bus Daemon 1.7.2
Copyright (C) 2002, 2003 Red Hat, Inc., CodeFactory AB, and others
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
fphysics#facet-srv01 ~$ /bin/dbus-daemon --version
D-Bus Message Bus Daemon 1.1.2
Copyright (C) 2002, 2003 Red Hat, Inc., CodeFactory AB, and others
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Maybe they're competing? I don't know. I've symlinked /home/fphysics/.dbus to /home/fphysics/joelfred/.dbus. That doesn't seem to help. I'm at a loss. I have no idea what's going on. How do I diagnose this? How do I get dbus to work in my scenario? How do I test it on a more basic level? What in the world is going on??
BTW, Ipython makes the testing easier, but the behavior is the same if I run from a python prompt.

Problem with sh profile in IPython on Mac

I recently heard about profiles in python and quickly discovered the sh profile. I went to invoke it to try it out and got the following error on my Mac running Snow Leopard and the system Python2.6. Anyone else see this before? I was thinking about trying to reinstall the IPython egg to see if it fixed it, but thought I'd ask if any other users had seen something similar (on an Apple computer most likely).
mwoods 5 22:11:48 ~$ ipython -p sh
[IPythonApp] Config file not found, skipping: <ipython_config_sh.py>
Traceback (most recent call last):
File "/Library/Python/2.6/site-packages/ipython-0.11.bzr.r1205-py2.6.egg/IPython/core/application.py", line 223, in load_file_config
self.file_config = loader.load_config()
File "/Library/Python/2.6/site-packages/ipython-0.11.bzr.r1205-py2.6.egg/IPython/config/loader.py", line 227, in load_config
self._find_file()
File "/Library/Python/2.6/site-packages/ipython-0.11.bzr.r1205-py2.6.egg/IPython/config/loader.py", line 234, in _find_file
self.full_filename = filefind(self.filename, self.path)
File "/Library/Python/2.6/site-packages/ipython-0.11.bzr.r1205-py2.6.egg/IPython/utils/genutils.py", line 569, in filefind
(filename, path_dirs))
IOError: File does not exist in any of the search paths: 'ipython_config_sh.py', ('/Users/mwoods', u'/Users/mwoods/.ipython')
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
Type "copyright", "credits" or "license" for more information.
IPython 0.11.bzr.r1205 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.
In [1]:
I had success running ipython --profile=sh. However, the features of the sh profile (as advertised here) weren't available.

Categories