代码之家  ›  专栏  ›  技术社区  ›  Bill Armstrong

VSCode集成终端未加载Django

  •  -1
  • Bill Armstrong  · 技术社区  · 8 年前

    水蟒:1.8.3 VScode:1.22.2 Mac:10.13.4

    刚刚升级到Anaconda 1.8.3,现在无法访问Django包。

    从conda python环境提示中,我得到:

    Last login: Sun Apr 29 19:21:31 on ttys001
    /Users/bill/.anaconda/navigator/a.tool ; exit;
    Bills-iMac:~ bill$ /Users/bill/.anaconda/navigator/a.tool ; exit;
    Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 12:04:33) 
    [GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import django
    >>> django.VERSION
    (2, 0, 2, 'final', 0)
    >>> 
    

    所以我知道Django已经安装,可以从终端导入。在VSCode集成终端中,我得到:

    Bills-iMac:Prov bill$ python manage.py makemigrations
    Traceback (most recent call last):
      File "manage.py", line 8, in <module>
        from django.core.management import execute_from_command_line
    ModuleNotFoundError: No module named 'django'
    
    The above exception was the direct cause of the following exception:
    
    Traceback (most recent call last):
      File "manage.py", line 14, in <module>
        ) 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?
    Bills-iMac:Prov bill$
    

    在VSCode设置中,我的 python.pythonPath 没有改变-仍然是:

    {
        "python.pythonPath": "/anaconda3/envs/py36"
    }
    

    在 venv 路径为:

    >>> import sys
    >>> print(sys.path)
    ['', '/anaconda3/envs/py36/lib/python36.zip', '/anaconda3/envs/py36/lib/python3.6', '/anaconda3/envs/py36/lib/python3.6/lib-dynload', '/anaconda3/envs/py36/lib/python3.6/site-packages']
    >>> 
    

    我已将VScode中的设置更新为 sys.path 但这并没有改变任何事情。我知道软件包已经安装,我知道我可以从提示符进入,但我无法让VScode正常工作。如果是 path 这个问题,我不知道如何解决它-我猜它是更多的东西-只是不知道是什么。

    使现代化

    我刚刚意识到如果我改变 pythonPath 在VScode设置中 pylint 在VScode中,抛出一个错误-它找不到Django包。这告诉我,VScode以某种方式具有正确的设置来解析路径,但当程序从 integrated terminal . 我也可以跑步 python manage.py 对于 makemigrations , migrate 和 runserver 从 venv公司 终端(VScode之外)-现在我不太确定问题是什么,但它似乎是特定于VScode的 综合终端 .

    1 回复  |  直到 6 年前
        1
  •  1
  •   Erik Brown    7 年前

    我刚才在Windows上使用Django运行了一个类似的程序,并找到了以下文档: https://code.visualstudio.com/docs/python/tutorial-django

    作为其中的一部分,建议将Python解释器设置为虚拟环境:

    1. 查看->命令选项板。。。或者只需Ctrl-Shift-P
    2. Python:选择解释器
    3. 选择与您的虚拟环境相对应的一个

    这修复了我的查找错误以及终端中的操作,因此可能类似的操作也会起作用。