如何在Ubuntu上使用Visual Studio Code开发(运行和调试)Odoo v11模块?

5
有没有可能在 Visual Studio Code 上运行和调试 Odoo?如果可以,请分享配置方法。

Visual Studio Code 是 Microsoft 为 Windows、Linux 和 macOS 开发的源代码编辑器。它支持调试、内嵌 Git 控制、语法高亮、智能代码补全、代码片段和重构等功能。虽然官方下载需要专有许可证,但它是免费且开源的。


你试过 PyCharm 吗?这是一个非常适合 Python 编程的优秀 IDE。 - Charif DZ
1
是的,目前我正在使用PyCharm,但我已经厌倦了试用版。 - Binyam Assefa
5个回答

6

我知道我有点晚了,但我已经成功地使用Odoo 11进行了工作。

我的安装路径是"C:\Program Files (x86)\Odoo 11.0\server"

现在打开vs code并转到Workspace设置,并粘贴此内容:

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

保存并打开vs中的代码文件夹"C:\Program Files (x86)\Odoo 11.0\server\odoo"

然后进入调试设置,创建一个新的配置文件,并粘贴以下代码:

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

只需点击运行按钮即可。请记住,VS Code可能会给您一些警告,请按忽略按钮并等待控制台打开,然后就完成了。享受调试和编码的乐趣。

不要忘记从Windows服务中停止Odoo服务。


3

是的,你甚至可以使用VSCode调试Odoo:

首先,你需要在VSCode中安装Python扩展

将Odoo安装文件夹添加到当前项目中。你可以使用多根工作区功能。我认为在这种情况下很方便:在一个文件夹中打开你的项目模块,在另一个文件夹中打开Odoo。

然后,如果你想要开始调试,只需点击调试按钮并点击侧边栏顶部的轮子。文件launch.json将会打开,你只需要在底部添加此元素。

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

一旦添加完成,您就可以在VSCode中运行Odoo了。有关启动配置的更多信息请点击此处
现在您可以像往常一样创建断点。您也可以使用调试器控制台。如果您像我一样使用属性:"console": "externalTerminal",则可以同时在外部控制台中显示日志。

enter image description here

注意:如果您只想运行Odoo,可以使用VSCode中的集成控制台。

注意2:我建议您也安装Odoo Snippets扩展程序


2
你在配置文件中是否已经设置了正确的插件路径?请记得在11版本中使用odoo-bin文件而不是odoo.py。无论如何,我已经尝试过8版本了,如果有时间,我以后会再尝试11版本。 - ChesuCR
我已经尝试了版本10和11,它可以工作!@BinyamAssefa你是否使用正确的Python版本?确保你正在使用Python3来运行Odoo 11。你可以进入odoo-bin文件并更改头部以强制使用你想要的Python版本 #!/usr/bin/python3 - ChesuCR
我按照你说的做了,但还是没有成功。顺便说一下,我正在使用Lubuntu操作系统。 - Binyam Assefa
你在VSCode中使用正确的Python版本了吗?看一下我的截图,状态栏上应该显示Python 3版本。你可以通过点击它来更改版本。你在哪里收到“module odoo not found”消息?是在外部终端吗? - ChesuCR
什么是正确的Python版本?我正在使用Python 3.6(64位)。 - Binyam Assefa
显示剩余7条评论

0

我想要添加更新的答案,因此我正在分享我使用的简单launch.json脚本。该脚本假定odoo位于项目文件夹中。

{
    // 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 版本:1.39.2(2019 年 9 月)

odoo 版本:11

关于 launch.jsonhttps://code.visualstudio.com/docs/editor/debugging#_launch-configurations


0

我的设置 - Odoo 12,Visual Studio Code 1.40.0,Linux

在Adeel Rizvi的帖子基础上进行了改进 - 非常感谢!更新了Linux和更新版本的VSCode。

我花了一整天的时间使其正常工作。 我使用Odoo的多实例设置,这一点也没有帮助。

为了调试,我需要解决权限问题,因此决定将调试实例作为当前用户运行。在下一个命令中,始终使用您的实际用户名替换myuser。 使用我的odoo70用户是不可行的,因为它无法访问我的主文件夹中的vscode内容,包括调试器。

我将Odoo的内容复制到了/opt/odoo70l,执行了sudo chown -R myuser:users /opt/odoo70l命令,并创建了一个新的postgres用户sudo su - postgres -c "createuser --createdb --username postgres --no-createrole --no-superuser --no-password myuser"以保持能够使用对等身份验证。这还需要在配置文件中将数据库用户更改为myuser,对于我来说是/opt/odoo70l/odoo70l.conf。(请注意,这将使以前的数据库对于调试实例不可访问 - 要么更改它们的所有者,要么在新用户下导出和导入。)我还在那里注释掉了日志文件,以便直接将日志打印到终端。

我的工作区在/opt/odoo-dev/中,其子文件夹包含我正在开发的模块项目。我使用多根工作区,以便也可以查看基本的Odoo代码。

我的工作区选项(/opt/odoo-dev/workspace-name.code-workspace):

我的每个Odoo实例都有自己的Python虚拟环境 - 如果您不使用它,请记得编辑python.pythonPath/usr/bin/python3或任何您的which python3指向的位置。

{
    "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
        }
    },
}

我的/opt/odoo-dev/.vscode/launch.json文件(只有第一个块是重要的,其余部分是默认VSCode配置留下来的,以防您还想进行其他Python调试):

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

-2

假设可能的话,

Odoo官方开发是在Ubuntu操作系统上完成的。

对于Odoo 11.0,大多数人建议使用Sublime或带插件的Gedit进行开发,这是最好的工具。

在Ubuntu中使用Visual Studio https://www.youtube.com/watch?v=aY4Rhmv5P_Q

自2011年以来,我们已经在Ubuntu上使用gedit构建了很多Odoo代码。此外,Atomhttps://atom.io/也是编辑内容的好工具。

我特别关注gedit,它占用的内存很少,速度很快。如果您有大量RAM,那么Visual Studio也可以很好地工作。

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接