如何手动重构python项目?

如何手动重构python项目?,python,package,directory-structure,Python,Package,Directory Structure,随着我越来越多地学习如何不构建编码项目,我意识到我必须移动很多东西才能将它们放在正确的位置 例如,我有一个实践数据科学项目,我只是将各种不相关的代码转储到其中。我的目录如下所示: - PyCharm Projects - data-science-at-home - birth_names.py - birthplots.py - genedata.py - etc. 现在,我正在学习如何将代码分离到与modu

随着我越来越多地学习如何不构建编码项目,我意识到我必须移动很多东西才能将它们放在正确的位置

例如,我有一个实践数据科学项目,我只是将各种不相关的代码转储到其中。我的目录如下所示:

 - PyCharm Projects
     - data-science-at-home
         - birth_names.py
         - birthplots.py
         - genedata.py
         - etc.
现在,我正在学习如何将代码分离到与modules.py文件相关的包中,对吗

因此,在我的IDE PyCharm中,我创建了一个新的包,然后将重构后的.py文件移到其中:

 - PyCharm Projects
     - data-science-at-home
         - birth-names
             - birth_names.py
             - birthplots.py
         - package_genestuff
             - genedata.py
因此,我发现我的所有代码仍在按预期编译和运行,但在graphingutility.py文件的顶部,我将出生名导入为bn,我得到一个没有模块命名的出生名错误。出于某种原因,所有的东西都在编译,并且假定不存在的模块被反复使用,但是错误弹出窗口真的很烦人

我注意到move refactor只在大约一半的时间内起作用,而且在起作用时似乎会引起很多问题。也许手动执行这类操作会更好,但我不了解所有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")

将顶行更改为: 从…起以bn的形式导入您的姓名

说明: 在英语中,上述行的意思是:从该脚本所在的目录中,导入名为“bn”的文件“birth_names”


这个。指示本地目录。

您需要向我们显示一些代码和错误消息,以便我们能够帮助您。你可能只是走错了路。你从哪里输入出生名?@TammoHeeren你是什么意思?我展示了问题的确切层次结构。如上图所示,这两个文件并排放在同一个包/文件夹中?显示printsys.path.的输出要导入模块,要么它必须在当前目录中,要么它的父目录必须在sys.path中。@JohnGordon,这两个文件并排在同一目录中。那么可能是系统路径的问题?那是什么?\uuuu init\uuuuu.py仅用于Python 2。在Python3中,可以有没有它的模块,明白了。您能从graphingutility.py文件中发布代码吗?发布时,代码为导入出生名称为bn。但是实际的模块名是birthnames,而不是birthnames。@约翰戈登,对不起,我没想到人们会想要实际的代码,所以我懒得回答我的问题。实际的文件名确实匹配。就我而言,这是一个非常糟糕的形式,我已经更新了这个问题,以反映导入名和文件名实际上是匹配的。@TD1完美,解决了它!关于为什么这是正确的方法,有什么线索吗?