I use manjaro and want to use vscode tool for debug my python program,but i use CUDA, so i usually use a command like this:
optirun ipython program.py arg1 arg2
when i try to debug the program optirun isn't called, so i don't have access to GPU, how can i call optirun before python (or ipython)?
I've tried to change settins.json and launch.json to add it to command but it doesn't work.
Thanks.
This is what I did and it worked.
Make a bash script, name it anything, assume script.sh.
#!/bin/bash
optirun python "$#"
In .vscode/launch.json set "pythonPath" to ./script.sh.
Here is a example launch.json
{
// 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",
"linux": {
"pythonPath": "./script.sh"
},
}
]
}
Now it should work as intended.
I also faced the same problem while debugging a OpenCL application in python. My Nvidia graphics card was managed by optirun. $# passes all the command line arguments supplied by vscode to the python interpreter.
You could try editing your launch.json to point to optirun as your Python interpreter via the "python.pythonPath" setting.
Related
I'm writing a python unit test using unittest and I want to compile some Solidity contracts in the test setup.
However, when I did the compile by os.system() or subprocess.run(), it shows that solc, the Solidity compiler, not found. While it runs properly when running in a non-test python program.
After this happens, I found a further interesting things: when I print(os.environ) in both a test python program using unittest and a normal python program, the result is of hugh difference! Including the most important one: $PATH. It looks like the following:
****PATH in unittest****
/usr/bin
/usr/local/bin
...(mostly the default $PATH set by Linux)
****PATH in normal program****
/usr/bin
/.../myEnvs/.../bin
...(as same as my console's $PATH, which is exported in .bashrc)
Since I'm working with others to develop this program, I should NOT add path such as "myEnvs" in program (by using environment setting like env= parameter provided by subprocess.Popen()).
I think the abnormal $PATH in python.unittest is caused by some configurations introduced by VScode, so what are these configs? How could I modify them? or this "PATH inconsistence" is caused by some other reasons?
Okey..... After a period of time, I finally find out what happens:
Exactly, The so-called "$PATH difference" is caused by VScode configurations in launch.json.
You can create a new term like this and make your unittest configuration same as a normal debug:
{
...
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": false,
"args": []
},
{
"name": "Python: Debug Tests",
"type": "python",
"request": "launch",
"program": "${file}",
"purpose": ["debug-test"],
"console": "integratedTerminal", // Consistence with ```Python: CurrentFile```
"justMyCode": false,
},
]
}
This Debug Test configuration can be used in different test mode, see more in VSCode doc - Debug tests.
The critial one is "console", the default value is "internalConsole", which seems to share the same env with system defaults. While I should set it to "integratedTerminal", which is consistent with my own shell.
When I execute a simple print('Hello world') by using "Run Python File" in VScode I get this output
barakiva#pop-os:~/Development/Educational/Languages/Python/lang$ /usr/bin/python3
/home/barakiva/Development/Educational/Languages/Python/lang/vars.py Hello world
However, opening VScode's integrated console and typing python3 vars.py outputs normally with no clutter
barakiva#pop-os:~/Development/Educational/Languages/Python/lang$ python3 vars.py
Hello world
This happens because the code is run through the Terminal.
In short, under the hood, VSCode opens a new terminal and executes the command to run your program using the full path. It's done this way to avoid possible conflicts, if you choose to e.g. use an external terminal with a custom configuration.
VSCode can also display the program output only, via the Debug Console (tab to the left from Terminal). To do this, you need to create a custom launch configuration.
In the Run and Debug tab click create a launch.json file (if you don't have one already) and select the Python File template. Then, in .vscode/launch.json change property "console": "integratedTerminal", to "console": "internalConsole" for the desired configuration. See the complete config below:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "internalConsole",
"justMyCode": true
}
]
}
With this in place, you can run your Python file like before, and see the program output in the Debug Console:
I set up a python virtual environment named "myenv" in my project. Then, I try to debug the script from VS code.
Here is the output. Please give some insights. Thanks
$ cd d:\\xx\\yy ; env PYTHONIOENCODING=UTF-8 PYTHONUNBUFFERED=1 d:\\xx\\yy\\myenv\\Scripts\\python.exe c:\\Users\\xyz\\.v
scode\\extensions\\ms-python.python-2019.6.24221\\pythonFiles\\ptvsd_launcher.py --default --client --host localhost --port 1817 d:\\xx\
\yy\\.vscode\\launch.json
bash: env: command not found
(myenv)
My launch json looks like below.
{
// 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"
}
]
}
If you install and use Python Extension from Microsoft then you can just start VS Code from your project root folder: code . Then open some Python source file and in the lower left corner at the bottom status bar of VS Code you will see your current Python execution environment. You can click and change it by selecting your environment from the list.
I think the problem is you are running under Windows but you set your shell to bash. If you're using WSL then please see the instructions on remote development which includes WSL. If you're using git-bash then please note the Python extension does not support git-bash as a shell.
I am trying to debug an IronPython script (my company requires IronPython for some tasks) using Visual Studio Code. The version of IronPython has to be executed using the -X:Frames option.
Here's how I would run the script from the command line:
ipy -X:Frames my_script.py
Adding -X:Frames to the "args" launcher property makes it an argument for the script instead of the interpreter which is incorrect:
ipy my_script.py -X:Frames
Is there a way to pass arguments to the interpreter itself?
If it helps, here's my launch configuration:
"launch": {
"configurations": [
{
"name": "My Tests",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"pythonPath": "ipy",
"program": "C:\\temp\\my_script.py",
"args": ["-X:Frames"],
"cwd": "C:\\temp",
"env": {},
"envFile": "",
"debugOptions": [
"RedirectOutput"
]
}
]
},
Currently there is no way to pass explicit arguments to the interpreter and not the script itself. See this feature request and please leave a +1 reaction to vote for your desire to see it implemented.
How do I run a Python program under debug and set the working directory for the run?
#SpeedCoder5's comment deserves to be an answer.
In launch.json, specify a dynamic working directory (i.e. the directory where the currently-open Python file is located) using:
"cwd": "${fileDirname}"
This takes advantage of the "variables reference" feature in VS Code, and the predefined variable fileDirname.
If you're using the Python: Current File (Integrated Terminal) option when you run Python, your launch.json file might look like mine, below (more info on launch.json files here).
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File (Integrated Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${fileDirname}"
},
//... other settings, but I modified the "Current File" setting above ...
}
Remember the launch.json file controls the run/debug settings of your Visual Studio code project; my launch.json file was auto-generated by VS Code, in the directory of my current "Open Project". I just edited the file manually to add "cwd": "${fileDirname}" as shown above.
Remember the launch.json file may be specific to your project, or specific to your directory, so confirm you're editing the correct launch.json (see comment)
If you don't have a launch.json file, try this:
To create a launch.json file, open your project folder in VS Code (File > Open Folder) and then select the Configure gear icon on the Debug view top bar.
Per #kbro's comment, you might be prompted to create a launch.json file by clicking the Debug button itself:
When I clicked on the Debug button on my navigation panel it said "To customise Run and Debug create a launch.json file." Clicking on "create..." opened a dialog asking what language I was debugging. In my case I selected Python
Configure the cwd setting in launch.json as follows:
{
"name": "Python",
"type": "python",
"pythonPath": "python",
...
"cwd": "<Path to the directory>"
...
}
This setting helps me: (I am a Windows person)
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"cwd": "${workspaceFolder}\\app\\js", // set directory here
"program": "${workspaceFolder}\\app\\js\\server.js", // set start js here
}
In some cases, it might be also useful to set the PYTHONPATH along with the workspaceFolder:
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${workspaceFolder}",
"env": {
"PYTHONPATH": "${cwd}"
}
}
I am posting this sample configuration for people who use TypeScript on Node.js
in my project my Node.js server TypeScript files are located in folder Application_ts
and the compiled js files are generated in the folder named Application
because when we run our application in debug mode or start it normally we should start from Application folder which contains the js files
so bellow configuration run debug from root folder where my application_ts also exists and works perfect
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug TypeScript in Node.js",
"program": "${workspaceRoot}\\Application\\app.js",
"cwd": "${workspaceRoot}\\Application",
"protocol": "inspector",
"outFiles": [],
"sourceMaps": true
},
{
"type": "node",
"request": "attach",
"name": "Attach to Process",
"port": 5858,
"outFiles": [],
"sourceMaps": true
}
]
}
You can set up current working directory for debugged program using cwd argument in launch.json
To set current working directory to whatever file you are executing at the time:
File > Preferences > Settings > Python > Data Science > Execute in File Dir
Thanks brch: Python in VSCode: Set working directory to python file's path everytime
I faced the same issue and noticed that when running the which python command in Terminal in Mac it shows me a different path to what I get when I run the which python command in vs code. And also that my file runs properly in the terminal when run using python filename.py
So I copied that path from the terminal and pasted it in VS code into Preferences->Settings->Extensions->Python->Default Interpreter Path and it worked. I hope this helps.
I use the "justMyCode = false" so I can also debug and jump into the functions that the main script calls.
{
// 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",
"justMyCode": false,
"cwd": "${fileDirname}" }
]
}