如何在 Linux 上运行 Python 脚本?

发布于 2025-01-13 03:10:09 字数 372 浏览 0 评论 0原文

我是 Windows 用户。我目前正在一台 Linux Mint 机器上工作。我尝试像在 Windows 上一样运行 python 脚本:我在桌面上创建了一个 myscript.py 文件并双击它,但它只会在文本编辑器中打开它。

测试脚本非常简单,只有一行:

input("Success!")

然后我尝试将默认应用程序更改为 usr/bin/python3,但是当我双击该文件时没有任何反应。

我还尝试打开一个新终端,但单击该图标只会关注当前打开的终端,我无法使用该终端,因为它已用于记录其他内容。

那么,如何才能像在 Windows 上一样,通过单击按钮来运行一个或多个 Python 脚本(每个脚本都有自己的终端)呢?

I'm a Windows user. I'm currently working on a Linux Mint machine. I tried running a python script the same way I would on Windows: I created a myscript.py file on the desktop and double clicked it, but it would just open it in a text editor.

The test script is very simple, there is only one line:

input("Success!")

Then I tried changing the default application to usr/bin/python3, but then nothing happens when I double click the file.

I also tried to open a new terminal, but clicking the icon will just focus on the currently open terminal, which I can't use because it's already in use for logging something else.

So how can I run one or several Python scripts each with their own terminal by the click of a button, the way I can on Windows?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沉默的熊 2025-01-20 03:10:09

方法一:某些为 Windows(不是最佳选择)

告诉文件管理器在 python /usr/bin/python3 中打开以 .py 结尾的文件(注意斜杠在开始时)。
然后双击它。

方法二(不是最佳选择)

打开 shell 并输入 python3 «脚本名称»

方法三(首选方法)

  • 添加 #!/usr/bin/python3 作为脚本的第一行。
  • 使文件可执行chmod +x «脚本名称»(或右键单击文件管理器)。

旁注:

使用此方法最好删除 .py,因为不需要它,并且会泄漏实现:脚本的所有调用者都需要了解编程语言。

Method one: some as Windows (not the best option)

Tell the file-manager to open files ending in .py in python /usr/bin/python3 (note the slash at the start).
Then double click it.

Method two (not the best option)

Open a shell and type python3 «script name»

Method three (the preferred method)

  • Add #!/usr/bin/python3 as the first line of the script.
  • Make the file executable chmod +x «script name» (or right click in your file-manager).

side note:

With this method it is better to remove the .py, as it is not needed, and leaks the implementation: All callers of the script need to know the programming language.

零崎曲识 2025-01-20 03:10:09

首先使用此命令获取桌面薄荷:

cd /home/<USER>/Desktop

输入后只需输入命令:

python3 myscript.py

first get your desktop mint with this command :

cd /home/<USER>/Desktop

after enter just enter command:

python3 myscript.py
流心雨 2025-01-20 03:10:09

从 GUI 运行脚本可能没有打开终端供您查看其输出。

要运行 python,你需要一个终端。例如,要打开一个程序,您只需打开开始菜单并输入“终端”,然后打开建议的程序之一。

一旦你有了终端,你需要调用程序并给它一些运行参数,在这种情况下 python /path/to/my/file/myscript.py 应该足够了。

如果它不起作用,可能是系统无法识别python。在这种情况下,请尝试运行 python3 /path/to/my/file/myscript.py 。

如果这不起作用,您可能还没有安装 python。您可以从 GUI 应用程序“软件管理器”或使用命令 sudo apt install python 来安装它。如果这不起作用,您可能需要使用 sudo apt update 更新源。

我还尝试打开一个新终端,但单击该图标只会关注当前打开的终端,我无法使用该终端,因为它已用于记录其他内容。

根据终端模拟器(您用来访问终端和 shell 的程序),您应该能够在窗口顶部的“文件”>“文件”下的栏中打开一个新选项卡。新标签> [任何配置文件] 或使用快捷键 ctrl + shift + t。这样,您将在同一个终端模拟器中单独运行多个选项卡。 Linux Mint Cinnamon 的默认终端模拟器 Gnome-Terminal 具有此功能。

您还应该能够始终使用快捷键 ctrl + alt + t 打开新终端。

在我看来,这是运行任何 python 脚本的最简单方法。如果您确实想避免从终端运行它们,您可以选择添加桌面启动器并使用您在终端中输入的命令对其进行配置。

为此,右键单击桌面,然后单击“在此处创建新启动器”。应该会打开一个小窗口,您可以在其中输入要显示的名称、要执行的命令、启动器的注释、设置图标并使其从终端启动。启用“在终端中启动”并将类似 python3 /path/to/my/file/myscript.py 的内容放入命令字段中。

一切设置完毕后,创建启动器并单击它来执行脚本。 (这适用于任何命令)。

请记住,Python 脚本完成后您的终端将关闭。如果它立即关闭,则您的 python 脚本立即完成。

塔尔;博士:
命令:python3 /path/to/my/file/myscript.py,
在终端中运行它(如果需要,打开多个选项卡或窗口),
或者制作一个桌面启动器。


这是我第一次在 Stack Exchange 上回答帖子,我希望它有帮助,但不要太详细。如果您有任何疑问,我很乐意为您提供帮助。

Running the script from GUI has probably not opened a terminal for you to see it's output.

To run python you need a terminal. To open one you could for example just open the start menu and type 'terminal', then open one of the suggested programs.

Once you have your terminal you need to call the program and give it some arguments to run with, in this case python /path/to/my/file/myscript.py should be enough.

If it didn't work it is possible that python is not recognized by the system. In that case try running python3 /path/to/my/file/myscript.py instead.

If that didn't work as well, you might not have installed python yet. You can install it from the GUI App 'Software Manager' or by using the command sudo apt install python. If that didn't work, you might need to update your sources using sudo apt update.

I also tried to open a new terminal, but clicking the icon will just focus on the currently open terminal, which I can't use because it's already in use for logging something else.

Depending on the terminal emulator (the program that you are using to access the terminal and shell) you should be able to open a new tab in the bar on the top of the window under File > New Tab > [any profile] or alternatively by using the shortcut ctrl + shift + t. That way you will have multiple tabs in the same terminal emulator running separately. The default terminal emulator Gnome-Terminal of Linux Mint Cinnamon has this functionality.

You should also be able to always open a new terminal using the shortcut ctrl + alt + t.

In my opinion this is the easiest way to run any python script. If your really want to avoid running them from terminal, an option you have is to add a desktop launcher and configure it with the command you would put into a terminal.

For that, right click on the desktop and click 'Create new launcher here'. A small window should open where you can put in a name to be displayed, a command to be executed, a comment for the launcher, set an icon, and make it launch from terminal. Enable Launch in Terminal and put something like python3 /path/to/my/file/myscript.py into the command field.

When everything is set up create the launcher and click it to execute your script. (This works for any command).

Keep in mind that your terminal will close once the python script is finished. If it closes instantly, your python script finished instantly.

Tl;Dr:
Command: python3 /path/to/my/file/myscript.py,
run this in terminal (if needed open multiple tabs or windows),
or make a desktop-launcher.


This is the first time i have answered a post on Stack Exchange, I hope it was helpful and not too much detailed. If you have any questions, I would be happy to help.

っ〆星空下的拥抱 2025-01-20 03:10:09

打开新终端 (ctrl+alt+t) 并写入 python3 [您的文件路径]/[文件名]

Open new terminal (ctrl+alt+t) and write python3 [your file path]/[file name].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文