[英]'Unable to import' errors for anaconda environment in VS Code
I am trying to solve partial differential equations with Python using FEniCS .我正在尝试使用 FEniCS 用Python求解偏微分方程。 I installed it with
anaconda
and conda-forge
and to use it, I activate the fenicsproject
environment我用
anaconda
和 conda conda-forge
forge 安装它并使用它,我激活了fenicsproject
环境
source activate fenicsproject
I run my scripts in jupyter
(that works), but often it is more convenient to use VS Code for more elaborate code.我在
jupyter
中运行我的脚本(有效),但通常使用 VS Code 来编写更复杂的代码更方便。 When I run the scripts written in VS Code in the (built-in) terminal, they run without error as long as I have the fenicsproject
environment enabled.当我在(内置)终端中运行用 VS Code 编写的脚本时,只要我启用了
fenicsproject
环境,它们就可以正常运行。
But in the editor I get a lot of errors like this但是在编辑器中我得到了很多这样的错误
[pylint] Unable to import '...' [E0401]'
[pylint] Undefined variable '...' [E0602]
How can I get rid of those errors in the editor, so that the real errors can stand out.我怎样才能摆脱编辑器中的那些错误,以便真正的错误能够脱颖而出。 What would be even better, make it that auto-complete and suggestions work for the packages like
fenics
, mshr
etc.更好的是,让自动完成和建议适用于
fenics
、 mshr
等软件包。
According to the Python in Visual Studio Code docs , this is probably due to Visual Studio Code pointing at the wrong Python version. 根据Visual Studio Code文档中的Python ,这可能是由于Visual Studio Code指向了错误的Python版本。
1. Unable to import (pylint)
1.无法导入(圆筒)
Solution 1: (configure workspace settings to point to fully qualified python executable):
Scenario: You have a module installed, however the linter in the IDE is complaining about;
场景:您已经安装了一个模块,但是IDE中的linter一直在抱怨; not being able to import the module, hence error messages such as the following are displayed as linter errors:
无法导入模块,因此以下错误消息将显示为棉绒错误:
.. unable to import 'xxx' ..
- Cause: The Python extension is most likely using the wrong version of Pylint.
原因:Python扩展很可能使用了错误的Pylint版本。
解决方案1 :(配置工作区设置以指向完全合格的python可执行文件):
Solution 2: (open VS Code from an activated virtual environment):
- Open the workspace settings (settings.json)
打开工作区设置(settings.json)
- Identify the fully qualified path to the python executable (this could even be a virtual environment)
确定python可执行文件的完全限定路径(甚至可以是虚拟环境)
- Ensure Pylint is installed for the above python environment
确保为上述python环境安装了Pylint
Configure the setting "pythonPath" to point to (previously identified) the fully qualified python executable.
将设置“ pythonPath”配置为指向(先前标识的)标准python可执行文件。
"python.pythonPath": "/users/xxx/bin/python" ```
解决方案2 :(从已激活的虚拟环境中打开VS Code):
- Open the terminal window
打开终端窗口
- Activate the relevant python virtual environment
激活相关的python虚拟环境
Ensure Pylint is installed within this virtual environment
确保在此虚拟环境中安装了Pylint
pip install pylint
- Close all instances of VS Code
关闭所有VS Code实例
- Launch VS Code from within this terminal window
从此终端窗口中启动VS Code
(this will ensure the VS Code process will inherit all of the Virtual Env environment settings)(这将确保VS Code进程将继承所有Virtual Env环境设置)
A slight correction to "Solution 1" above: use对上述“解决方案 1”的轻微修正:使用
"python.defaultInterpreterPath": "/users/xxx/bin/python" ```
source: https://github.com/microsoft/vscode-python/wiki/Setting-descriptions#pythondefaultinterpreterpath来源: https://github.com/microsoft/vscode-python/wiki/Setting-descriptions#pythondefaultinterpreterpath
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.