vscode调试套件突然找不到Django模块

时间:2019-08-20 00:30:49

标签: python django python-3.x visual-studio-code

我已经将我的vscode与我的Django项目完美集成了大约一个月。我周末回家,今天早上突然间我的vscode调试套件无法正常工作。我几乎可以发誓,自上次工作以来,我从未碰过任何东西,但我想我有可能做了一些事情。

注意:当我运行vscode时,以下命令失败,但是当我打开python manage.py shell时,我可以做from django.core.management import execute_from_command_line很好。

我正在尝试在所有测试中运行测试套件,但是在运行以下命令(出于可读性考虑,我放了单独的几行)后,出现错误。我已经在虚拟环境的内部和外部进行了此测试,但没有任何效果。

/Users/hgducharme/Programming/webapp ; 
env DJANGO_SETTINGS_MODULE=webapp.settings.development 
PYTHONIOENCODING=UTF-8 
PYTHONUNBUFFERED=1 
/usr/local/bin/python3-32 
/Users/hgducharme/.vscode/extensions/ms-python.python-2019.8.30787/pythonFiles/ptvsd_launcher.py 
--default --client --host localhost --port 52944 
/Users/hgducharme/Programming/webapp/manage.py test apps/tests/ 

我得到了错误:

Traceback (most recent call last):
  File "/Users/hgducharme/Programming/webapp/manage.py", line 10, in main
    from django.core.management import execute_from_command_line
ImportError: No module named 'django'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/hgducharme/.vscode/extensions/ms-python.python-2019.8.30787/pythonFiles/ptvsd_launcher.py", line 43, in <module>
    main(ptvsdArgs)
  File "/Users/hgducharme/.vscode/extensions/ms-python.python-2019.8.30787/pythonFiles/lib/python/ptvsd/__main__.py", line 432, in main
    run()
  File "/Users/hgducharme/.vscode/extensions/ms-python.python-2019.8.30787/pythonFiles/lib/python/ptvsd/__main__.py", line 316, in run_file
    runpy.run_path(target, run_name='__main__')
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/runpy.py", line 240, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/Users/hgducharme/Programming/webapp/manage.py", line 21, in <module>
    main()
  File "/Users/hgducharme/Programming/webapp/manage.py", line 16, in main
    ) from exc
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?

.vscode / launch.json

{
    "version": "0.2.0",
    "configurations": [{
            "name": "All tests",
            "type": "python",
            "request": "launch",
            "stopOnEntry": true,
            "pythonPath": "${config:python.pythonPath}",
            "program": "${workspaceFolder}/manage.py",
            "args": [
                "test",
                "apps/tests/",
            ],
            "env": {},
            "envFile": "${workspaceFolder}/.vscode/.env",
            "justMyCode": false,
        },
    ]
}

settings.json

{
    "python.testing.unittestArgs": [
        "-v",
        "-s",
        "./apps/tests",
        "-p",
        "test_*.py"
    ],
    "python.testing.pytestEnabled": false,
    "python.testing.nosetestsEnabled": false,
    "python.testing.unittestEnabled": true,
    "python.pythonPath": "/usr/local/bin/python3-32",
    "python.formatting.provider": "black",
    "python.linting.pylintEnabled": true,
    "python.linting.enabled": true,
    "python.linting.flake8Enabled": false,
}

点冻结

appdirs==1.4.3
astroid==2.2.5
atomicwrites==1.3.0
attrs==19.1.0
black==19.3b0
certifi==2019.6.16
chardet==3.0.4
Click==7.0
Django==2.2.2
django-crispy-forms==1.7.2
django-debug-toolbar==2.0
django-redis==4.10.0
idna==2.8
importlib-metadata==0.19
isort==4.3.21
lazy-object-proxy==1.4.1
mccabe==0.6.1
more-itertools==7.2.0
packaging==19.1
pluggy==0.12.0
py==1.8.0
pylint==2.3.1
pyparsing==2.4.2
pytest==5.0.1
pytz==2019.1
redis==3.2.1
requests==2.22.0
six==1.12.0
sqlparse==0.3.0
toml==0.10.0
typed-ast==1.4.0
urllib3==1.25.3
wcwidth==0.1.7
wrapt==1.11.2
xmltodict==0.12.0
zcrmsdk==1.0.8
zipp==0.5.2

1 个答案:

答案 0 :(得分:0)

  1. 确保没有任何依赖项安装,因此安装虚拟环境,激活然后安装要求
  2. 确保激活并运行服务器,以便该应用程序正常运行。
  3. 如果服务器运行正常,我们可以运行测试。