1

I know how to add arguments for the Python script I want to run. For example, if test.py is my script file and it has one argument like '--batch_size' then I can edit launch.json in VS Code and set "args": ["--batch_size", "32"]

But I don't know how to add environmental arguments for Python itself. For example, Python has -m environmental variable, which runs library module as a script. If I want to run python -m torch.distributed.launch test.py --batch_size 32, what should I edit in VS Code to run the debugger?

UPDATE: Here is my launch.json

enter image description here

2
  • 1
    -m is not environmental variable, it's just an argument too Commented Nov 15, 2019 at 5:56
  • I guess you are right, it is an argument for python itself but not for test.py. My problem is I dont know how to pass argument for python itself in VS code. Commented Nov 15, 2019 at 6:00

3 Answers 3

5

-m is not environmental variable. It's just a regular argument.

To run python -m torch.distributed.launch test.py --batch_size 32 use args "args": ["-m", "torch.distributes.launch" ,"--batch_size", "32"] Also you need to run python itself instead of running script to pass these args to it ("program": "python3").

To set actual environment variables use "env": {"ENV_VAR_NAME1": "value of ENV_VAR_NAME1", "ENVVAR2": "Value for ENVVAR2"}

Here you can find more information about launch.json configuration

Sign up to request clarification or add additional context in comments.

8 Comments

It says "File does not exist: "python3" "
Do you use python 2? How do you launch your script when it works? Best way to answer is to attach your launch.json to your question
I have attached launch.json, please take a look
Future advice: it's not recommended to attach screenshots of code About your launch.json I guess it's correct, but most probably you can't launch python executable because it's not on your PATH variable in OS (I guess you're using windows) You need to add path to your python.exe to PATH variable or just use full path to it in "program" attribute
I did that. Got "ValueError: source code string cannot contain null bytes" error
|
0

So if anyone else is still stuck in the problem, I got it to work by replacing "program" with "module" and setting "module" parameter to "torch.distributed.launch" in the launch.json file. The "args" will then be set to ["--nproc_per_node", "2", "${file}"]. Here I have 2 processes.

Comments

-3

You can just set the environment variables of your machine using:

export ENV_VAR_NAME='value'

or set the variable before executing the command.

ENV_VAR_NAME='value' test.py

3 Comments

I guess it's not going to be right answer at all. Turns out that problem is with running python with additional arguments. Anyway, question about envvars was about how to configure VS code to launch code with envvars, not how to launch it with envvars from shell
Python debugger does not use current environment variable you set in terminal
No, exporting variables in bash does not pass them to python in VS Code (code-server) terminal. You need to switch to Jupyter Notebook or Lab if you want such simplicity:)

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.