0

I'd like to run a Python file in VSC but the file requires external data from two different .txt files.

As an example, if I run it in the Python IDLE, I'd open the Python file, select Run...Customized, and enter the path to the .txt file, (e.g. data/xxxxx.txt data/yyyy.txt output.png) and then run it.

Could you please indicate if there is a way to run a file in that way in VSC, or refer me to where I can find any supportive documentation that would cover this special requirement?

This is the function where the exception occurs:

def main():

    # Check usage
    if len(sys.argv) not in [3, 4]:
        sys.exit("Usage: python generate.py structure words [output]")

    # Parse command-line arguments
    structure = sys.argv[1]
    words = sys.argv[2]
    output = sys.argv[3] if len(sys.argv) == 4 else None

    # Generate crossword
    crossword = Crossword(structure, words)
    creator = CrosswordCreator(crossword)
    assignment = creator.solve()

    # Print result
    if assignment is None:
        print("No solution.")
    else:
        creator.print(assignment)
        if output:
            creator.save(assignment, output)

And this is the exception in VSC:

Exception has occurred: SystemExit

Usage: python myfile.py structure words [output]

  File "/Users/#######/myfile.py", line 277, in main
    sys.exit("Usage: python myfile.py structure words [output]")   
 File "/Users//#######/myfile.py", line 299, in <module>
   main()
4
  • 1
    Are you looking for how to create a launch configuration with VS code? Commented Jul 7, 2020 at 9:18
  • In the launch.json set the cwd to the location you want Commented Jul 7, 2020 at 10:01
  • To M. Spiller, thanks but I'm not sure a launch configuration is the answer, but I'll have a better look at that in the documentation. Thanks Commented Jul 7, 2020 at 11:33
  • To rioV8. Thanks for your suggestion. I'll need to have a better look at how the launch.json works, but at the moment I couldn't find the way to make it work as I wanted. Commented Jul 7, 2020 at 11:42

2 Answers 2

2

It seems like you want to run your python file with the VS Code Debugger, but can't tell it what arguments to launch your program with.

Navigate to the debugger tab (Ctrl / Cmd + Shift + D) and click on "Create a launch.json file", specifying in an appearing prompt that you want to add launch configurations to a python file.

Screenshot to create a launch.json file

It should create and open a boilerplate json file. Inside the first element of "configurations" you then can add a key "args" with all the necessary arguments inside an array.

In full, the json file may look something like this:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",

            // this is what I added manually
            "args": [
                "a.txt",
                "b.txt",
                "out.png"
            ]
        }
    ]
}

Then, when you have your python file open, launch it either through the debug tab or with the green arrow in the top right corner.

EDIT:

With frequently changing command line arguments, you can tell VS Code to prompt you for those. See the following json. If there's something you don't understand, let me know.

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "args": [
                "${input:firstArg}",
                "${input:secondArg}",
                "out.png"
            ]
        }
    ],

    "inputs": [
        {
            "id": "firstArg",
            "type": "promptString",
            "default": "myDefault.txt",
            "description": "First txt"
        },
        {
            "id": "secondArg",
            "type": "promptString",
            "description": "Now your second txt"
        }
    ]
}
Sign up to request clarification or add additional context in comments.

6 Comments

Many thanks for the complete and clear explanation. I think you put me on the right path now. I'm at work, but I'll try your solution later and get back to you with the results. Thanks
It worked beautifully! Many thanks. I had the launch.json file created but without the args. Once I added the args I could run it w/o problems. Now there is the minor complication that I have to change the args every time I need to run the Python file with other arguments, but I can live with that. Thank you
You can tell VS Code to prompt you for command line arguments. See my edit :)
That's great. I think I've got the idea, so I'll give it a try with different files. Now, it's all good to come here and find a solution thanks to people like you, but may I ask you how did you learn all these details of VSC? I've been though the documentation and videos and they are not clear enough, at least for me. Thanks
You googled it just to help me with my question? Wow! The world is not in such a bad shape as I believed. Many thanks for your support. I'll continue investigating the VSC docs.
|
0

You need to install python seperately for VSC as it is an extension.

After installing :

python filename.py

For more details: VS code documentation

1 Comment

I already have Python installed in my computer Akshat, as well as the Python extension in VSC. Can you clarify your instruction after installing? It looks like running Python in the terminal. Thanks

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.