Visual Studio代码中的Python项目设置

时间:2017-12-18 18:38:03

标签: python visual-studio-code

我在visual studio代码中设置了python项目。下面是代码的一部分是什么样的

...
from .pgcompleter import PGCompleter
from .pgtoolbar import create_toolbar_tokens_func
from .pgstyle import style_factory
from .pgexecute import PGExecute
from .pgbuffer import PGBuffer
from .completion_refresher import CompletionRefresher
from .config import (get_casing_file,
    load_config, config_location, ensure_dir_exists, get_config)
from .key_bindings import pgcli_bindings
from .encodingutils import utf8tounicode
from .encodingutils import text_type
...

pgcompleter等文件仅来自我的项目。

而且,这是我的launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python",
            "type": "python",
            "request": "launch",
            "stopOnEntry": true,
            "pythonPath": "${config:python.pythonPath}",
            "program": "${file}",
            "cwd": "${workspaceFolder}",
            "env": {},
            "envFile": "${workspaceFolder}/.env",
            "debugOptions": [
                "RedirectOutput"
            ]
        }
}

当我尝试调试时,我在控制台上出现错误

Traceback (most recent call last):
  File "<absolute_path_to>main.py", line 43, in <module>
    from .pgcompleter import PGCompleter
ValueError: Attempted relative import in non-package

好的,所以我知道两个解决方法

- python -m some_folder_name.main.py

- 并且,更改导入,如

from pgcompleter import PGCompleter
from pgtoolbar import create_toolbar_tokens_func
from pgstyle import style_factory

我只是想知道可以在不改变导入的情况下使其工作吗?

2 个答案:

答案 0 :(得分:0)

要为包创建入口点,请使用名为__main__.py的文件。因此,将main.py更改为__main__.py,然后运行python -m some_folder_name

答案 1 :(得分:0)

您需要在与var cartResource = $resource('http://localhost:8080/Cart/:cartId', {cartId: '@id'}); $scope.cart1 = cartResource.get({cartId: 1},function(todo) { // success console.log("k","k") }, function(errResponse) { console.log("nooooo",errResponse) }); 等文件相同的目录中拥有#if targetEnvironment(simulator) testSimulatorFlag.text = "Compiler thinks this is a simulator" #else testSimulatorFlag.text = "Compiler thinks this is a device" #endif 个文件。这会将它从常规目录转换为Python包。