python文件名与包重名
import os import xlrd newpath = os.chdir('E:/') filename = "example_copy.xlsx" file = os.path.join(os.getcwd(),filename) xl = xlrd.open_workbook(file) print(xl.sheet_names()) print(xl.sheets())
文件名是xlrd,导入包名xlrd,运行结果
D:\Python\Python36\python3.exe C:\Users\Da\PycharmProjects\untitled3\xlrd.py
Traceback (most recent call last):
File "C:\Users\Da\PycharmProjects\untitled3\xlrd.py", line 2, in <module>
import xlrd
File "C:\Users\Da\PycharmProjects\untitled3\xlrd.py", line 11, in <module>
xl = xlrd.open_workbook(file)
AttributeError: module 'xlrd' has no attribute 'open_workbook'
将文件名改成与包xlrd包不同命就可以了。
文件名与包同名,包会被文件名屏蔽,导致无法导入。python中导入单个文件和导入包是同样import