I am trying to use the subprocess module in python but its a bit tricky to get working. Here's my code
import sys
import os
import subprocess
import shlex
def install_module(dir_path, command):
c = shlex.split(command)
os.chdir(dir_path)
try:
p = subprocess.check_output(c, shell=True)
except subprocess.CalledProcessError as e:
#print('install failed for: ' + dir_path + ' ' + command)
print(e.output)
def main():
install_module('D:\installed_software\python modules\kennethreitz-requests-e95e173'
, 'python setup.py install')
install_module('D:\installed_software\python modules\psycopg2-2.6.1'
, 'python setup.py build')
install_module('D:\installed_software\python modules\psycopg2-2.6.1'
, 'python setup.py install')
install_module('D:\installed_software\python modules\pypyodbc-1.3.3\pypyodbc-1.3.3'
, 'python setup.py install')
if __name__ == "__main__":
sys.exit(main())
and my output:
install failed for: D:\installed_software\python modules\psycopg2-2.6.1 python setup.py build
b'running build\r\nrunning build_py\r\nrunning build_ext\r\n'
install failed for: D:\installed_software\python modules\psycopg2-2.6.1 python setup.py install
b'running install\r\nrunning build\r\nrunning build_py\r\nrunning build_ext\r\n'
but when i try running this command normally through cmd i get the below output
D:\installed_software\python modules\psycopg2-2.6.1>python setup.py build
running build
running build_py
running build_ext
Error: pg_config executable not found.
Please add the directory containing pg_config to the PATH
or specify the full executable path with the option:
python setup.py build_ext --pg-config /path/to/pg_config build ...
or with the pg_config option in 'setup.cfg'.
why are they different. I have played with this module a little bit and its really hard to get it to put input back in and to read output from its current shell. Any help would be greatly appreciated
UPDATE:
So the below code works yay! thanks J.F! But I am still having issues with
sys.sterr.flush()
my code with the sys.sterr.flush() line commented
import sys
import os
from subprocess import CalledProcessError, STDOUT, check_output
import shlex
import sys
import os
from subprocess import CalledProcessError, STDOUT, check_output
import shlex
def run_in_path(command, dir_path):
#c = shlex.split(command)
#os.chdir(dir_path)
try:
p = check_output(command, cwd=dir_path, stderr=STDOUT)
except CalledProcessError as e:
sys.stderr.write(e.output.decode("utf-8"))
#sys.sterr.flush()
return e.returncode
else:
return 0
def main():
run_in_path('python setup.py build',
'D:\installed_software\python modules\kennethreitz-requests-e95e173')
run_in_path('python setup.py build',
'D:\installed_software\python modules\psycopg2-2.6.1')
run_in_path('python setup.py install',
'D:\installed_software\python modules\psycopg2-2.6.1')
run_in_path('python setup.py install',
'D:\installed_software\python modules\pypyodbc-1.3.3\pypyodbc-1.3.3')
if __name__ == "__main__":
sys.exit(main())
The error I get when i run sys.sterr.flush() is
sys.sterr.flush()
AttributeError: 'module' object has no attribute 'sterr'
python setup.py install, preferpip install -r requirements.txtinstead: it may enable proper uninstall and it could speedup reinstallations (due to wheel support).pipwithout internetsys.stderrvs.sys.sterr