如何在我的代码中嵌入一个IPython shell并让它自动显示调用它的行号和函数?
我目前有以下设置在我的代码中嵌入IPython shell:
from IPython.frontend.terminal.embed import InteractiveShellEmbed
from IPython.config.loader import Config
# Configure the prompt so that I know I am in a nested (embedded) shell
cfg = Config()
prompt_config = cfg.PromptManager
prompt_config.in_template = 'N.In <\\#>: '
prompt_config.in2_template = ' .\\D.: '
prompt_config.out_template = 'N.Out<\\#>: '
# Messages displayed when I drop into and exit the shell.
banner_msg = ("\n**Nested Interpreter:\n"
"Hit Ctrl-D to exit interpreter and continue program.\n"
"Note that if you …Run Code Online (Sandbox Code Playgroud) 我正在计算scipy.sparse矩阵(CSC)和numpy ndarray向量之间的点积:
>>> print type(np_vector), np_vector.shape
<type 'numpy.ndarray'> (200,)
>>> print type(sp_matrix), sparse.isspmatrix(sp_matrix), sp_matrix.shape
<class 'scipy.sparse.csc.csc_matrix'> True (200, 200)
>>> dot_vector = dot(np_vector, sp_matrix)
Run Code Online (Sandbox Code Playgroud)
结果似乎是一个新的ndarray矢量,正如我所料:
>>> print type(dot_vector), dot_vector.shape
<type 'numpy.ndarray'> (200,)
Run Code Online (Sandbox Code Playgroud)
但是当我尝试向该向量添加标量时,我收到异常:
>>> scalar = 3.0
>>> print dot_vector + scalar
C:\Python27\lib\site-packages\scipy\sparse\compressed.pyc in __add__(self, other)
173 return self.copy()
174 else: # Now we would add this scalar to every element.
--> 175 raise NotImplementedError('adding a nonzero scalar to a '
176 'sparse matrix is not supported')
177 elif isspmatrix(other): …Run Code Online (Sandbox Code Playgroud) 我正在使用IPython强大的并行化功能运行一堆长时间运行的任务.
如何在我的IPython客户端中从ipengines的stdout获得实时输出?
例如,我正在运行dview.map_async(fun, lots_of_args)并fun打印到stdout.我希望看到输出正在发生.
我知道AsyncResult.display_output(),但它只在所有任务完成后才可用.
不久前我在OSX上安装了python科学环境(numpy,scipy,matplotlib,pandas,ipython),使用brew和pip或easy_install(或其中2个,我不记得了).现在,如果尝试运行ipython,我得到:
-> ipython
-bash: ipython: command not found
Run Code Online (Sandbox Code Playgroud)
然后我做了:
-> sudo find . -iname "*ipython*"
Password:
find: ./dev/fd/3: Not a directory
find: ./dev/fd/4: Not a directory
./Users/jfk/.ipython
./usr/local/Cellar/matplotlib/1.2.0/lib/python2.7/site-packages/matplotlib/sphinxext/ipython_console_highlighting.py
./usr/local/Cellar/matplotlib/1.2.0/lib/python2.7/site-packages/matplotlib/sphinxext/ipython_console_highlighting.pyc
./usr/local/Cellar/matplotlib/1.2.0/lib/python2.7/site-packages/matplotlib/sphinxext/ipython_directive.py
./usr/local/Cellar/matplotlib/1.2.0/lib/python2.7/site-packages/matplotlib/sphinxext/ipython_directive.pyc
./usr/local/Cellar/matplotlib/1.2.1/lib/python2.7/site-packages/matplotlib/sphinxext/ipython_console_highlighting.py
./usr/local/Cellar/matplotlib/1.2.1/lib/python2.7/site-packages/matplotlib/sphinxext/ipython_console_highlighting.pyc
./usr/local/Cellar/matplotlib/1.2.1/lib/python2.7/site-packages/matplotlib/sphinxext/ipython_directive.py
./usr/local/Cellar/matplotlib/1.2.1/lib/python2.7/site-packages/matplotlib/sphinxext/ipython_directive.pyc
./usr/local/Cellar/python/2.7.4/Frameworks/Python.framework/Versions/2.7/share/doc/ipython
./usr/local/Cellar/python/2.7.4/Frameworks/Python.framework/Versions/2.7/share/doc/ipython/examples/core/ipython-get-history.py
./usr/local/Cellar/python/2.7.4/Frameworks/Python.framework/Versions/2.7/share/doc/ipython/examples/core/ipython-qtconsole.desktop
./usr/local/Cellar/python/2.7.4/Frameworks/Python.framework/Versions/2.7/share/doc/ipython/examples/core/ipython-sh.desktop
./usr/local/Cellar/python/2.7.4/Frameworks/Python.framework/Versions/2.7/share/doc/ipython/examples/core/ipython.desktop
./usr/local/Cellar/python/2.7.4/Frameworks/Python.framework/Versions/2.7/share/doc/ipython/examples/core/ipython_here_shell_extension.reg
./usr/local/Cellar/python/2.7.4/Frameworks/Python.framework/Versions/2.7/share/man/man1/ipython.1
./usr/local/lib/python2.7/site-packages/IPython
./usr/local/lib/python2.7/site-packages/IPython/config/profile/cluster/ipython_config.py
./usr/local/lib/python2.7/site-packages/IPython/config/profile/cluster/ipython_config.pyc
./usr/local/lib/python2.7/site-packages/IPython/config/profile/math/ipython_config.py
./usr/local/lib/python2.7/site-packages/IPython/config/profile/math/ipython_config.pyc
./usr/local/lib/python2.7/site-packages/IPython/config/profile/pysh/ipython_config.py
./usr/local/lib/python2.7/site-packages/IPython/config/profile/pysh/ipython_config.pyc
./usr/local/lib/python2.7/site-packages/IPython/config/profile/sympy/ipython_config.py
./usr/local/lib/python2.7/site-packages/IPython/config/profile/sympy/ipython_config.pyc
./usr/local/lib/python2.7/site-packages/IPython/frontend/html/notebook/static/codemirror/README-IPython.rst
./usr/local/lib/python2.7/site-packages/IPython/frontend/html/notebook/static/codemirror/theme/ipython.css
./usr/local/lib/python2.7/site-packages/IPython/frontend/qt/console/ipython_widget.py
./usr/local/lib/python2.7/site-packages/IPython/frontend/qt/console/ipython_widget.pyc
./usr/local/lib/python2.7/site-packages/IPython/frontend/qt/console/resources/icon/IPythonConsole.svg
./usr/local/lib/python2.7/site-packages/IPython/frontend/qt/console/rich_ipython_widget.py
./usr/local/lib/python2.7/site-packages/IPython/frontend/qt/console/rich_ipython_widget.pyc
./usr/local/lib/python2.7/site-packages/ipython-0.13.2-py2.7.egg-info
./usr/local/lib/python2.7/site-packages/matplotlib/sphinxext/ipython_console_highlighting.py
./usr/local/lib/python2.7/site-packages/matplotlib/sphinxext/ipython_console_highlighting.pyc
./usr/local/lib/python2.7/site-packages/matplotlib/sphinxext/ipython_directive.py
./usr/local/lib/python2.7/site-packages/matplotlib/sphinxext/ipython_directive.pyc
./usr/local/share/python/ipython
Run Code Online (Sandbox Code Playgroud)
我很惊讶没有看到任何/ bin目录.
然后我尝试重新安装ipython
-> pip install ipython
Requirement already satisfied (use --upgrade to upgrade): ipython in …Run Code Online (Sandbox Code Playgroud) 我有一个名为"poc.py"的脚本,它带有一个位置命令行参数"inputfile.txt".poc.py脚本用于argparse处理位置命令行参数,然后将args dict传递给main().进入后main(),我读取输入文件,进行一些处理,创建一个pandas DataFrame,最后绘制数据.我很难操纵我的DataFrame并控制结果图的确切格式,所以我想尝试ipython以交互方式探索这个,看看我是否能更好地掌握处理pandas/matplotlib的"pythonic"方法.
所以我尝试使用ipython并运行脚本,但是我无法让ipython保留我的脚本命名空间.
我试过这个:
$ ipython --pylab -i poc.py inputfile.txt
Run Code Online (Sandbox Code Playgroud)
哪个运行我的脚本就好了,并显示图(即使没有阻塞plt.show()调用),但是当脚本完成时,ipython who和whos命令说Interactive namespace is empty.同样,如果我首先进入ipython shell然后执行:
In [2]: run poc.py inputfile.txt
Run Code Online (Sandbox Code Playgroud)
当脚本完成时(再次,大量输出,图表显示)我得到相同的结果:一个空的交互式命名空间.
在理解如何运行外部并使用ipython以交互方式探索脚本中的数据/对象方面,我缺少什么?
这是我的脚本(poc.py)设置方式的准系统示例:
import numpy as np
import matplotlib as plt
import pandas as pd
# etc ...more libraries and custom functions here...
def main(args):
data = np.genfromtxt(args.inputfile)
# (omitted)...more data processing / manipulation...
pdata = pd.DataFrame(data)
# (omitted)...more data …Run Code Online (Sandbox Code Playgroud) 我想拥有
from __future__ import (absolute_import, division, print_function, unicode_literals)
Run Code Online (Sandbox Code Playgroud)
使用PYTHONSTARTUP文件在每个交互式会话中加载.这适用于基本的python(2.7.5,与Anaconda一起安装),但不适用于ipython(1.0.0,Anaconda).其他import语句与ipython一起使用,但__future__只是忽略了它们(尽管如果我在命令行输入它们它们就可以工作).这是一个ipython错误,还是有什么我想念的?
你如何让iPython使用乳胶输出结果?
例如,在此页面上:http://nbviewer.ipython.org/urls/raw.github.com/ipython/ipython/master/examples/notebooks/SymPy%20Examples.ipynb
如果我执行代码:
Rational(3,2)*pi + exp(I*x) / (x**2 + y)
Run Code Online (Sandbox Code Playgroud)
我得到输出:
Out[13]: 3*pi/2 + exp(I*x)/(x**2 + y)
Run Code Online (Sandbox Code Playgroud)
我想看看乳胶中的输出,如上面的链接所示.
伙计们,
我最近更新了我的Ubuntu到13.10版本.令我惊讶的是,Ipython和Matplotlib仅在我之前的12.04 LTS安装版本之前的版本中可用.
当使用12.04时,我在我的存储库中设置了Julian Taylor PPA,这就是为什么(我认为)我从两个软件包中获得了最新版本的原因.
目前在13.10中的版本是:
Matplotlib:1.2.1(http://matplotlib.org/index.html,最新版本是1.3.1)
Ipython:0.13.2(http://ipython.org,最新版本是1.1.0 - 2013年9月)
是否有13.10的PPA可用?JTaylor ppa仅涵盖12.10(Quantal,由于堆栈交换中的声誉等级,链接不可用)
我应该在apt之外安装这些软件包吗?这不麻烦吗?
谢谢.
我正在使用ipython笔记本执行我的第一步,并将其成功安装在我的远程服务器上(通过SSH),并使用以下命令启动它:
ipython notebook --ip='*' ---pylab=inline --port=7777
Run Code Online (Sandbox Code Playgroud)
然后我检查了http://myserver.sth:7777 /并且笔记本运行正常.然后我想关闭与服务器的SSH连接并让ipython在后台运行.当我这样做时,我无法连接到myserver.sth:7777.一旦我通过SSH再次连接到远程服务器,我就可以再次连接到笔记本电脑.然后我尝试使用screen启动ipython:我创建了一个新的屏幕screen -S ipy,我开始上面的ipython笔记本,我曾经Ctrl+A,D分离屏幕并退出到TTY.我仍然可以远程连接到笔记本电脑.然后我关闭了SSH连接,404 NOT FOUND当我尝试访问我之前存储的笔记本时遇到错误,我无法在http://myserver.sth:7777 /的笔记本列表中看到它.我试图创建一个新的笔记本,但我有一个500 Internal Server Error.
我也尝试过ipython notebook使用和不使用sudo.
有任何想法吗?
Windows:我在我的计算机上安装了Python软件包CVXOPT,用于常规的Python发行版,虽然不是专门用于Anaconda,所以当我在进行文本编辑器/ cmd python脚本编写时,它导入的很好.我尝试用Anaconda安装CVXOPT,但是没有用,所以我不得不在使用iPython时直接导入库.
我的目录结构如下:
C:
--Python27
----Lib
------site-packages
--------cvxopt
----------__init__.py
----------.....
Run Code Online (Sandbox Code Playgroud)
我在iPython笔记本中运行此代码时发生错误:
import sys
sys.path.append('C:\Python27\Lib\site-packages\cvxopt')
import cvxopt
Run Code Online (Sandbox Code Playgroud)
错误:
ImportError: No module named cvxopt
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?也许我正在错误地追加路径?
ipython ×10
python ×7
homebrew ×1
latex ×1
macos ×1
mathjax ×1
matplotlib ×1
numpy ×1
pandas ×1
pip ×1
python-2.7 ×1
scipy ×1
ubuntu ×1
ubuntu-13.10 ×1