0

Let me know if this question is obvious enough and there are also duplicates of this question which I am failed for search beforehand.

I am trying to run pip install requirements/dev.txt from Django management command. I have a logic which is, do not try to run the script pip ... if the cache is unchanged. I am trying to figure out how to run this command from the DMC?

Usage:

python manage.py install_prepreqs
Cache is unchanged, skipping.... 

install_prepreqs.py

# I want to run `pip install requirements/dev.txt` with some additional logic. 
Community
  • 1
  • 1
A.J.
  • 6,664
  • 10
  • 55
  • 74
  • (1) It's `pip install -r requirements/dev.txt` with `-r` (2) What bad about this command? It became kind of a standard if you don't want to use a `setup.py`. – Klaus D. Apr 16 '18 at 15:01
  • Command is not a problem, running the external command is from Django management command is the question. – A.J. Apr 16 '18 at 15:06

1 Answers1

0

It is probably not a goot idea to run pip from inside Django, but if you wish then knock yourself out: djangos custom-management-commands and pythons subprocess module

See also this post about the subprocess module.

from django.core.management.base import BaseCommand, CommandError
import subprocess

class Command(BaseCommand):
    def handle(self, *args, **options):
        # check your precondition logic here
        # ... your code goes here ...

        if my_condition == True:
            subprocess.run(["ls", "-l"])

        # ... some more code goes here if you wish ...
Ralf
  • 13,322
  • 4
  • 31
  • 55