1

I would like to always log ipython commandline and output to a file in append mode. On reading the file under .ipython/profile_default/startup README

$ 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

I tried adding a file first.py with below contents

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

logstart is a magic command. I am getting below error when I am trying to start ipython

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

How to resolve this issue?

1
  • 2
    To use magic commands in startup files, use a .ipy extension. .py files have to be regular Python code, without IPython special syntax. Commented Aug 26, 2016 at 7:42

1 Answer 1

1

"Thomas K" comment also looks like a solution, but here is different solution that I found myself

As he stated correct, we can only have python code in *.py file in the startup folder, I replaced my earlier text with below. Magic commands can be invoked using run_line_magic method

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", "")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.