Currently when I debbuging moves to my_proyect and run
/home/my_user/my_company/my_proyect/my_folder/my_code.py
However, I getting the following error:
FileNotFoundError: [Errno 2] No such file or directory: 'data/my_data.csv'
I trying to change the cwd variable in launch.json inside .vscode as follows:
{
// 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",
"env": {
"ENVIROMENT_EXECUTION": "local",
"LOGGING_LEVEL": "info",
"ENVIROMENT_STAGE": "develop",
"ENVIROMENT_DEBUG": "yes"
},
"cwd": "${workspaceFolder}/my_folder"
}
]
}
The structure of the folders is:
my_proyect/my_folder/data/my_data.csv
my_proyect/my_folder/my_code.py
And inside the code I have the following lines:
import pandas as pd
my_data = pd.read_csv("data/my_data.csv")
I note that works perfect if I add my_folder to the path as follows:
import pandas as pd
my_data = pd.read_csv("my_folder/data/my_data.csv")
But I can't do that because when I deploy my code in AWS Lambda it works perfect. So, Any idea how to solve this issue?
VS Code takes the open folder as the workspace. I have tested according to your directory structure, and there is no problem.
I open the my_project folder with VS Code and VS Code uses it as a workspace.
Below is my 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",
"justMyCode": true,
"cwd": "${workspaceFolder}/my_folder",
}
]
}
So, please modify your cwd configuration according to your workspace.
UPDATE:
Open the folder using File --> Open Folder...
Here is your directory:
/home/my_user/my_company/my_proyect/my_folder/my_code.py
If you have my_company turned on then "cwd" is
"cwd": "${workspaceFolder}/my_proyect/my_folder/"
If you have my_user turned on then "cwd" is
"cwd": "${workspaceFolder}/my_user/my_proyect/my_folder/"
${workspaceFolder} is just a variable that represents the path to the folder you are currently opening as a workspace.
Of course you can also use absolute path directly as "cwd":
"cwd": "/home/my_user/my_company/my_proyect/my_folder/my_code.py"
I have a python project I need to debug
It comes with its own python installation, lets say in c:\project\python.exe and libraries, located in c:\project\libraries\..
It's launched with a cmd file C:\project\start.cmd
The cmd file executes another file called c:\projects\setenv.cmd which sets up paths to each library while the prompt is open
I'm trying to replicate this functionality in Visual Studio Code
I created a workspace and set it up so the following files are in the workspace .vscode folder:
settings.json
{
"python.pythonPath": "C:\\project\\python.exe"
}
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"preLaunchTask": "shellCommand",
"console": "integratedTerminal"
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "shellCommand",
"command": "C:\\project\\set_env.cmd ; pause",
"type": "shell"
}
]
}
When I open the main.py file which the start.cmd executes after setenv.cmd, and start debugging,
I can see that the task shellCommand is executed and the setenv.cmd runs, because of the pause statement.
The output I get is below:
C:\project>SET PYTHONPATH=C:\project\libraries\camera;C:\project\libraries\exewrappers
Press Enter to continue...:
However, the main.py file crashes on the first library import, which it cannot find
The library path is correct, but it seems that Visual Studio Code forgets the Path variable after setenv.cmd is executed
How can I make it so the env variables set up by setenv.cmd are still remembered when I'm debugging the main.py file?
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.
I am writing a small application with Visual Studio Code and Python. My application have these files:
Main.py
MyCustomClass.py
Basically, the Main.py is the entry point to the application. The other class is just logic to solve some problems.
While I developing the code, I test it, by running it, step by step. I run the application with F5 which is running the current file I'm editing. It can be the file MyCustomClass.py which doesn't have an entry point at all and I'm losing time to swap between files.
Is it possible to configure Visual Studio Code to run a specific file (Main.py) while running (F5)? No matter which file I am currently viewing.
You need to edit your launch.json file. A quick way to do it is to click the wrench icon on the run toolbar:
Then add the following to the launch.json file:
{
"name": "Python: main.py (Integrated Terminal)",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/main.py",
"console": "integratedTerminal"
},
Above is my configuration, and the "program" line is what does the magic.
The program setting in your launch configuration settings refers to the Python script that will be executed. By default, it is set to ${file} which refers to the file you are actively editing. Just set the value to the file you want to run.
{
"name": "Python",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"pythonPath": "${config:python.pythonPath}",
"program": "main.py", // Specify the full path to your file
"cwd": "${workspaceFolder}",
"env": {},
"envFile": "${workspaceFolder}/.env",
"debugOptions": [
"RedirectOutput"
]
},
I should mention that if you are using Windows you can use either forward slashes / or double back slashes \\ in the program path. Single back slashes won't work.
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.