首页 > 解决方案 > 如何修复 vscode 中的 import xxx 错误?

问题描述

当我尝试在 vscode 中导入 numpy/tensorflow 时,我遇到了一些奇怪的错误。但是,在具有相同虚拟环境的 pycharm 中一切都很好。例如,运行以下代码

print('hello VScode')
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

然后我得到了

PS E:\Py_projects\intensity> conda activate deep
PS E:\Py_projects\intensity> & D:/Anaconda/envs/deep/python.exe e:/Py_projects/intensity/hellovs.py
hello VScode
Traceback (most recent call last):
  File "D:\Anaconda\envs\deep\lib\site-packages\tensorflow_core\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "D:\Anaconda\envs\deep\lib\site-packages\tensorflow_core\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "D:\Anaconda\envs\deep\lib\site-packages\tensorflow_core\python\pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "D:\Anaconda\envs\deep\lib\imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "D:\Anaconda\envs\deep\lib\imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: DLL load failed: The specified module could not be found.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "e:/Py_projects/intensity/hellovs.py", line 2, in <module>
    import tensorflow as tf
  File "D:\Anaconda\envs\deep\lib\site-packages\tensorflow\__init__.py", line 99, in <module>
    from tensorflow_core import *
  File "D:\Anaconda\envs\deep\lib\site-packages\tensorflow_core\__init__.py", line 34, in <module>
    from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
  File "D:\Anaconda\envs\deep\lib\site-packages\tensorflow\__init__.py", line 50, in __getattr__
    module = self._load()
  File "D:\Anaconda\envs\deep\lib\site-packages\tensorflow\__init__.py", line 44, in _load
    module = _importlib.import_module(self.__name__)
  File "D:\Anaconda\envs\deep\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "D:\Anaconda\envs\deep\lib\site-packages\tensorflow_core\python\__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "D:\Anaconda\envs\deep\lib\site-packages\tensorflow_core\python\pywrap_tensorflow.py", line 74, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "D:\Anaconda\envs\deep\lib\site-packages\tensorflow_core\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "D:\Anaconda\envs\deep\lib\site-packages\tensorflow_core\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "D:\Anaconda\envs\deep\lib\site-packages\tensorflow_core\python\pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "D:\Anaconda\envs\deep\lib\imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "D:\Anaconda\envs\deep\lib\imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: DLL load failed: The specified module could not be found.


Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/errors

for some common reasons and solutions.  Include the entire stack trace
above this error message when asking for help.

另一个例子是

 from sklearn.model_selection import train_test_split

然后我得到了

PS E:\Py_projects\intensity> & D:/Anaconda/envs/deep/python.exe e:/Py_projects/intensity/LR.py
Traceback (most recent call last):
  File "e:/Py_projects/intensity/LR.py", line 2, in <module>
    from sklearn.model_selection import train_test_split
  File "D:\Anaconda\envs\deep\lib\site-packages\sklearn\__init__.py", line 74, in <module>
    from .base import clone
  File "D:\Anaconda\envs\deep\lib\site-packages\sklearn\base.py", line 20, in <module>
    from .utils import _IS_32BIT
  File "D:\Anaconda\envs\deep\lib\site-packages\sklearn\utils\__init__.py", line 25, in <module>
    from .fixes import np_version
  File "D:\Anaconda\envs\deep\lib\site-packages\sklearn\utils\fixes.py", line 18, in <module>
    import scipy.stats
  File "D:\Anaconda\envs\deep\lib\site-packages\scipy\stats\__init__.py", line 379, in <module>
    from .stats import *
  File "D:\Anaconda\envs\deep\lib\site-packages\scipy\stats\stats.py", line 180, in <module>
    import scipy.special as special
  File "D:\Anaconda\envs\deep\lib\site-packages\scipy\special\__init__.py", line 641, in <module>
    from ._ufuncs import *
ImportError: DLL load failed: The specified module could not be found.

标签: pythonvscode-settings

解决方案


当您将 VS Code shell 从 Powershell 切换到 cmd 时,检查一切是否正常。如果有效,则可能是此处提到的路径问题:DLL 加载失败:找不到指定的模块 Windows 10 shell


推荐阅读