【发布时间】:2023-03-11 08:32:01
【问题描述】:
蟒蛇:1.8.3 VS代码: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 运行时仍然无法导入包。我也可以从venv 终端(在VScode 之外)为makemigrations、migrate 和runserver 运行python manage.py - 现在我不太确定问题出在哪里,但它似乎是特定于VScodeintegrated terminal.
【问题讨论】:
-
Anaconda 5 是最新的,您为什么使用 2013 年发布的 Anaconda 1.8?你的 python 路径应该在环境变量中
-
@Prateek - 当我查看“About Anaconda Navigator”时,它显示版本 1.8.3,但它是包含主页上的 VScode 启动器的最新版本(?)。奇怪 - 不知道为什么 - 更新 anaconda-navigator 时它会返回最新状态。关于环境变量,来自
Command Palette、Python: Select Interpreter- 它引用了/Anaconda3/envs/py36/bin/python,我认为这是正确的。还有其他地方可以存储解释器的环境变量吗? -
Anaconda Navigator v1.8.3 与 Anaconda Distribution v.5.1.0。我的参考是 Anaconda Navigator...
-
这就是我的想法,您指的是一些客户端软件而不是主要软件。我没有 Anaconda,但我也使用 VS 访问 Python。环境变量是指windows系统环境,希望Mac也有类似的。
-
在我的机器上
"python.pythonPath": "python"和"python.envFile": "${workspaceFolder}/.env"希望有帮助
标签: python django visual-studio-code