Python 空闲 openpyxl - 运行 脚本时出现 AttributeError
Python IDLE openpyxl - AttributeError when running script
我正在尝试从脚本中使用 openpyxl。
当从 IDLE shell 中使用 openpyxl 时,一切顺利:
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 功能。
但是,当把它放在脚本中时...:[=18=]
import openpyxl as px
wb = px.workbook.Workbook()
(注意脚本是called/saved as 'openpyxl_2.py')
和运行 IDLE 中的脚本,我得到以下错误:
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 很好地安装)。
问题:
为什么 运行 我从脚本中得到上述错误?
如何让它工作?
Post scriptum - 请注意,我已经检查了以下主题:
cannot import workbook in openpyxl,
Import error for openpyxl,
openpyxl library - jdcal error
问题是同一文件夹中名为 "openpyxl.py" 的脚本。
导入 openpyxl
模块时,将导入此本地 script\module 而不是全局模块。
重命名此文件,它应该可以工作。 print px.__file__
确认实际导入了哪个模块。
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:
我正在尝试从脚本中使用 openpyxl。
当从 IDLE shell 中使用 openpyxl 时,一切顺利:
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 功能。
但是,当把它放在脚本中时...:[=18=]
import openpyxl as px
wb = px.workbook.Workbook()
(注意脚本是called/saved as 'openpyxl_2.py')
和运行 IDLE 中的脚本,我得到以下错误:
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 很好地安装)。
问题:
为什么 运行 我从脚本中得到上述错误? 如何让它工作?
Post scriptum - 请注意,我已经检查了以下主题: cannot import workbook in openpyxl, Import error for openpyxl, openpyxl library - jdcal error
问题是同一文件夹中名为 "openpyxl.py" 的脚本。
导入 openpyxl
模块时,将导入此本地 script\module 而不是全局模块。
重命名此文件,它应该可以工作。 print px.__file__
确认实际导入了哪个模块。
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: