2017-11-11 355 views
1

我想在Mac上从Excel 2016运行Python脚本。当我运行代码时,什么都没有发生,并且Excel中的状态栏卡在“正在运行”上。我检查了xlwings log file,我可以看到error为什么在Mac上从Excel运行RunPython时,会出现xlwings的ImportError错误?

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

然而,当我输入xlwings从一个Python壳它工作得很好,我也设法写从Python外壳采用xlwings活动工作簿。为什么当我明确安装它时没有名为xlwings的模块?

我使用的是简单的hello.py例如,从xlwings文档:

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

回答

0

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

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

要设置在你的.bash_profile,你会做这样的事情:

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

谢谢您的回答。我当前的'.bash_profile'如下: #设置PATH为Python 3.6 #原始版本保存在.bash_profile.pysave 'PATH =“/ Library/Frameworks/Python.framework/Versions/3.6/bin:$ {PATH}“ export PATH' 这似乎是根据您的建议。但它仍然不起作用。我是否需要在'.config文件'中为解释器设置特定的路径? 'xlwings'模块位于上面PATH中给出的相同目录中。 –

+0

当你打开一个终端窗口时,在其中输入'python',然后尝试导入xlwings,它工作吗? –

+0

不,因为我使用python 3.6。但是,如果我在终端中编写python3并导入xlwings,它就会起作用,正如我在我的问题中所述。 –

相关问题