擅长:python、mysql、java
<p>从Django内部运行<code>pip</code>可能不是一个愚蠢的主意,但是如果你愿意,那么就把自己击倒:<a href="https://docs.djangoproject.com/en/2.0/howto/custom-management-commands/" rel="nofollow noreferrer">djangos custom-management-commands</a>和<a href="https://docs.python.org/3.6/library/subprocess.html" rel="nofollow noreferrer">pythons ^{<cd2>} module</a></p>
<p>另请参见<a href="https://stackoverflow.com/questions/89228/calling-an-external-command-in-python">this post</a>关于子流程模块。你知道吗</p>
<pre><code>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 ...
</code></pre>