运行python脚本,就像它是以交互方式运行的一样

运行python脚本,就像它是以交互方式运行的一样,python,Python,出于某些文档目的,我需要运行一些python代码行,并将输出放入类的docstring中 结果应该如下所示: >>> from sklearn.cluster import KMeans >>> import numpy as np >>> X = np.array([[1, 2], [1, 4], [1, 0], ... [4, 2], [4, 4], [4, 0]]) >>> kmeans =

出于某些文档目的,我需要运行一些python代码行,并将输出放入类的docstring中

结果应该如下所示:

>>> from sklearn.cluster import KMeans
>>> import numpy as np
>>> X = np.array([[1, 2], [1, 4], [1, 0],
...               [4, 2], [4, 4], [4, 0]])
>>> kmeans = KMeans(n_clusters=2, random_state=0).fit(X)
>>> kmeans.labels_
array([0, 0, 0, 1, 1, 1], dtype=int32)
>>> kmeans.predict([[0, 0], [4, 4]])
array([0, 1], dtype=int32)
>>> kmeans.cluster_centers_
array([[ 1.,  2.],
       [ 4.,  2.]])
现在我的问题是,假设我有一个包含这几行代码的文件,如何使用
python
运行它,从而获得这样的输出

Bash有一个类似的选项,您可以在文件中包含以下内容,比如
demo.sh

mkdir /tmp/test1
touch /tmp/test1/1
ls /tmp/test1
您可以将其作为
bash-xdemo.sh运行,并获得以下输出:

$ bash -x /tmp/tmp.sh 
+ mkdir /tmp/test1
+ touch /tmp/test1/1
+ ls /tmp/test1
1

有没有一种方法可以对
python
执行同样的操作?

您可以使用
code
模块的
InteractiveConsole
类:

模拟交互式.py

import code
import sys

icon = code.InteractiveConsole()

prompt = '>>>'
for line in sys.stdin:
    line = line.rstrip()
    print(prompt, line)
    prompt = ('...' if icon.push(line) else '>>>')
import random

print(random.randint(1, 7))
print(random.randint(1, 7))
print(random.randint(1, 7))
print(random.randint(1, 7))
test.py

import code
import sys

icon = code.InteractiveConsole()

prompt = '>>>'
for line in sys.stdin:
    line = line.rstrip()
    print(prompt, line)
    prompt = ('...' if icon.push(line) else '>>>')
import random

print(random.randint(1, 7))
print(random.randint(1, 7))
print(random.randint(1, 7))
print(random.randint(1, 7))
运行示例:

~/Desktop $ python3 emulate-interactive.py < test.py
>>> import random
>>>
>>> print(random.randint(1, 7))
1
>>> print(random.randint(1, 7))
7
>>> print(random.randint(1, 7))
4
>>> print(random.randint(1, 7))
4
~/Desktop $
~/Desktop$python3仿真-interactive.py>>随机输入
>>>
>>>打印(random.randint(1,7))
1.
>>>打印(random.randint(1,7))
7.
>>>打印(random.randint(1,7))
4.
>>>打印(random.randint(1,7))
4.
~/桌面$

在tmp.txt中包含以下内容

$ cat tmp.txt
from sklearn.cluster import KMeans
import numpy as np
X = np.array([[1, 2], [1, 4], [1, 0],
              [4, 2], [4, 4], [4, 0]])
kmeans = KMeans(n_clusters=2, random_state=0).fit(X)
kmeans.labels_
kmeans.predict([[0, 0], [4, 4]])
以下cmd将以交互方式显示与从tmp.txt运行cmds相同的输出

$ python -c "import code; c=code.InteractiveConsole(); dec = lambda f: lambda x: print(x) or f(x); c.push = dec(c.push); c.interact('', '')" < tmp.txt
>>> from sklearn.cluster import KMeans
>>> import numpy as np
>>> X = np.array([[1, 2], [1, 4], [1, 0],
...               [4, 2], [4, 4], [4, 0]])
>>> kmeans = KMeans(n_clusters=2, random_state=0).fit(X)
>>> kmeans.labels_
array([0, 0, 0, 1, 1, 1], dtype=int32)
>>> kmeans.predict([[0, 0], [4, 4]])
array([0, 1], dtype=int32)
>>> 
$python-c“导入代码;c=code.InteractiveConsole();dec=lambda f:lambda x:print(x)或f(x);c.push=dec(c.push);c.interact(“”,)”>>从sklearn.cluster导入KMeans
>>>将numpy作为np导入
>>>X=np.数组([[1,2],[1,4],[1,0],
...               [4, 2], [4, 4], [4, 0]])
>>>kmeans=kmeans(n_集群=2,随机状态=0)。拟合(X)
>>>kmeans.labels_
数组([0,0,0,1,1,1],dtype=int32)
>>>kmeans.predict([[0,0],[4,4]]
数组([0,1],dtype=int32)
>>> 

<>代码>你可以考虑使用<代码> iPython < /C>吗?