5

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?

5
  • a python file is a text file with the .py extension. So how does double clicking a text file in Windows make it execute it? It will just open it with whatever editor you have configured by default to open .py files. It's unclear how did you make it execute in Windows. Do you have a .bat file that calls the python executable and passing your file as parameter? Commented Mar 10, 2022 at 16:58
  • @SembeiNorimaki on my Windows machine Python is the default application to open .py files. So it will open and run them in a command shell. Commented Mar 10, 2022 at 17:02
  • Then create a Desktop launcher and make it call python askubuntu.com/questions/437145/… Commented Mar 10, 2022 at 17:03
  • 1
    @SembeiNorimaki No need to sound so incredulous about it. Clearly it wasn't obvious to the OP that double clicking wasn't going to know to run the script in python. This is the sort of approach that could put a beginner off - people need to feel they can ask questions when stuck. Commented Mar 10, 2022 at 22:06
  • I don't know if your question is still open, but I posted a suggestion anyway for future visitors. See below and also python-forum.io/thread-45643.html Commented Oct 3 at 16:24

5 Answers 5

5

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.

Sign up to request clarification or add additional context in comments.

Comments

4

first get your desktop mint with this command :

cd /home/<USER>/Desktop

after enter just enter command:

python3 myscript.py

Comments

0

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.

Comments

0

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

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

I don't know if your question is still open, but I post a suggestion anyway for future visitors.

What helped for me:

-right click on myscript.py
-choose 'open with' --> other application
-in the open field below, give the command
gnome-terminal -- bash -c "/usr/bin/python3 %F; bash"
-click 'Set as Default'
-click 'Ok'

In this way a .py file always runs AND you can see the output of a print command (without having to create a launcher or a separate bash file).
More info: python-forum.io/thread-45643.html

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.