我有一个名为test1.py的脚本,它不在模块中.它只有在脚本本身运行时才能执行的代码.没有函数,类,方法等.我有另一个作为服务运行的脚本.我想从作为服务运行的脚本中调用test1.py.
例如:
print "I am a test"
print "see! I do nothing productive."
Run Code Online (Sandbox Code Playgroud)
# Lots of stuff here
test1.py # do whatever is in test1.py
Run Code Online (Sandbox Code Playgroud)
我知道一种方法是打开文件,读取内容,并基本上评估它.我假设有一个更好的方法来做到这一点.或者至少我希望如此.
这是我的代码:
# map function
def echo(lines):
if lines:
for i, line in enumerate(lines):
print i, "=>", line
# load the data
idsFile = "ids.txt" # Should be some file on your system
linesData = sc.textFile(idsFile).cache()
# clean it
cleanLinesData = linesData.map(lambda line: line.strip())
filteredLinesData = cleanLinesData.filter(lambda line: True if line else False)
# setup task
answers = filteredLinesData.mapPartitions(echo, 2) # split file into 2 pieces
Run Code Online (Sandbox Code Playgroud)
和文件ids.txt
:
1,2,3,4,5,1
6,4,8,3,2
9,9,9,9,9,9
100000000,1
10,10,10
1,2,4,2
Run Code Online (Sandbox Code Playgroud)
要运行它,我运行:
$ IPYTHON=1 pyspark --master local[2] …
Run Code Online (Sandbox Code Playgroud) 我怎样才能在IPython中获得类似Spyder的runfile
功能?(我在 Python 2.7.9 下使用 IPython v.4.1.2。)