0

I have multiple commands and would like to run them using os.system() and in a .py script. however, i have realized that some of the commands do not get executed, and I cannot figure out what the problem is. this is how the content of my .py script look like:

import os

def main():
    commands= ['python -m venv test_venv & '
    'source ./test_venv/bin/activate &'
    'pip install -U pip setuptools wheel & '
    'pip install -U spacy & '
    'python -m pip freeze  > packages_information/spacy_packages.txt & '
    'python -m pip install -r packages_information/spacy_packages.txt & '
    'python -m spacy download de_core_news_sm & '
    'python -m pip freeze  > packages_information/de_core_news_sm_spacy_requirements.txt & '
    'python -m pip install -r  packages_information/de_core_news_sm_spacy_requirements.txt & '
    'pip uninstall de_core_news_sm & '
    'python -m spacy download de_dep_news_trf & '
    'python -m pip freeze  > packages_information/de_dep_news_trf_spacy_requirements.txt & '
    'python -m pip install -r  packages_information/de_dep_news_trf_spacy_requirements.txt &'
    'pip uninstall de_dep_news_trf & '
    'deactivate test_venv &'
    'rm -r test_venv']

        for command in commands:
    os.system(command)

    # clears out the files containing '.txtpython' extentions
    directory = os.curdir
    files_in_directory = os.listdir()

    filtered_files = [file for file in files_in_directory if file.endswith(".txtpython")]

    for file in filtered_files:
        path_to_file = os.path.join(directory, file)
        os.remove(path_to_file)

if __name__ == '__main__':
    main()
    

in particular the uninstallation of Spacy and the delete/remove of the virtual environment do not get executed.

zara kolagar
  • 881
  • 3
  • 15
  • Your code's indentation is screwed up. Can you fix it? I find it easier to create code-blocks using three backticks ( ` ) on the lines before and after my code than to use spaces. – Pranav Hosangadi May 05 '21 at 16:00
  • One possible problem is that you have an ampersand at the end of each command. IDK what OS you are using, but in unix-like shells, `&` at the end of a command causes it to run in the background, so you might be trying to run the delete/remove command parallelly with the others and that would cause it to fail. https://stackoverflow.com/questions/13338870/what-does-at-the-end-of-a-linux-command-mean – Pranav Hosangadi May 05 '21 at 16:03

0 Answers0