Python: Django: How to run django app using a python script


I’ve a python file named "" which actually checks the ip of user and run the django server on that port accordingly.
The method I used there was to create a batch file using python and execute it later.

import socket
import subprocess
x = socket.gethostbyname(socket.gethostname())
x = str(x)
with open("run.bat", "w") as f:
    f.write(f' runserver {x}:0027')[r'run.bat'])

But this method is not very effective. I want a way like:
import something"")

or something accordingly

Kindly Help me doing this

I’ve seen Django’s
Can I edit this file (the sys.argv section) in such a way that it automatically runs a server on 192.168.x.x:8000 just by clicking on

Please help


I think the method you’re trying to reach is to run a shell command inside a python script, which can be achieved by using the os.system() function.

You can use it in your code as the following:

import socket
import subprocess
import os
x = socket.gethostbyname(socket.gethostname())
x = str(x)
os.system(f' runserver {x}:0027')

Answered By – Bernardo Duarte

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published