Python IDLE openpyxl-运行脚本时发生AttributeError [英] Python IDLE openpyxl - AttributeError when running script
问题描述
我正在尝试通过脚本使用openpyxl.
I'm trying to use openpyxl from a script.
在IDLE shell中使用openpyxl时,一切顺利:
When using openpyxl from an IDLE shell, all goes well:
Python 2.7.9 |Anaconda 2.2.0 (32-bit)| (default, Dec 18 2014, 17:00:07) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import openpyxl as px
>>> wb = px.workbook.Workbook()
>>>
并且我可以使用所有其他openpyxl功能.
and I can use all other openpyxl-functionalities.
但是,将其放入脚本中时...:
However, when putting this in a script...:
import openpyxl as px
wb = px.workbook.Workbook()
(请注意,脚本被称为/保存为"openpyxl_2.py")
(note that the script is called/saved as 'openpyxl_2.py')
并在IDLE中运行脚本,出现以下错误:
and running the script in IDLE, I get the following error:
Python 2.7.9 |Anaconda 2.2.0 (32-bit)| (default, Dec 18 2014, 17:00:07) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
Traceback (most recent call last):
File "\\verdc01\userdocs$\wkvdleeden\My Documents\Python excel\openpyxl_2.py", line 1, in <module>
import openpyxl as px
File "\\verdc01\userdocs$\wkvdleeden\My Documents\Python excel\openpyxl.py", line 8, in <module>
AttributeError: 'module' object has no attribute 'workbook'
>>>
使用Python 2.7.9和openpyxl 2.3.2(与pip一起安装).
Using Python 2.7.9 and openpyxl 2.3.2 (nicely installed with pip).
问题:
为什么从脚本运行时出现上述错误?如何使其正常工作?
How comes that running from a script I get the above error? How to get it working?
后脚本-请注意,我已经检查了以下主题:无法在openpyxl中导入工作簿, openpyxl的导入错误, openpyxl库-jdcal错误
Post scriptum - note that I already checked the following topics: cannot import workbook in openpyxl, Import error for openpyxl, openpyxl library - jdcal error
推荐答案
问题是同一文件夹中名为"openpyxl.py"的脚本.导入 openpyxl
模块时,将导入此本地脚本\模块,而不是全局模块.
The problem is the script named "openpyxl.py" in the same folder.
When importing the openpyxl
module, this local script\module will be imported instead of the global module.
重命名此文件,它应该可以工作.打印px .__ file __
以确认实际导入了哪个模块.
Rename this file and it should work. print px.__file__
to confirm which module is actually imported.
另一个常见陷阱(尤其是对于初学者而言)正在使用本地模块遮盖标准库或第三方名称的名称应用程序所依赖的软件包或模块.特别是一个违反此陷阱的令人惊讶的方法是使用这样的名称作为脚本,因为它随后与之前的执行主模块两次"陷阱会引起麻烦.例如,如果尝试要详细了解Python的套接字模块,您可能会倾向于您的实验脚本socket.py.事实证明,这真的很糟糕想法,因为使用这样的名称意味着Python解释器无法再在标准库中找到真正的套接字模块当前目录中的套接字模块出现问题:
Another common trap, especially for beginners, is using a local module name that shadows the name of a standard library or third party package or module that the application relies on. One particularly surprising way to run afoul of this trap is by using such a name for a script, as this then combines with the previous "executing the main module twice" trap to cause trouble. For example, if experimenting to learn more about Python’s socket module, you may be inclined to call your experimental script socket.py. It turns out this is a really bad idea, as using such a name means the Python interpreter can no longer find the real socket module in the standard library, as the apparent socket module in the current directory gets in the way:
这篇关于Python IDLE openpyxl-运行脚本时发生AttributeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!