python scripts debug in ROS with Visual Studio Code - python

In order to debug python scripts, I would like to run "source path_to/setup.bash" before python debugger begins. How should I to do so ? Thank you!

VSCode enables you to do this by setting a preLaunchTask in your launch.json
What you need is a task.json with a type set to shell. It looks something like this.
task.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "prerun",
"type": "shell",
"command": "source hello.sh" // your shell command here
}
]
}
Remember the label "prerun". Now in your launch.json, alt Launch configuration, set this label as preLaunchTask like
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 Experimental: Current File (Integrated Terminal)",
"type": "pythonExperimental",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"preLaunchTask": "prerun"
},
]
}
You can tailor your debug configuration for your needs. For the example above, it pre-launches hello.sh every time Python source file is the editor is in debug. Now switch to your Python code and continue debugging. Hope this helps.

Related

Debugger does not start in VSCode for Django

I'm trying to debug my Django project, but whenever I hit the play button on the run and debug window in VSCode, it thinks for a second, and then completely stops. My project runs completely fine when I run it with the "runserver" command.
I've tried restarting VSCode, and restarting my computer.
here is my launch.json file
{
// 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: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}\\manage.py",
"args": [
"runserver",
"9000"
],
"django": true
}
]
}
Maybe this help :
Ctrl + Shift + P
Python: Select interpreter
Choose the right one

Changing the python bin for a django debug session in vs code

I have this in my launch.json
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: Django",
"type": "python",
"request": "launch",
"python": "${command:python.interpreterPath}",
"program": "${workspaceFolder}/manage.py",
"args": [
"runserver"
],
"django": true,
"justMyCode": true
}
]
}
I want to change the bin used so that it doesn't use the normal bin of ubuntu but a virtual env (from pipenv shell).
does anyone now any configurations that i have to change to get this?
This configuration in the launch.json file can be used to specify the python interpreter path. "python": "${command:python.interpreterPath}".
For more configuration information, you can refer to this.

I got a wrong directory using os.path.abspath('.') [duplicate]

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}" }
]
}

How to make VSCode always run main.py

I am writing my first library in Python, When developing I want my run code button in VS Code to always start running the code from the main.py file in the root directory. I have added a new configuration to launch.json however I seem to be unable to use this configuration as default. How can I do this/
You need to put the 'launch.json' under the '.vscode' folder inside your workspace. Then Run > Run Without Debugging (shortcut on windows CTRL+F5)
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": "${workspaceFolder}/main.py",
"console": "integratedTerminal"
}
]
}
You can modify the launch.json with the below settings for key program. You may want to point program to the file which you want to execute. In the below case main.py is present in my workspace folder only. You can modify it as per your requirement.
{
// 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": "${workspaceFolder}/main.py",
"console": "integratedTerminal"
}
]
}
I found the right solution is to just change "program" in launch.json to:
"program": "main.py",
If trying to add the {workspaceFolder} it gives a FileNotFoundError.

How to develop (run and debug) modules in Odoo v11 on Visual Studio Code in Ubuntu?

Is it possible to run and debug Odoo on Visual Studio Code? If yes please share me the configuration.
Visual Studio Code is a source code editor developed by Microsoft for Windows, Linux and macOS. It includes support for debugging, embedded Git control, syntax highlighting, intelligent code completion, snippets, and code refactoring. It is free and open-source, although the official download is under a proprietary license.
I know I'm a bit late but I have managed to work with Odoo 11.
My installation path is "C:\Program Files (x86)\Odoo 11.0\server"
Now open the vs code and goto Workspace settings and paste this:
{
"python.pythonPath": "C:\\Program Files (x86)\\Odoo 11.0\\python\\python.exe",
"python.linting.pylintEnabled": false,
// use this so the autocompleate/goto definition will work with python extension
"python.autoComplete.extraPaths": [
"${workspaceRoot}/odoo/addons",
"${workspaceRoot}/odoo",
"${workspaceRoot}/odoo/openerp/addons"
],
//"python.linting.pylintPath": "optional: path to python use if you have environment path",
"python.linting.enabled": false,
//load the pylint_odoo
"python.linting.pylintArgs": [
"--load-plugins",
"pylint_odoo"
],
"python.formatting.provider": "yapf",
//"python.formatting.yapfPath": "optional: path to python use if you have environment path",
// "python.linting.pep8Path": "optional: path to python use if you have environment path",
"python.linting.pep8Enabled": true,
// add this auto-save option so the pylint will sow errors while editing otherwise
//it will only show the errors on file save
"files.autoSave": "afterDelay",
"files.autoSaveDelay": 500,
// The following will hide the compiled file in the editor/ add other file to hide them from editor
"files.exclude": {
"**/*.pyc": true
}
}
save it and open the code folder in vs "C:\Program Files (x86)\Odoo 11.0\server\odoo"
then goto debugging setting and a new configuration file and paste below code:
{
// 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: Odoo",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"pythonPath": "${config:python.pythonPath}",
"console": "externalTerminal",
"program": "${workspaceRoot}\\..\\odoo-bin",
"args": [
"--config=${workspaceRoot}\\..\\odoo.conf",
],
"cwd": "${workspaceRoot}",
"env": {},
"envFile": "${workspaceRoot}/.env",
"debugOptions": [
"RedirectOutput"
]
}
]
}
and just hit the run button. remember vs code might give you some warning press ignore button and wait for the console to open and you are done. enjoy debugging and coding.
Don't forget to stop the Odoo service from window services.
Yes, you can even debug Odoo with VSCode:
First, you need to install the Python Extension within VSCode.
Add the folder where Odoo is installed to the current project. You can use the functionality Multiroot Workspaces. I think it is convenient in this case: open your project modules in one folder, and Odoo in other folder
Then, if you want to start debugging you just need to click on the Debug button and click on the wheel on the top of the sidebar. The file launch.json will open and you just need to add this element to the bottom.
{
"name": "Python: Odoo",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"pythonPath": "${config:python.pythonPath}",
"console": "externalTerminal",
"program": "${workspaceRoot}/odoo_path/odoo.py",
"args": [
"--config=/odoo_config_path/.odoo_8.conf",
],
"cwd": "${workspaceRoot}",
"env": {},
"envFile": "${workspaceRoot}/.env",
"debugOptions": [
"RedirectOutput"
]
}
Once it is added you already can run Odoo under VSCode. For more information about the launch configurations click here
Now you can create breakpoint as usual. You can use the debugger console as well. And if you use the property: "console": "externalTerminal" as I did, you can show the log in an external console at the same time
Note: If you only want to run odoo you can use the integrated console in VSCode
Note 2: I recommend to install the Odoo Snippets extension as well.
I want to add updated answer and so I am sharing simple launch.json script I am using. This script assume odoo is in project folder.
{
// 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": "Odoo",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/odoo-bin",
"console": "integratedTerminal",
"args": [
"--addons-path",
"addons,mymodules",
],
}
]
}
vscode version: 1.39.2 (September 2019)
odoo version: 11
about launch.json: https://code.visualstudio.com/docs/editor/debugging#_launch-configurations
My settings - Odoo 12, Visual Studio Code 1.40.0, Linux
Building on Adeel Rizvi's post - thanks a lot! Updated for Linux and for newer VSCode.
I spent better part of the day to make it work. I use a multi-instance setup for Odoo, which didn't help at all.
For debugging, I needed to sort out the permissions hick-ups, so I decided to run the debugging instance as a current user. In the next commands, always replace myuser by your actual user name. Using my odoo70 user was a no-go, as it couldn't access the vscode stuff in my home folder, including the debugger.
I copied the Odoo stuff to /opt/odoo70l, did sudo chown -R myuser:users /opt/odoo70l and created a new postgres user sudo su - postgres -c "createuser --createdb --username postgres --no-createrole --no-superuser --no-password myuser" to keep being able to use peer authentication. That also required changing the database user to myuser in the config file - for me /opt/odoo70l/odoo70l.conf. (Note that it will make the previous databases inaccessible for the debugging instance - either change their owner, or export and import under the new user.) I also commented out the logfile there so it would print logs directly to the terminal.
I have my workspace in /opt/odoo-dev/ and its subfolders contain projects with modules I work on. I use multi-root workspace to be able to also peek into the base Odoo code.
My workspace options (/opt/odoo-dev/workspace-name.code-workspace):
(Each of my Odoo instances have their own python venv - if you don't use that, remember to edit python.pythonPath to /usr/bin/python3 or wherever your which python3 points to.)
{
"folders": [
{
"path": "."
},
{
"path": "/opt/odoo70l"
},
],
"settings": {
"python.pythonPath": "/opt/odoo70l/odoo-venv/bin/python3",
"python.linting.pylintEnabled": false,
// use this so the autocompleate/goto definition will work with python extension
"python.autoComplete.extraPaths": [
"/opt/odoo70l/odoo/addons",
"/opt/odoo70l/odoo",
"/opt/odoo70l/odoo/odoo/addons"
],
//"python.linting.pylintPath": "optional: path to python use if you have environment path",
"python.linting.enabled": false,
//load the pylint_odoo
"python.linting.pylintArgs": [
"--load-plugins",
"pylint_odoo"
],
"python.formatting.provider": "yapf",
//"python.formatting.yapfPath": "optional: path to python use if you have environment path",
// "python.linting.pep8Path": "optional: path to python use if you have environment path",
"python.linting.pep8Enabled": true,
// add this auto-save option so the pylint will sow errors while editing otherwise
//it will only show the errors on file save
"files.autoSave": "afterDelay",
"files.autoSaveDelay": 500,
// The following will hide the compiled file in the editor/ add other file to hide them from editor
"files.exclude": {
"**/*.pyc": true
}
},
}
My /opt/odoo-dev/.vscode/launch.json (only the first block is important, rest is left there from default VSCode configuration in case you want to do other Python debugging as well):
{
// 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: Odoo 12 myuser",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"pythonPath": "${config:python.pythonPath}",
"console": "externalTerminal",
"program": "/opt/odoo70l/odoo/odoo-bin",
"args": [
"--config=/opt/odoo70l/odoo70l.conf",
],
"cwd": "${workspaceRoot}",
"env": {},
"envFile": "${workspaceRoot}/.env",
},
{
"name": "Python: Current File (Integrated Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
},
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"port": 5678,
"host": "localhost",
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "."
}
]
},
{
"name": "Python: Module",
"type": "python",
"request": "launch",
"module": "enter-your-module-name-here",
"console": "integratedTerminal"
},
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"console": "integratedTerminal",
"args": [
"runserver",
"--noreload",
"--nothreading"
],
"django": true
},
{
"name": "Python: Flask",
"type": "python",
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "app.py"
},
"args": [
"run",
"--no-debugger",
"--no-reload"
],
"jinja": true
},
{
"name": "Python: Current File (External Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "externalTerminal"
}
]
}
Let's say possible,
Odoo official development done with ubuntu operating system.
For odoo 11.0 Most suggest subline or gedit with plugins most great tool for development.
Visula studio in ubuntu
https://www.youtube.com/watch?v=aY4Rhmv5P_Q
We had build lot odoo code since 2011 till date on gedit with ubuntu. Also atom https://atom.io/ is good tool for edting contents.
My special concern with gedit, it take very less memory and give speed.
In case you had large RAM, yes. this visual studio works great.

Categories

Resources