python中import的引用机制引起的坑

最近在撸Scikit-Learn的代码,想加载点Seaborn的数据训练模型,简单的一句seaborn.load_dataset('')都编译通不过。

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from sklearn.linear_model import LinearRegression
# 选择模型
model = LinearRegression(fit_intercept=True)
# 整理数据
iris = sns.load_dataset('iris')
rng = np.random.RandomState(42)
x = 10* rng.rand(50)
y = 2*x - 1 + rng.randn(50)
X = x[:, np.newaxis]
#拟合数据
model.fit(X,y)
#预测
xfit = np.linspace(-1,11)
Xfit = xfit[:, np.newaxis]
yfit = model.predict(Xfit)
plt.scatter(x,y)
plt.plot(xfit, yfit)
plt.show()
PS D:\sanye\pythonDEMO> python
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> import seaborn as sns
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\sanye\pythonDEMO\seaborn.py", line 5, in <module>
    iris = sns.load_dataset('iris')
AttributeError: module 'seaborn' has no attribute 'load_dataset'

慌了,一脸懵。仔细检查发现项目路径下有一个seaborn.py文件,改名一起恢复平静。汗~
这个完全是因为python导入模块的搜索路径以及优先级问题引起的。import导入模块搜索顺序:先当前路径,再环境变量路径。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值