pythonpipcommand-lineenvironment-variablessubprocess

python app does not work when launched from subprocess


I have installed an app with pip in editable mode (App). I am using App in a script via subprocess. App should make a subfolder within the folder containing an input csv, and then add 4 more CSV files to this subfolder. When I call this app via subprocess in the script, it runs for a suspiciously short time, and then completes. There are print statements from the script before the subprocess call, but no subfolder, which implies there is an issue with my subprocess call. Can anyone see any issues?

I am running the script from a USB, in a conda virtual env. I'll try running this from my C drive instead and see if this works. I have already successfully run App from the command line by copying the command fed to subprocess.

Can anyone see any obvious problems?

Thanks!

Script:

print (f'running App on {query_filepath}')
subprocess.run(f"App -csv {query_filepath} -e -sd",
               shell = True)
raise ValueError()

Output (with none of the expected subdirectories from App):

running App on d:\databases\formatting/app_query.csv
Traceback (most recent call last):

  File C:\Anaconda3\envs\proteomics\lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File d:\databases\formatting\parse_id_mapping.py:83
    raise ValueError()

ValueError

EDIT: I also tried the following unsuccessfully:

subprocess.run(['App', '-csv', query_filepath, '-e', '-sd'],
               shell = True)

Solution

  • The problem was that App was running into an error. In order to prevent data overwriting, it checks to see if the subfolder (containing the results CSVs mentioned in my question) already exists, and raises an error if it does asking the user to move or rename the subfolder. The subfolder already existed when I called the app, and so it raised the error to stderr and I wasn't checking or catching that. I think the simplest fix is to have check = True in the subprocess call to confirm it is completing it properly.

    This solution was mainly due to @axerotol who linked me to an answer where they were discussing subprocess calls in detail. This led me to assign the subprocess output to a variable which I then inspected in Spyder, which immediately highlighted the issue.