Python 导入错误:DLL 加载在 Jupyter 笔记本中失败,但在 .py 文件中工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46353215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:34:55  来源:igfitidea点击:

Import error: DLL load failed in Jupyter notebook but working in .py file

pythonanacondajupyter-notebookswigpython-importlib

提问by nth-attempt

I installed BreakoutDetectionthe module in Anaconda environment. When I tried to import the module using import breakout_detectionin jupyter notebook, I get the below error

我在 Anaconda 环境中安装了BreakoutDetection模块。当我尝试使用import breakout_detectionjupyter notebook导入模块时,出现以下错误

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-18-96c0fdb15b96> in <module>()
----> 1 import breakout_detection

C:\Users\sgadiyar\AppData\Local\Continuum\Anaconda2\lib\site-packages\breakout_detection.py in <module>()
     15         except ImportError:
     16             return importlib.import_module('_breakout_detection')
---> 17     _breakout_detection = swig_import_helper()
     18     del swig_import_helper
     19 elif _swig_python_version_info >= (2, 6, 0):

C:\Users\sgadiyar\AppData\Local\Continuum\Anaconda2\lib\site-packages\breakout_detection.py in swig_import_helper()
     14             return importlib.import_module(mname)
     15         except ImportError:
---> 16             return importlib.import_module('_breakout_detection')
     17     _breakout_detection = swig_import_helper()
     18     del swig_import_helper

C:\Users\sgadiyar\AppData\Local\Continuum\Anaconda2\lib\importlib\__init__.pyc in import_module(name, package)
     35             level += 1
     36         name = _resolve_name(name[level:], package, level)
---> 37     __import__(name)
     38     return sys.modules[name]

ImportError: DLL load failed: The specified procedure could not be found.

I could import the same module in python shell

我可以在 python shell 中导入相同的模块

I looked at the system paths (print sys.path) and in both python shell and jupyter notebook. They both are the same. Executable path (print sys.executable) is also the same for the both.

我查看了系统路径 ( print sys.path) 以及 python shell 和 jupyter notebook。他们都是一样的。print sys.executable两者的可执行路径 ( ) 也相同。

Can someone help me out on the steps I should take to resolve this issue? Thanks!

有人可以帮助我解决我应该采取的步骤来解决这个问题吗?谢谢!

回答by picklu

I encountered the same problem running Jupyter Notebook from PowerShell. Even though the question was asked a year back, I am answering it here to help those who encounter the same error recently. In my case, first, I activated the root environment activate basethen I ran jupyter notebookand it worked just fine. Once you activate the base, you will notice that the prompt will change like this: (base) X:\Users\xxxxx\current-directory-name>.

我在从 PowerShell 运行 Jupyter Notebook 时遇到了同样的问题。尽管这个问题是一年前提出的,但我在这里回答它是为了帮助那些最近遇到同样错误的人。就我而言,首先,我激活了根环境,activate base然后运行jupyter notebook,它运行得很好。激活底座后,您会注意到提示将更改如下: (base) X:\Users\xxxxx\current-directory-name>

  • Notethat the command activate basewill not work on Powershell. You have to switch to command prompt running cmdor you may try the whole thing on the command prompt instead of PowerShell.

  • If the required paths
    (path:\to\Anaconda3\;path:\to\Anaconda3\Scripts;path:\to\Anaconda3\Library\mingw-w64\bin;path:\to\Anaconda3\Library\usr\bin;path:\to\Anaconda3\Library\bin) are available to your PowerShell environment, then you don't need to activate the "base" environment.

  • 请注意,该命令activate base在 Powershell 上不起作用。您必须切换到正在运行的命令提示符,cmd或者您可以在命令提示符而不是 PowerShell 上尝试整个操作。

  • 如果所需的路径
    ( path:\to\Anaconda3\;path:\to\Anaconda3\Scripts;path:\to\Anaconda3\Library\mingw-w64\bin;path:\to\Anaconda3\Library\usr\bin;path:\to\Anaconda3\Library\bin) 可用于您的 PowerShell 环境,则您无需激活“基本”环境。

回答by Dheeraj

In windows, active root(base) in command prompt first by

在 Windows 中,首先在命令提示符中激活 root(base)

activate root

then

然后

jupyter notebook

回答by Akash Desarda

If you have already added anaconda to PATH variable then you have to do the following

如果您已经将 anaconda 添加到 PATH 变量,那么您必须执行以下操作

activate base jupyter kernelspec list python -m ipykernel install --user

activate base jupyter kernelspec list python -m ipykernel install --user

It worked for me

它对我有用

回答by Hemang

I recently re-installed Anaconda to a new directory (from D: to C:). After that, opening and running PowerShell (in Windows 10) caused it to throw the same errors.

我最近将 Anaconda 重新安装到一个新目录(从 D: 到 C:)。之后,打开并运行 PowerShell(在 Windows 10 中)导致它抛出相同的错误。

By following @picklu 's answer I was able to run it temporarily from the CMD (by running 'activate base' and then 'jupyter notebook'). Even 'activate root' worked instead of activate base. Also running it from conda prompt, anaconda navigator and the default shortcut for Jupyter Notebook worked. However, using these methods I wasn't able to start from the folder I had opened the PowerShell window in (by using Ctrl+Shift+ mouseRightCLick).

通过遵循@picklu 的回答,我能够从 CMD 临时运行它(通过运行“激活基础”然后运行“jupyter notebook”)。即使是“激活根”也能代替激活基。同样从 conda 提示符、anaconda 导航器和 Jupyter Notebook 的默认快捷方式运行它。但是,使用这些方法时,我无法从打开 PowerShell 窗口的文件夹开始(使用 Ctrl+Shift+m​​ouseRightCLick)。

However, by following the second part of @picklu 's answer I was able to successfully change the system variable PATH from the old D: drive to the current C: .

但是,通过遵循@picklu 答案的第二部分,我能够成功地将系统变量 PATH 从旧的 D: 驱动器更改为当前的 C: 。

I added the following:

我添加了以下内容:

  1. c:\users\USERNAME\anaconda3
  2. c:\users\USERNAME\anaconda3\scripts
  3. c:\users\USERNAME\anaconda3\library\bin
  4. c:\users\USERNAME\anaconda3\library\usr\bin
  1. c:\用户\用户名\anaconda3
  2. c:\users\USERNAME\anaconda3\scripts
  3. c:\users\USERNAME\anaconda3\library\bin
  4. c:\users\USERNAME\anaconda3\library\usr\bin

where c:\users\USERNAME\anaconda3 is the anaconda install location.

其中 c:\users\USERNAME\anaconda3 是 anaconda 安装位置。

回答by Raywall

I met the same problem when I import sklearn.I guess some pakages need upgrated.So I just run (conda update --all) to upgrate all the pakages,and it finally worked.

我在导入 sklearn 时遇到了同样的问题。我猜有些包需要升级。所以我只是运行(conda update --all)来升级所有的包,它终于奏效了。

回答by wombatonfire

If we look at the Anaconda FAQ, we can find that it's not recommended to add Anaconda to the Windows PATH.

如果我们查看Anaconda FAQ,我们会发现不建议将 Anaconda 添加到 Windows PATH 中。

While it should work, it's not really easy and straightforward to manage all the needed paths manually. As an example, none of the answers here at the time of my writing has all the paths, which Anaconda adds on my machine. We can get the list using os module:

虽然它应该可以工作,但手动管理所有需要的路径并不容易和直接。例如,在我撰写本文时,这里的所有答案都没有包含 Anaconda 在我的机器上添加的所有路径。我们可以使用 os 模块获取列表:

For Python 2:

对于 Python 2:

Python 2.7.15 |Anaconda, Inc.| (default, Feb 21 2019, 11:55:13) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print os.environ['PATH']

For Python 3:

对于 Python 3:

Python 3.7.2 (default, Feb 21 2019, 17:35:59) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print(os.environ['PATH'])

In my case, I have the following Anaconda paths in it:

就我而言,我有以下 Anaconda 路径:

C:\Anaconda;
C:\Anaconda\Library\mingw-w64\bin;
C:\Anaconda\Library\usr\bin;
C:\Anaconda\Library\bin;
C:\Anaconda\Scripts;
C:\Anaconda\bin;
C:\Anaconda\condabin;

And this is just for the base environment. If you have additional environments, there will be similar paths for each of them. Do you really want to add them all, update them, and make sure you haven't missed any if something is changed with new Anaconda release?

这仅适用于基础环境。如果您有其他环境,则每个环境都有相似的路径。如果新的 Anaconda 版本发生变化,您真的想全部添加、更新它们并确保您没有错过任何内容吗?

I doubt it. And the good news is that you don't have to. All the paths are configured for you automatically when you activate the environment.

我对此表示怀疑。好消息是您不必这样做。当您激活环境时,系统会自动为您配置所有路径。

It might only be usefull to add the Scriptsfolderto PATH, so that you can run activatewithout a full path. And then it might be handy to create a bunch of .bat/.cmdfiles for Python interpreters or Jupyter Notebook, which will simplify environment activation and allow you to do everything with one command.

将文件Scripts夹添加到PATH可能仅有用,这样您就可以在activate没有完整路径的情况下运行。然后.bat/.cmd为 Python 解释器或 Jupyter Notebook创建一堆文件可能会很方便,这将简化环境激活并允许您使用一个命令完成所有操作。

For example, I have two .cmdfiles for Python 2 and Python 3 interpreter on my machine:

例如,我的机器上有两个.cmd用于 Python 2 和 Python 3 解释器的文件:

python.cmd:

蟒蛇.cmd

@echo off
call activate
python %*

python3.cmd:

python3.cmd

@echo off
call activate python37
python %*

A folder with these files is added to PATH, so when I run pythonor python3command, the relevant conda environment is activated and the Python interpreter is started in the appropriate context. If you pass cmdline parameters to the interpreter, they are also forwarded correctly. Similar scripts can be cretated for Jupyter.

一个包含这些文件的文件夹被添加到 PATH,所以当我运行pythonpython3命令时,相关的 conda 环境被激活,并且 Python 解释器在适当的上下文中启动。如果您将 cmdline 参数传递给解释器,它们也会被正确转发。可以为 Jupyter 创建类似的脚本。