python - 如何将命令行参数传递给 ipython

标签 python ipython

在使用 ipython 时,有什么方法可以通过命令行将参数传递给我的 python 脚本?理想情况下,我想将我的脚本称为:

ipython -i script.py --argument blah

并且我希望能够在我的 sys.argv 中列出 --argumentblah

最佳答案

您可以在此之前使用一个 -- 更多选项:

ipython  script.py -- --argument blah

Ipython的帮助:

ipython [subcommand] [options] [-c cmd | -m mod | file] [--] [arg] ...

If invoked with no options, it executes the file and exits, passing the
remaining arguments to the script, just as if you had specified the same
command with python. You may need to specify `--` before args to be passed
to the script, to prevent IPython from attempting to parse them. If you
specify the option `-i` before the filename, it will enter an interactive
IPython session after running the script, rather than exiting.

演示:

$ cat script.py 
import sys
print(sys.argv)

$ ipython  script.py -- --argument blah
['script.py', '--argument', 'blah']

$ ipython  script.py -- arg1 arg2
['script.py', 'arg1', 'arg2']

关于python - 如何将命令行参数传递给 ipython,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22631845/

相关文章:

python - 如何在 python 中使用 Try/Catch

python - Sqlalchemy:engine.execute() 的 postgresql paramstyle

python - 我的输入函数的两个版本,递归与 while 循环?

python - 如何使用 Vim 选择 Python 代码块?

python - 评估我的 Python 服务器结构

python - Tensorflow 可视化工具 "Tensorboard"在 Anaconda 下不工作

python - 我如何配置 IPython 5 以具有 IPython 3 的 readline、tab 完成、(缺少)语法高亮等? (self.IPython)

python - 如何将 ipython/jupyter 设置为 vscode 的默认 python 终端?

python - % 在 Ipython 中的库前签名

python - ipython并行和非复制发送numpy数组