Visual Studio Code“没有已知父包文件的相对导入”

Vin*_*yen 4 visual-studio-code

我在调试main .py时收到导入错误。我的 IDE Visual Studio Code 似乎在识别我的 TREE 结构(如下所示)时遇到问题,因为以下内容在 VSC 之外工作:

  1. 在虚拟环境中运行:pipenv run python3.7 -m pset_1
  2. 虚拟环境调试:pipenv run python3.7 -m pdb pset_1

    发生异常:ImportError 尝试在没有已知父包的情况下相对导入文件“/home/hoang/Documents/E29/pset1/2019sp-pset-1-nhvinh118/pset_1/ main .py”,第 4 行,来自 .hash_str import get_csci_salt , get_user_id, hash_str 文件“/usr/local/lib/python3.7/runpy.py”,第 85 行,在 _run_code exec(code, run_globals) 文件“/usr/local/lib/python3.7/runpy.py” ,第 96 行,在 _run_module_code mod_name, mod_spec, pkg_name, script_name) 文件“/usr/local/lib/python3.7/runpy.py”,第 263 行,在 run_path pkg_name=pkg_name, script_name=fname)

__main__.py 中的 IMPORT 语句(要调试的模块)

from .hash_str import get_csci_salt, get_user_id, hash_str
from .load_data import load_vectors, load_words, load_data
Run Code Online (Sandbox Code Playgroud)

.
|-- Dockerfile
|-- Pipfile
|-- Pipfile.lock
|-- README.md
|-- __pycache__
|   `-- tokenize.cpython-37.pyc
|-- data
|   |-- hashed.parquet
|   `-- hashed.xlsx
|-- docker-compose.yml
|-- drun_app
|-- pipenvgraph.log
|-- pset_1
|   |-- WordEmbedding.py
|   |-- __init__.py
|   |-- __main__.py
|   |-- hash_str.py
|   |-- io.py
|   |-- load_data.py
|   `-- tokenize.py
|-- setup.cfg
`-- tests.py
Run Code Online (Sandbox Code Playgroud)

我的两个settings.json

(1)/home/hoang/.config/Code/User/settings.json

{
    "python.pythonPath": "/home/hoang/anaconda3/bin/python",
    "git.enableSmartCommit": true
} 
Run Code Online (Sandbox Code Playgroud)

(2)/home/hoang/Documents/E29/pset1/2019sp-pset-1-nhvinh118/.vscode/settings.json

{
<<<<<<< HEAD
    "python.pythonPath": "/home/hoang/.local/share/virtualenvs/2019sp-pset-1-nhvinh118-a6Ueu8mF/bin/python",
"~/Documents/E29/pset1/2019sp-pset-1-nhvinh118/pset_1/." 
"python.linting.enabled": true
=======
    "python.pythonPath": "/home/hoang/.local/share/virtualenvs/2019sp-pset-1-nhvinh118-a6Ueu8mF/bin/python"
>>>>>>> master
}
Run Code Online (Sandbox Code Playgroud)

关于我的系统

  • 操作系统:Linux x64 4.15.0-45 通用(Ubuntu 18.04.2 LTS)
  • IDE:Visual Studio 代码 v 1.31.0
  • 解释器:Python 3.7.1

小智 5

您可能正在将模块作为脚本而不是模块来运行。

检查您的launch.json配置。如果不存在,请通过调试面板上的齿轮图标添加一个新的。查看官方调试文档以获取有关如何设置的更多信息launch.json

并在其中添加 python 模块的新配置。例子:

// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Module",
            "type": "python",
            "request": "launch",
            "module": "pset_1.${fileBasenameNoExtension}"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

如果你的配置中有更多的子包,我认为根据层次结构命名它们是一个好习惯。

多个配置和子包的示例:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Module - foo",  // top-level package called "foo"
            "type": "python",
            "request": "launch",
            "module": "foo.${fileBasenameNoExtension}"
        },
        {
            "name": "Python: Module - foo.bar",
            "type": "python",
            "request": "launch",
            "module": "foo.bar.${fileBasenameNoExtension}"
        },
        {
            "name": "Python: Module - foo.buzz",
            "type": "python",
            "request": "launch",
            "module": "foo.buzz.${fileBasenameNoExtension}"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)


希望有帮助!