方法一:從命令行運行
(1)首先進入DOS環境。win+r,然後輸入cmd。
(2)直接輸入python+空格
(3)將文件拖拽到光標處或者輸入文件所在位置
方法二:解釋器中運行文件
爲了方便操作,將hello.py直接放在D盤下。
(1)在DOS環境下,定位到python文件所在位置;
(2)進入python交互式環境;
(3)import 文件名
方法三:在pycharm+adaconda環境下
將文件放在同一個工程下,如果要在A文件中引用B.py文件中的函數,
直接在A文件中開頭添加:from B import *
然後便可以在A文件中使用B中的函數。
比如:在新建的example_lenses.py中使用前篇中完成的決策樹構建和繪製文件trees.py和treePlotter.py文件。
from trees import *
from treePlotter import *
# 加載數據集
with open('lenses.txt') as f:
lensesData = [example.strip().split('\t') for example in f.readlines()]
lensesFeatLabels = ['age','prescript','astigmatic','tearRate']
print(lensesData)
lensesTree = createTree(lensesData,lensesFeatLabels)
print(lensesTree)
print(createPlot(lensesTree))
運行結果: