蟒: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
问题,我不确定如何修复它 - 我猜这是更多的东西 - 只是不确定是什么。
我刚刚意识到如果我在VScode设置中更改pythonPath
,VScode中的pylint
会抛出错误 - 它找不到Django包。这告诉我,VScode以某种方式具有正确的设置来解析路径 - 但是当从integrated terminal
运行程序时仍然无法导入包。我也可以从python manage.py
终端(VScode之外)运行makemigrations
,migrate
和runserver
venv
- 现在我不太确定问题是什么,但似乎特定于VScode integrated terminal
。
答案 0 :(得分:1)
我刚才在Windows上使用Django打了一个类似的程序,并找到了以下文档:https://code.visualstudio.com/docs/python/tutorial-django
为此,建议您将Python解释器设置为虚拟环境:
这修复了我的查找错误以及终端中的操作,因此也许可以进行类似的操作。