为什么在 Mac 上从 Excel 运行 运行 RunPython 时会出现 xlwings 的 ImportError?

Why do I get ImportError of xlwings when running RunPython from Excel on Mac?

我正在尝试 运行 Mac 上 Excel 2016 的 Python 脚本。当我 运行 代码没有任何反应时,Excel 中的状态栏卡在“运行”。我检查了 xlwings log file,我可以看到 error

Traceback (most recent call last):

File "", line 1, in

File "/Users/dano/Desktop/hello.py", line 3, in

import xlwings as xw

ImportError: No module named xlwings

然而,当我从 Python shell 导入 xlwings 时,它工作正常,而且我还设法从 Python Shell 使用 xlwings。为什么我明明安装了模块却说没有xlwings模块?

我正在使用 xlwings 文档中的简单 hello.py 示例:

import numpy as np
import xlwings as xw
def world():
  wb = xw.Book.caller()
  wb.sheets[0].range('A1').value = 'Hello World!'

.py fileexcel file 位于我的桌面上。我是 运行宁 Python 3.6 并且已经使用 pip3.

安装了 xlwings

xlwings 采用 .bash_profile 文件中定义的默认 Python 安装,请参阅 docs.

也就是说,您需要在 PATH 中包含 python3(假设您使用的是 pip3)或者您需要通过 xlwings 设置 Python 解释器。

要在您的 .bash_profile 中设置它,您需要执行以下操作:

export PATH="/path/to/python3/bin:$PATH"