Pytorch報錯(Windows平臺):from torch._C import * ImportError: DLL load failed: 找不到指定的模塊。

這個錯誤十分奇怪,以前多次安裝都沒碰到過,原因可能和安裝過程有關。貌似這段時間外網信號非常糟糕,我的安裝過程有多次中斷重啓。

如何安裝 pytorch請參考:https://blog.csdn.net/tanmx219/article/details/82814738

如果你的網絡不好,經常需要安裝本地已經下載下來的包,並且需要anaconda自動處理相關依賴,請重點參考其中的離線安裝方法(2)。

下面言歸正傳。

報錯內容

如下:

(base) C:\Users\Administrator>conda activate torch

(torch) C:\Users\Administrator>python
Python 3.7.2 (default, Feb 11 2019, 14:11:50) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "d:\Anaconda3\envs\torch\lib\site-packages\torch\__init__.py", line 84, in <module>
    from torch._C import *
ImportError: DLL load failed: 找不到指定的模塊。
>>>

原因:找不到pytorch庫文件。

解決方案

Python添加庫文件的方式十分簡單,基本上找得到正確的庫文件就不會報錯,所以根據這條線,我簡單地給出解決方案:

電腦==》屬性==》高級系統設置 ==》高級==》環境變量==》系統 ==》Path

添加下面這一條路徑:D:\Anaconda3\envs\torch\Lib\site-packages\torch\lib

如果你打開該路徑就會看到,此處就是Pytorch的_C.lib等庫文件的藏身之處。

 

進一步分析

(說明:後面的內容是碰到問題時的思路,目前還沒有結果,先記錄下  '-_-' ,有空再看)

正常情況下,anaconda是可以找到目錄的,找不到的原因可能是某地庫的地址記錄不全或安裝過程中曾經出現過錯誤。所以,如果pytorch是安裝在anaconda的base環境中,一般也不容易產生這種問題。如果是安裝在自己用conda create - n <name + option> 創建的環境中,就容易產生這種問題。

在windows下Python是如何找目錄的呢?下面這一段是在其源碼getpath.c中的一段註釋

/* ----------------------------------------------------------------
   PATH RULES FOR WINDOWS:
   This describes how sys.path is formed on Windows.  It describes the
   functionality, not the implementation (ie, the order in which these
   are actually fetched is different). The presence of a python._pth or
   pythonXY._pth file alongside the program overrides these rules - see
   below.

   * Python always adds an empty entry at the start, which corresponds
     to the current directory.

   * If the PYTHONPATH env. var. exists, its entries are added next.

   * We look in the registry for "application paths" - that is, sub-keys
     under the main PythonPath registry key.  These are added next (the
     order of sub-key processing is undefined).
     HKEY_CURRENT_USER is searched and added first.
     HKEY_LOCAL_MACHINE is searched and added next.
     (Note that all known installers only use HKLM, so HKCU is typically
     empty)

   * We attempt to locate the "Python Home" - if the PYTHONHOME env var
     is set, we believe it.  Otherwise, we use the path of our host .EXE's
     to try and locate one of our "landmarks" and deduce our home.
     - If we DO have a Python Home: The relevant sub-directories (Lib,
       DLLs, etc) are based on the Python Home
     - If we DO NOT have a Python Home, the core Python Path is
       loaded from the registry.  This is the main PythonPath key,
       and both HKLM and HKCU are combined to form the path)

   * Iff - we can not locate the Python Home, have not had a PYTHONPATH
     specified, and can't locate any Registry entries (ie, we have _nothing_
     we can assume is a good path), a default path with relative entries is
     used (eg. .\Lib;.\DLLs, etc)


   If a '._pth' file exists adjacent to the executable with the same base name
   (e.g. python._pth adjacent to python.exe) or adjacent to the shared library
   (e.g. python36._pth adjacent to python36.dll), it is used in preference to
   the above process. The shared library file takes precedence over the
   executable. The path file must contain a list of paths to add to sys.path,
   one per line. Each path is relative to the directory containing the file.
   Blank lines and comments beginning with '#' are permitted.

   In the presence of this ._pth file, no other paths are added to the search
   path, the registry finder is not enabled, site.py is not imported and
   isolated mode is enabled. The site package can be enabled by including a
   line reading "import site"; no other imports are recognized. Any invalid
   entry (other than directories that do not exist) will result in immediate
   termination of the program.


  The end result of all this is:
  * When running python.exe, or any other .exe in the main Python directory
    (either an installed version, or directly from the PCbuild directory),
    the core path is deduced, and the core paths in the registry are
    ignored.  Other "application paths" in the registry are always read.

  * When Python is hosted in another exe (different directory, embedded via
    COM, etc), the Python Home will not be deduced, so the core path from
    the registry is used.  Other "application paths" in the registry are
    always read.

  * If Python can't find its home and there is no registry (eg, frozen
    exe, some very strange installation setup) you get a path with
    some default, but relative, paths.

  * An embedding application can use Py_SetPath() to override all of
    these automatic path computations.

  * An install of Python can fully specify the contents of sys.path using
    either a 'EXENAME._pth' or 'DLLNAME._pth' file, optionally including
    "import site" to enable the site module.

   ---------------------------------------------------------------- */

從上面的信息來看,似乎修改sys.path的路徑能找到庫文件,於是我在不修改環境變量的前提下,分別試了下PYTHONPATH這個環境變量,也試了一下在D:\Anaconda3\envs\torch\Lib\site-packages下面添加.path文件的辦法(如下),實踐證明這兩者都不能讓系統 找到這些_c.lib庫文件,也就是說導入這些_C.lib等相關的庫文件路徑與sys.path這個參數無關!!!

pywin32.pth

# .pth file for the PyWin32 extensions
win32
win32\lib
Pythonwin
torch
torch\lib

如圖,

Type "copyright", "credits" or "license" for more information.
IPython 7.2.0 -- An enhanced Interactive Python.
>>> import sys
>>> sys.path
Out[2]: 
['C:\\Users\\Administrator',
 'C:\\Users\\Administrator',
 'D:\\Anaconda3\\envs\\torch\\Lib\\site-packages\\torch\\lib',
 'D:\\Anaconda3\\envs\\torch\\python37.zip',
 'D:\\Anaconda3\\envs\\torch\\DLLs',
 'D:\\Anaconda3\\envs\\torch\\lib',
 'D:\\Anaconda3\\envs\\torch',
 '',
 'D:\\Anaconda3\\envs\\torch\\lib\\site-packages',
 'D:\\Anaconda3\\envs\\torch\\lib\\site-packages\\win32',
 'D:\\Anaconda3\\envs\\torch\\lib\\site-packages\\win32\\lib',
 'D:\\Anaconda3\\envs\\torch\\lib\\site-packages\\Pythonwin',
 'D:\\Anaconda3\\envs\\torch\\lib\\site-packages\\torch',
 'D:\\Anaconda3\\envs\\torch\\lib\\site-packages\\IPython\\extensions',
 'C:\\Users\\Administrator\\.ipython']

>>> import torch
Traceback (most recent call last):

  File "<ipython-input-4-eb42ca6e4af3>", line 1, in <module>
    import torch

  File "D:\Anaconda3\envs\torch\lib\site-packages\torch\__init__.py", line 84, in <module>
    from torch._C import *

ImportError: DLL load failed: 找不到指定的模塊。

 

一些參考:

1。pytorch開發團隊認爲,這種問題是因爲在當前目前下也有一個torch文件夾,這個文件夾被誤認爲是torch庫所在的文件夾所致,可惜我的問題不屬於該種情況,

https://github.com/pytorch/pytorch/issues/574
the problem is that you have a folder called `torch` in the same directory which is being picked up. Do this: `cd ..` (to change directory), and then start `python` and `import torch`, it should work.

2。pip install numpy -I
這個辦法來自https://github.com/pytorch/pytorch/issues/2731

只能說這是個非常糟糕的辦法,我使用該命令後陷入了一個更大的困境,如下,

Traceback (most recent call last):
File "", line 1, in 
File "/home/user/anaconda2/envs/py35/lib/python3.5/site-packages/torch/**init**.py", line 53, in 
from torch._C import *
ImportError: numpy.core.multiarray failed to import

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章