Search code examples
pythonsocketsbatch-fileserverpycharm

PyCharm opening when I run .py file from batch file


I have created a batch file that is opening a Python Code that I programmed in PyCharm. But when I open the batch file that is opening my Python Code, then PyCharm randomly opens and I don't know why.

For example:

If I have created a Python Code that says print('Hello World') then when I have made a batch file that should open that code then the batch file should just say Hello World. But in my case, instead nothing happens and PyCharm is opening instead. And I don't know why. I even tried to do copy the same code into Visual Studio Code, a another programming program. I did the same code, created a batch file that should open the Python File/Script and even then PyCharm opens even if the code I created comes from a another program.

So the code looks like this:

import socket

HOST = '127.0.0.1'
PORT = 3759

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((socket.gethostname(), PORT))
s.listen(5)

while True:
    clientsocket, address = s.accept()
    print(f"Connection from {address} has been established!")
    clientsocket.send(bytes('Welcome to the server!', "utf-8"))

and the batch file goes like this:

@ECHO OFF
color 0a
"c:\Users\golle\PycharmProjects\Test\server.py

And I know just to notify that there isn't any errors. So the code I created is opening a server and the batch file is opening that server by going into its folder and starting the server.py file which contains the code for the server. And when I click on the batch file, then PyCharm opens and the server isn't opening.

Because what should happen is that when I click on the batch file then a server should come up without PyCharm opening. And when I login to the server thru a client.py then the server window that I opened thru the batch file should say:

Connection from {address} has been established!

But it isn't working.

The weird thing is that it worked just like I wanted it to a couple of days ago, but yesterday when I was gonna try it again, it didn't work and just like I said, PyCharm is randomly opening.

btw: I created the server with help from a video from this link: https://www.youtube.com/watch?v=Lbfe3-v7yE0


Solution

  • I think the reason Pycharm is randomly opening is because on your Windows computer, that's the default application to open for Python files. Running "c:\Users\golle\PycharmProjects\Test\server.py" is essentially the same as double clicking on it from the file explorer.

    What you need to do is have python execute the file, instead of trying to run the file directly.

    It would look something like this:

    python "c:\Users\golle\PycharmProjects\Test\server.py"
    

    This assumes that python is in your PATH.