【发布时间】:2017-03-13 11:49:00
【问题描述】:
要查看环境中已安装的库,我在 Jupyter Python 笔记本单元中运行此代码:
%%bash
pip freeze
这可行,但是如何有条件地执行此代码?
这是我的尝试:
from __future__ import print_function
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
def f(x1):
if(x1 == True):
f2()
return x1
interact(f , x1 = False)
def f2():
%%bash
pip freeze
但评估单元格会引发错误:
File "<ipython-input-186-e8a8ec97ab2d>", line 15
pip freeze
^
SyntaxError: invalid syntax
要生成我正在使用 ipywidgets 的复选框:https://github.com/ipython/ipywidgets
更新:
在 check_call 内运行 pip freeze 返回 0 个结果:
跑步
%%bash
pip freeze
返回已安装的库,因此 0 不正确。
subprocess.check_call("pip freeze", shell=True) 正确吗?
更新 2:
这行得通:
from __future__ import print_function
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
import subprocess
def f(View):
if(View == True):
f2()
interact(f , View = False)
def f2():
print(subprocess.check_output(['pip', 'freeze']))
【问题讨论】:
标签: python pyspark jupyter-notebook jupyter ipywidgets