How to Run Python Script on Terminal (Ubuntu)

How to run python script on terminal (ubuntu)?

This error:

python: can't open file 'test.py': [Errno 2] No such file or directory

Means that the file "test.py" doesn't exist. (Or, it does, but it isn't in the current working directory.)

I must save the file in any specific folder to make it run on terminal?

No, it can be where ever you want. However, if you just say, "test.py", you'll need to be in the directory containing test.py.

Your terminal (actually, the shell in the terminal) has a concept of "Current working directory", which is what directory (folder) it is currently "in".

Thus, if you type something like:

python test.py

test.py needs to be in the current working directory. In Linux, you can change the current working directory with cd. You might want a tutorial if you're new. (Note that the first hit on that search for me is this YouTube video. The author in the video is using a Mac, but both Mac and Linux use bash for a shell, so it should apply to you.)

How can I run a Python script from Ubuntu Dash?

(Tested on 18.04)
Create this file in ~/.local/share/applications (for use only by your user) or in /usr/share/applications (for use by all users).

The file name must end in .desktop.

[Desktop Entry]
Name=hello.py
Exec=/path/to/hello.py
Type=Application
Categories=GTK;GNOME;Utility;

Note that the script runs in the background, and errors are swallowed unless you configure logging to a file within your script.

If you want it to run in a console, you could do this (the console will close when the script exits, though):

[Desktop Entry]
Name=hello.py
Exec=gnome-terminal -- /path/to/hello.py
Type=Application
Categories=GTK;GNOME;Utility;

More features are available if you want icons, to limit what desktop environments it runs under, etc - docs here: https://developer.gnome.org/integration-guide/stable/desktop-files.html.en

assumptions:

  • hello.py is executable by the current user
  • hello.py has a valid shebang
  • the path needs to be an absolute path (i.e. not relative and also not using shell expansions such as ~ or variables)

How to run python script in linux terminal console and continue using command line?

The script takes a long time to run, and I would like to continue
using the command line to perform other tasks.

It seems that you want to run said process in background, please try pasting following

python script.py &
echo "123"

it should start your script.py and then output 123 (without waiting until script.py ends)

how can I see the active processes that are running on the server to
see if a process is running?

Using ps command

ps -ef

will list all processes which you would probably want to filter to get interested one to you.

How to run python script from Linux terminal every hour?

Use the command watch on unix to run any command any set interval of time.

More information: https://en.wikipedia.org/wiki/Watch_(Unix)

(chose this way over cron because you specified in a terminal, and this would allow you to see the output in the terminal you start it from)

Ways to run python script in the background on ubuntu VPS and save the logs

I could not understand what you mean "the first log". Maybe the first line of logs?

To run something in the background when SSH connection is closed, I prefer Linux screen, a terminal simulation tool that help you run your command in a sub-process. With it, you could choose to view your output any time in the foreground, or leave your process run in the background.

Usage (short)

screen is not included in most Linux distributions. Install it (Ubuntu):

$ sudo apt-get install screen

Run your script in the foreground:

$ screen python3 my_script.py

You'll see it running. Now detach from this screen: Press keys Ctrl-A followed by Ctrl-D. You'll be back to your shell where you run previous screen command. If you need to switch back to the running context, use screen -r command.

This tool supports multiple parallel running process too.

Something weird

I've tried to redirect stdout or stderr to a file with > or >> symbol. It turned out in failure. I am not an expert of this either, and maybe you need to see its manual page. However, I tend to directly write to a file in Python scripts, with some essential output lines on the console.



Related Topics



Leave a reply



Submit