Jupyter Notebook运行包含外部参数的Python程序报错

报错如下:

usage: ipykernel_launcher.py [-h] [--input [INPUT]]
                             [--label-input [LABEL_INPUT]] [--output [OUTPUT]]
                             [--dimensions DIMENSIONS] [--walk-step WALK_STEP]
                             [--num-walks NUM_WALKS]
                             [--window-size WINDOW_SIZE] [--iter ITER]
                             [--workers WORKERS] [--max-hop MAX_HOP]
                             [--weighted] [--unweighted] [--directed]
                             [--undirected]
ipykernel_launcher.py: error: unrecognized arguments: .....json

An exception has occurred, use %tb to see the full traceback.

SystemExit: 2

原因为Jupyter会读入一个默认的环境参数-f ;所以为了解决这个问题,我们可以手动移除或者忽略这个参数。
两个简单的solution:
Solution 1:

import sys
if __name__ == '__main__':
    if '-f' in sys.argv:
        sys.argv.remove('-f')
    main()

Solution 2:
把parser.parse_args()改为parser.parse_known_args()[0]

你可能感兴趣的:(炼丹玄学)