随着我越来越多地学习如何不构建编码项目,我意识到我必须四处走动才能将它们放置在正确的位置。
例如,我有一个“实践数据科学”项目,我只是将各种无关的代码转储到其中。我的目录如下所示:
- PyCharm Projects
- data-science-at-home
- birth_names.py
- birthplots.py
- genedata.py
- etc.
现在,我正在学习如何将您的代码分成与之相关的模块“包”(.py文件,对吗?)。
因此,在我的IDE(PyCharm)中,我创建了一个新程序包,然后将重构的.py文件移动到它们中:
- PyCharm Projects
- data-science-at-home
- birth-names
- birth_names.py
- birthplots.py
- package_genestuff
- genedata.py
因此,我发现我的所有代码仍然可以按预期进行编译和运行,但是在我的graphingutility.py文件的顶部(import birthnames as bn
出现no module named birthnames
错误)。由于某种原因,所有内容都在编译,并且原本不存在的模块正在重复使用,但是错误弹出窗口确实令人讨厌。
我注意到,移动重构只能工作大约一半的时间,并且在工作时似乎会引起很多问题。也许手动做这种事情会更好,但是我不理解所有的xml,config和git文件的内部工作方式,每次我抽动手指时似乎都会改变……什么是正确的获取方式完成了吗
编辑:根据要求,实际代码:
import birth_names as bn
import matplotlib.pyplot as plt
def myPlotter(ax, data1, data2, param_dict):
out = ax.plot(data1, data2, **param_dict)
return out
def plotRankAndScores(name, gender):
files = bn.getPaths()
print(files)
x1, y1 = bn.getAllRanks(name, gender, files)
x2, y2 = bn.getAllScores(name, gender, files)
ave = bn.getAverageRank(name, gender, select=False, filez=files)
# fig, (ax1, ax2) = plt.subplots(2, 1)
# myPlotter(ax1, x1, y1, {'linestyle': '-.', 'color': 'red'})
# myPlotter(ax2, x2, y2, {'linestyle': '--'})
fig2, (ax3, ax4) = plt.subplots(2, 1, sharex='all', figsize=(10, 10))
plt.xlabel("Year")
ax3.plot(x1, y1, 'b')
ax3.set_ylabel("Rank")
ax3.axhline(y1.mean(), label='average = {}'.format(ave), linestyle='--', color='red')
ax3.legend()
ax4.plot(x2, y2, 'b')
ax4.set_ylabel("Number of Births")
ax4.axhline(y2.mean(), label='average = {}'.format(y2.mean()), linestyle='--', color='red')
ax4.legend()
plt.suptitle("Name Rank and Number of Births by Year")
plt.show()
if __name__ == '__main__':
plotRankAndScores("Wesley", "M")
将顶行更改为此: from . import birth_names as bn
说明:用英语,上一行表示:从此脚本所在的目录中,导入名称为“ bn”的文件“ birth_names”
该.
指示本地目录。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句