2016-08-24 77 views
1

我想永遠記錄ipython命令行並輸出到附加模式的文件。對下.ipython/profile_default/startup自述ipyton啓動腳本語法錯誤

$ cat README 
This is the IPython startup directory 

.py and .ipy files in this directory will be run *prior* to any code or files specified 
via the exec_lines or exec_files configurables whenever you load this profile. 

Files will be run in lexicographical order, so you can control the execution order of files 
with a prefix, e.g.:: 

    00-first.py 
    50-middle.py 
    99-last.ipy 

讀取文件我嘗試添加一個文件first.py下面內容

logstart -o -r -t logs/ipython.log 'append' 

logstart是一個神奇的命令。當我嘗試啓動ipython時出現以下錯誤

File "/home/mtk/.ipython/profile_default/startup/00-first.py", line 1 
    logstart -o -r -t logs/ipython.log 'append' 
         ^
SyntaxError: invalid syntax 

如何解決此問題?

+1

要在啓動文件中使用魔術命令,請使用'.ipy'擴展名。 '.py'文件必須是常規的Python代碼,不需要IPython特殊的語法。 –

回答

1

「托馬斯·K」的評論也看起來像一個解決方案,但這裏是不同的解決方案,我發現自己

正如他說是正確的,我們只能在* .py文件的Python代碼在啓動文件夾,我用以下替換我的早期文本。可以使用run_line_magic方法調用魔術命令

get_ipython().run_line_magic(u"logstart", u"-o -r /home/mt/Dropbox/a/logs/ipython_log.py append") 
get_ipython().run_line_magic(u"logstate", "")