I am following the code mentioned in debugging a python file in vs code with input text file. To get my error:
- Create a new virtual pip virtual environment.
- Create a new folder called
project
. - Inside
project
, create simple "sort two numbers" python filehello.py
:
line = input()
a,b = line.split()
a = int(a)
b = int(b)
if a <= b:
print(a)
print(b)
else:
print(b)
print(a)
(If possible, I prefer answers that do not modify this .py file because I am actually working on problems like the ones in kattis. The code passes the test of https://open.kattis.com/problems/sorttwonumbers .) As my goal is to redirect input from a text file, I create the following input.txt
also in project
:
987 23
(This input.txt is legitimate: use Pycharm to redirect input for example will make the hello.py
run correctly.) By setting the python interpreter to be the virtual environment, we can see that hello.py
runs in the virtual environment if the user gives numbers in the command line because green environment name shows up in the command line.
- Create folder
.vscode
inproject
, in which we createlaunch.json
:
{
"version": "0.2.0",
"configurations": [
{
"name": "PYTHON REDIRECT INPUT",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/hello",
"console": "externalTerminal",
"args": [
"<",
"${workspaceFolder}/input.txt",
]
}
]
}
(In the program part, hello.py
or hello
yields exactly the same result. )
- Click on debug button on the left side of the screen, we indeed see that "PYTHON REDIRECT INPUT" shows up as an option. Click on debug, however, opens the following window:
Where the buttons read "open launch.json" and "cancel". The debugger does not read my text files. The same format of "args" entry will work when I am debugging C++ but it just is not working for python.
from Visual Studio Code Debug Python Redirecting Input from Text File in Virtual Environment
No comments:
Post a Comment