以下代码位于目录A的fx.py文件中。我想从dir B中的script.py调用run_bash(&#39; ls&#39;)。请帮我解决下面的错误。< / p>
这是fx.py
def run_bash(command, communicate=False):
import subprocess
print "to bash: "+str(command)
print "to assign result to variable, set communicate=True"
if communicate is False:
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
process.wait()
elif communicate is True:
(result, err) = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE).communicate()
process.wait()
return result
print process.returncode
这是script.py
import sys
sys.path.insert(0,'/home/.../fx')
from fx import run_bash
run_bash('ls')
这是错误:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-7-837e03759b76> in <module>()
3 from fx import run_bash
4
----> 5 run_bash('ls')
/home/rtst_adele/Documents/WonderfulDataSpecialist/fx/fx.py in run_bash(command, communicate)
7 import subprocess
8
----> 9 print "to bash: "+str(command)
10 print "to assign result to variable, set communicate=True"
11 if communicate is False:
NameError: global name 'subprocess' is not defined