您现在的位置是:首页 > 文章详情

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

日期:2018-09-13点击:636

最近在撸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导入模块搜索顺序:先当前路径,再环境变量路径。

原文链接:https://yq.aliyun.com/articles/639652
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章