当我从批处理文件运行 .py 文件时打开 PyCharm [英] PyCharm opening when I run .py file from batch file

查看:54
本文介绍了当我从批处理文件运行 .py 文件时打开 PyCharm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个批处理文件,它正在打开我编写的Python 代码PyCharm 中.但是当我打开正在打开我的 Python 代码批处理文件时,然后 PyCharm 随机打开,我不知道为什么.

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.

例如:

如果我创建了一个Python 代码 表示 print('Hello World') 然后当我制作了一个批处理文件应该打开那个代码然后批处理文件应该只是说Hello World.但就我而言,没有任何反应,而是打开 PyCharm.我不知道为什么.我什至尝试将相同的代码复制到另一个编程程序 Visual Studio Code 中.我做了同样的代码,创建了一个批处理文件,应该打开Python文件/脚本,即使这样即使我创建的代码来自另一个程序,PyCharm 也会打开.

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.

所以代码看起来像这样:

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"))

批处理文件是这样的:

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

我知道只是为了通知没有任何错误.所以我创建的代码是打开一个服务器,然后批处理文件通过进入其文件夹并启动server.py来打开该服务器包含服务器代码的文件.当我点击批处理文件时,PyCharm 会打开,但服务器没有打开.

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.

因为应该发生的是,当我点击批处理文件时,应该会出现一个服务器无需 PyCharm 打开.当我通过 client.py 登录到服务器时,服务器窗口我通过批处理文件打开应该说:

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!

但它不起作用.

奇怪的是,它就像几天前我想要的那样工作,但昨天当我要再试一次时,它不起作用,就像我说的,PyCharm 随机打开.

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.

如果您知道解决此问题的方法,我将不胜感激.谢谢!

If you know a way to fix this, I would gladly appreciate it. Thank you!

顺便说一句:我在此链接中的视频的帮助下创建了服务器:https://www.youtube.com/watch?v=Lbfe3-v7yE0

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

推荐答案

我认为 Pycharm 随机打开的原因是因为在您的 Windows 计算机上,这是为 Python 文件打开的默认应用程序.运行 "c:\Users\golle\PycharmProjects\Test\server.py" 本质上与从文件资源管理器双击它相同.

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.

您需要做的是让python执行文件,而不是尝试直接运行文件.

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

它看起来像这样:

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

这里假设 python 在你的 PATH 中.

This assumes that python is in your PATH.

这篇关于当我从批处理文件运行 .py 文件时打开 PyCharm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆