簡體   English   中英

如何使用腳本重新創建IPython的'--pylab'選項的效果?

[英]How can I recreate the effect of IPython's '--pylab' option with a script?

我想創建一個與IPython的--pylab標志“手動”相同的配置文件。 腳本的內容應該是什么 - 包導入,命名空間指定,設置等 - 來實現這一目標?

作為替代方案,我還想知道是否有辦法檢查當前IPython會話啟動時是否設置了--pylab標志。


簡單地from matplotlib.pylab import *不起作用, %pylab也不起作用(因為在配置文件中似乎不允許使用魔法)。

深入了解幫助和手冊頁( $ ipython --help-all然后搜索“pylab”)顯示:

--pylab=<CaselessStrEnum> (InteractiveShellApp.pylab) # mean is an alias for

--InteractiveShellApp.pylab=<CaselessStrEnum>
    Default: None
    Choices: ['auto', 'gtk', 'inline', 'osx', 'qt', 'qt4', 'tk', 'wx']
    Pre-load matplotlib and numpy for interactive use, selecting a particular
    matplotlib backend and loop integration.
--InteractiveShellApp.pylab_import_all=<Bool>
    Default: True
    If true, IPython will populate the user namespace with numpy, pylab, etc.
    and an ``import *`` is done from numpy and pylab, when using pylab mode.
    When False, pylab mode should not import any names into the user namespace.


所以在你加載的任何配置配置文件中 c.InteractiveShellApp.pylab='auto'都可以解決問題。

(同意Andrew這樣隱藏的是unpythonic。)

在ipython意義上,您可以通過創建新的iPython配置文件來實現此目的

ipython profile create <name>

編輯ipython_config.py目錄。 在Linux下更新的ipython安裝時,這個文件將在~/.config/ipython/profile_<name>目錄中,但如果您不確定,可以找到它:

ipython profile locate <name>

對我來說,我可以使用bash命令編輯相應的文件:

vim `ipython profile locate <name>`/ipython_config.py

編輯該文件中的相應變量(請參閱ipython文檔)。


在更一般的意義上,您可以通過設置PYTHONSTARTUP環境變量並將其指向包含命令的python文件,強制python在啟動時運行任意代碼。 要等效於--pylab ,該文件需要具有以下內容(相當於ipython 1.1)。

# See: http://ipython.org/ipython-doc/rel-1.1.0/api/generated/IPython.core.magics.pylab.html
import numpy
import matplotlib
from matplotlib import pylab, mlab, pyplot
np = numpy
plt = pyplot

from IPython.display import display
from IPython.core.pylabtools import figsize, getfigs

from pylab import *
from numpy import *

根據過去的經驗設置你的PYTHONSTARTUP變量可能是一個壞主意 除非您設置的后端沒有彈出繪圖窗口,否則您的所有Python代碼都必須具有x會話(或等效的)。 它還會污染您運行的所有內容的全局命名空間,並會降低解釋器的速度(特別是matplotlib需要花費大量時間在我的計算機上導入)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM