简体   繁体   English

如何通过 xlwings 包导入 numpy? “导入错误:DLL 加载失败:找不到指定的模块。”

[英]How to import numpy through xlwings package? "ImportError: DLL load failed: The specified module could not be found."

I'm trying to use the 'Run Python' function of xlwings to run Python code through VBA.我正在尝试使用 xlwings 的“运行 Python”功能通过 VBA 运行 Python 代码。

I have been using Spyder to execute my code and it runs with no errors.我一直在使用 Spyder 来执行我的代码并且它运行没有错误。

When trying to run this from VBA with the xlwings package I receive:尝试使用 xlwings 包从 VBA 运行此程序时,我收到:

"ImportError: DLL load failed: The specified module could not be found." “导入错误:DLL 加载失败:找不到指定的模块。”

and this error relates to the numpy package.这个错误与 numpy 包有关。

I tried uninstalling and reinstalling the anaconda package and using pip install numpy .我尝试卸载并重新安装 anaconda 包并使用pip install numpy

I checked that I have the most up to date version of xlwings 0.15.8.我检查了我是否拥有最新版本的 xlwings 0.15.8。

I found this thread https://github.com/xlwings/xlwings/issues/954 stating this issue was fixed with version 0.15.7 of xlwings.我发现此线程https://github.com/xlwings/xlwings/issues/954说明此问题已在 xlwings 0.15.7 版中解决。

VBA code: VBA代码:

RunPython ("import Demand; Demand.calibrate_Demand()")

Spyder code:间谍代码:

import numpy as np
import xlwings as xw
import pandas as pd
import statsmodels.api as sm
from statsmodels.tsa.arima_model import ARMA
from statsmodels.tsa.arima_model import ARMAResults
from matplotlib import pyplot as plt
import datetime

def calibrate_dDemand():

My Python file is called Demand.py我的 Python 文件名为 Demand.py

When executing my VBA code I receive the following error:执行我的 VBA 代码时,我收到以下错误:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "e:\julia\calibration automation\Demand.py", line 17, in <module>
    import numpy as np
  File "C:\Users\julia\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\__init__.py", line 140, in <module>
    from . import _distributor_init
  File "C:\Users\julia\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\_distributor_init.py", line 34, in <module>
    from . import _mklinit
ImportError: DLL load failed: The specified module could not be found.

If I place import pandas as pd first (before importing numpy) I receive this error如果我首先将import pandas as pd (在导入 numpy 之前),我会收到此错误

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "e:\julia\calibration automation\Demand.py", line 19, in <module>
    import pandas as pd
  File "C:\Users\julia\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\__init__.py", line 19, in <module>
    "Missing required dependencies {0}".format(missing_dependencies))
ImportError: Missing required dependencies ['numpy']

I've been trying for a couple of weeks to hit back against this problem too.我也已经尝试了几个星期来反击这个问题。
I have a company-owned laptop which is severely restricted to what environment variables I have access too, and PATH is not one of them.我有一台公司拥有的笔记本电脑,它也严格限制了我可以访问的环境变量,而 PATH 不是其中之一。
I have no trouble linking Excel with Python (Spyder / Anaconda3 installation), but as soon as add import numpy as np to my python code, I get the same DLL load failed error.我将 Excel 与 Python 链接起来没有问题(Spyder / Anaconda3 安装),但是一旦将import numpy as np添加到我的 Python 代码中,我就会收到相同的DLL load failed错误。 (It didn't always do this though, it just started doing it overnight for no reason I can fathom, but I digress). (虽然它并不总是这样做,但它只是一夜之间开始这样做,我无法理解,但我离题了)。 The code itself works fine in Spyder though.不过,代码本身在 Spyder 中运行良好。
I did eventually find something of a workaround though, but it's a bit of a last resort, being more than a bit awkward and daft.不过,我最终确实找到了一些解决方法,但这是最后的手段,不仅有点笨拙和愚蠢。 Maybe useful for a developer to track down a more sensible solution though?不过,也许对开发人员追踪更明智的解决方案有用吗?
Anyway, I found that if I launched Excel from Spyder, the numpy module correctly imports, and remains stable.无论如何,我发现如果我从 Spyder 启动 Excel,numpy 模块会正确导入并保持稳定。 To do this, I just access the contents of one of the workbook's cells without having Excel already open.为此,我只需访问工作簿单元格之一的内容,而无需打开 Excel。 This launches Excel, opens the workbooks, and correctly imports numpy.这将启动 Excel,打开工作簿,并正确导入 numpy。
What do you think?你怎么认为?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MeCab:“ ImportError:DLL加载失败:找不到指定的模块。” - MeCab: “ImportError: DLL load failed: The specified module could not be found.” 导入错误:DLL 加载失败:找不到指定的模块。(Keras) - ImportError: DLL load failed: The specified module could not be found.(Keras) MeCab:“ImportError:DLL 加载失败:找不到指定的模块。” - MeCab: “ImportError: DLL load failed: The specified module could not be found.” 导入错误:DLL 加载失败:找不到指定的模块。 安装成功后 package - ImportError: DLL load failed: The specified module could not be found. After successfully installing the package 导入错误:DLL 加载失败:找不到指定的模块。 Python 3.6 导入 dotnet - ImportError: DLL load failed: The specified module could not be found. Python 3.6 import dotnet ImportError:DLL加载失败:找不到指定的模块。 尝试“从PIL导入图像”时 - ImportError: DLL load failed: The specified module could not be found. when trying to “from PIL import Image” from torch._C import * (ImportError: DLL 加载失败:找不到指定的模块。) - from torch._C import * (ImportError: DLL load failed: The specified module could not be found.) 导入错误:DLL 加载失败:找不到 numpy 的指定模块 - ImportError: DLL load failed: The specified module could not be found for numpy 将Numpy导入Python:“ ImportError:DLL加载失败:找不到指定的过程。” - Importing Numpy to Python: “ImportError: DLL load failed: The specified procedure could not be found.” ImportError:DLL加载失败:找不到指定的过程。蟒蛇 - ImportError: DLL load failed: The specified procedure could not be found. Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM