I received an import error while debugging main.py. My IDE Visual Studio Code seems having problem recognizing my TREE structure (shown below), since the following worked outside of VSC:
- Run in virtual environment:
pipenv run python3.7 -m pset_1 Debug in virtual environment:
pipenv run python3.7 -m pdb pset_1Exception has occurred: ImportError attempted relative import with no known parent package File "/home/hoang/Documents/E29/pset1/2019sp-pset-1-nhvinh118/pset_1/main.py", line 4, in from .hash_str import get_csci_salt, get_user_id, hash_str File "/usr/local/lib/python3.7/runpy.py", line 85, in _run_code exec(code, run_globals) File "/usr/local/lib/python3.7/runpy.py", line 96, in _run_module_code mod_name, mod_spec, pkg_name, script_name) File "/usr/local/lib/python3.7/runpy.py", line 263, in run_path pkg_name=pkg_name, script_name=fname)
IMPORT statement in __main__.py (module to debug)
from .hash_str import get_csci_salt, get_user_id, hash_str
from .load_data import load_vectors, load_words, load_dataTREE
.
|-- 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.pyMy two settings.json
(1) /home/hoang/.config/Code/User/settings.json
{ "python.pythonPath": "/home/hoang/anaconda3/bin/python", "git.enableSmartCommit": true
} (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
}About my system
- OS: Linux x64 4.15.0-45 generic (Ubuntu 18.04.2 LTS)
- IDE: Visual Studio Code v 1.31.0
- Interpreter: Python 3.7.1
1 Answer
You are probably running the module as a script instead of a module.
Check your launch.json configuration. If not present, add a new one via the gear icon at the debug panel. Check out the official debugging documentation for more info about how to setup launch.json.
And in there, add a new configuration for a python module. Example:
// For more information, visit:
{ "version": "0.2.0", "configurations": [ { "name": "Python: Module", "type": "python", "request": "launch", "module": "pset_1.${fileBasenameNoExtension}" } ]
}If there are more sub packages in your configuration, I consider it a good habit to name them according to the hierarchical structure.
Example for multiple configuration and sub packages:
{ "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}" } ]
}Hope that helps!