Run python programs in linux

How to run python script on terminal (ubuntu)?

I’m new with python, I’ve been learning for a few weeks. However now I’ve just changed my OS and I’m now using ubuntu and I can’t run any script on my terminal. I made sure to have the #!/usr/bin/env python but when I go to the terminal and type, for example python test.py the terminal shows an error message like this

I will just add a smal precision, if you use #!/usr/bin/env python you can just type ./test.py to execute your script as Terminal will take account of your header and use python to launch the script. But before you have to change execution permission by doing chmod +x test.py . I understand it can be a bit special for a beginner but you will appreciate this in the future 😉

5 Answers 5

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:

Читайте также:  Linux ppc64le что это

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

Источник

Run Python Scripts in Linux Command Line

This quick tip shows how to run Python programs from the Linux command line.

If you want to do something, it is pretty much guaranteed that there is a Python script for it. So, when you have a Python script, how do you run it?

The simplest way is to use the python or python3 command in the following manner:

Some distributions have used python2 as python and have an explicit command python3 for python3. So if you want Python 3 to be guaranteed, use python3 .

Method 1: Run it using python

The easiest method to run a Python script on any Linux distribution is by invoking the python command and provide it with the name of your Python script.

This will ensure that if the file’s contents are valid, it will be executed without any problems.

Method 2: Make Python script executable

You may have encountered executing bash scripts simply by typing out their relative/absolute path in the terminal. i.e. you never had to do a bash .sh like you do with Python scripts by typing python .py

In order to achieve this, a few things should be done first.

Читайте также:  Анонимный серфинг linux mint

If you might have noticed, the bash scripts that you can execute by simply typing out their relative/absolute path, they are «executable» files.

To make your Python script executable, run the following command in your terminal:

This should be it. Right? I have a file hello.py , let’s try running it.

$ cat hello.py print("Hello Linux Handbook!") $ chmod +x hello.py $ ./hello.py ./hello.py: 1: Syntax error: word unexpected (expecting ")")

Uh oh. What? Syntax error? But it is a valid Python program/script.

The reason why we got this error is because by making hello.py an executable file, all we did was tell the shell that «You can execute it», but not what interpreter/program to use when it is being executed.

My shell (bash) thought that it was a bash script. And hence, we got this error.

The solution to overcome this is pretty simple. Add the following line to the first line of your Python script.

The #! syntax is used mostly in scripts (where you need an interpreter). Now, the /usr/bin/env part means that we are calling env to find the python command from $PATH and execute it for us.

This essentially starts Python for us, instead of the user needing to invoke it manually every time. And, if you don’t automate these things, are you even scripting?

Another reason to use env in shebang is to make it portable. Some systems have the Python executable interpreter stored as /usr/local/bin/python , while some might have /usr/bin/python .

Now, let’s try executing hello.py once again, but with the necessary changes made to it.

$ sed -i '1 i\#!/usr/bin/env python\n' hello.py $ cat hello.py #!/usr/bin/env python print("Hello Linux Handbook!") $ ./hello.py Hello Linux Handbook!

Oh hey! It runs flawlessly now.

Читайте также:  Log files location in linux

Now that you know how to run Python programs from the terminal, how about learning to use Linux commands from Python scripts?

Источник

Оцените статью
Adblock
detector