Subprocess not working

I use subprocess all the time as the โ€˜correctโ€™ way to call shell commands in python. Always works fine.

In streamlit its a total fail nothing works most things return โ€˜0โ€™ not the result of the command.
Or the server seems to lock up.
a simple test:

import streamlit as st
import subprocess
res = subprocess.call('ls l', shell=True)
st.write(res)

When I run this, I get the following message in the terminal: ls: cannot access 'l': No such file or directory

You can run this to get the output:

import streamlit as st
import subprocess
res = subprocess.check_output(['ls', '-l'])
st.write(res)

What are you actually trying to do with your code, outside of this trivial example?

Best,
Randy

Oops sorry - gremlin when I copied the code. the basic python code is:

import subprocess
res = subprocess.call('ls -al', shell=True) 
print(res)

which gives an extended directory listing.

so in streamlit:

import streamlit as st

import subprocess
res = subprocess.call('ls -al', shell=True) 
st.write(res)

I think streamlit just gets the return code (0) so I think I can use the more advanced subprocess
stuff to get stdout, stderr etc.

from subprocess import Popen, PIPE

p = Popen(['command', 'arg1'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
# filter data returned by p etc

interestingly this simple os example seems to work ok:

import os
import streamlit as st

files = os.listdir()
for x in files:
 st.write(x)

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.