简体   繁体   English

如何在 vscode 中为 Python 执行添加命令行选项?

[英]How to add a command line option to Python execution in vscode?

It was my understanding that to add a command line option (in my case I want the -i option) to the python execution in VScode, I was to alter the variable "python.terminal.launchArgs"我的理解是,要在 VScode 中向 python 执行添加命令行选项(在我的情况下,我想要 -i 选项),我将更改变量“python.terminal.launchArgs”

As this is curiously not working, I am going to describe the steps I took, so that perhaps some more knowledgeable person can spot my mistake.由于这奇怪地不起作用,我将描述我采取的步骤,以便可能有更多知识的人可以发现我的错误。

  1. I went to file > preferences > settings我去了文件>首选项>设置

  2. then I searched for python然后我搜索了python

  3. The first mention I found to settings.json was in Python > Analysis:Disabled.我发现 settings.json 第一个提到的是 Python > Analysis:Disabled。 I clicked "Edit in settings.json", hoping to open the config file我点击了“在settings.json中编辑”,希望能打开配置文件

  4. I set the config file as follows:我将配置文件设置如下:

     { "window.zoomLevel": 3, "python.pythonPath": "/usr/bin/python3", "python.terminal.launchArgs": ["-i"] }
  5. I went back to my code and pressed F5 to run it.我回到我的代码并按 F5 运行它。 It ran on the terminal that comes with vscode.它在 vscode 自带的终端上运行。 The command line was as follows命令行如下

    lucas@mongolia:~/aulas_ed$ cd /home/lucas/aulas_ed ; env "PYTHONIOENCODING=UTF-8" "PYTHONUNBUFFERED=1" /usr/bin/python3 /home/lucas/.vscode/extensions/ms-python.python-2018.12.1/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 34661 /home/lucas/aulas_ed/teste.py
  6. As you can see, that did not include "-i" .如您所见,这不包括"-i"

  7. I tried altering other variables in settings.json , in the same window.我尝试在同一窗口中更改settings.json中的其他变量。 The text size works (text size changes immediately) and the python path variable works (if I type a nonsensical path, F5 stops working and VScode starts complaing about an undefined path -- just to say that the settings file seems to work properly for other ends.文本大小有效(文本大小立即更改)并且 python 路径变量有效(如果我输入一个无意义的路径,F5 停止工作并且 VScode 开始抱怨未定义的路径——只是说设置文件似乎可以正常工作其他结束。

Found a way... But for sure it's not THE way...找到了一种方法......但肯定不是这样......

.vscode/settings.json .vscode/settings.json

{
    "python.terminal.launchArgs": [ 
        "${workspaceFolder}/main.py",
        "--port 8010"
    ]
}

pythonfile (main.py) python文件(main.py)

import sys
port = int(sys.argv[-2].split(' ')[-1]) if int(sys.argv[-2].split(' ')[-1]) else 8001

# >>>  * Running on http://10.100.100.75:8010/ (Press CTRL+C to quit)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM