why python3 can't execute some linux command?

  • Last Update :
  • Techknowledgy :

you can try use subprocess that allows to save stdout and stderr too:

    import subprocess
    # # # define the command
    command = 'mjpg_streamer -i "input_uvc.so -d /dev/video0 -r 800x448" -o "output_http.so -p 8090 -w /usr/local/share/mjpg-streamer/www/"'
    # # # execute the command and save stdout and stderr as variables
    output, error = subprocess.Popen(command, universal_newlines = True, shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE).communicate()

Suggestion : 2

I am utterly baffled and mystified. I have been happily coding and running Python scripts from the terminal (and also running the Python interpreter). I went to bed a happy user of Python, but suddenly I cannot run any Python script from the shell.,PS: I am using Ubuntu 20.04 (Focal Fossa). I have tried rebooting, and sudo apt install -f --reinstall python3 python3.8 python3-minimal python3.8-minimal libpython3.8-minimal all to no avail.,After observing the output of cat -e hello.py, it seems the script contains a byte order mark near its shebang/hashbang which may have been inserted by some Windows software. This prevented the script from actually loading the Python interpreter, so it instead got executed by zsh, thus causing the error.,Since you are able to run the script using python3 hello.py, it is evident that the issue isn't with the Python installation or the symlinks. Instead the issue is with the script itself.

You can remove BOMs and other DOS-specific characters using dos2unix.

dos2unix hello.py # Install using `sudo apt install dos2unix`(
   if not already installed)

Or you can also use sed to remove BOMs. Since BOMs were present in UTF-8, their hexadecimal representation is EF BB BF.

sed - i '1s/^\xEF\xBB\xBF//'

How that byte order mark got there is a mystery, but at least now:

. / hello.py
Hello world!

Suggestion : 3

python3 is already installed by default in Ubuntu, I have added python3 to the command for the sake of generality with other Linux distributions. IDLE 3 is an Integrated Development Environment for Python 3. Open IDLE 3 and then open your Python script from the menu in IDLE 3 -> File -> Open., 1 Are you sure you do not have python3 installed on your linux box ? Several distros ship it but it is not the default python used to run scripts. – NaeiKinDus Jan 6, 2015 at 11:02 , Then you definitely have to install python 3. I see you're discussing that in the comments to the other answers. – Nemo Jan 6, 2015 at 14:50 ,In Windows IDLE 3 is bundled with the Python 3 installation file, and the instructions for opening a Python 3 script in IDLE 3 are the same.

You have registered for Ask Ubuntu Q&A, so I'll provide the command for Ubuntu. In Ubuntu open the terminal and type:

sudo apt install python3 idle3 ipython3

On many systems, "python" defaults to "python2", but you can run a script under python 3 simply by doing

python3 myscript.py


which python3

Also, if the script includes an appropriate shebang,

. / myscript.py

There is a difference from Linux to Windows. On Linux you would type like this:

python3 script.py

While on Windows (once you have Python installed) chances are you would type:

py - 3 script.py

Suggestion : 4

It’s because the Python language is not installed as python but python3 or python2 (in some older Ubuntu versions).,At some point in time in the distant past, Python was actually available as python package/executable. When Python released version 2, Ubuntu and other distros had to provide support for both Python version 1.x and 2.x.,Python 2 is no longer supported and Python 3.x is what you get on Ubuntu. The package is still named python3.,So, they named the newer Python version python2 to distinguish between the two. Other applications or libraries also specified python or python2 in their code.

How do you run a Python program in the Linux terminal? Like this, right?

python program.py

Ubuntu 18.04 had Python 2 as well but 20.04 and higher versions have Python 3 only. Still, which version(s) you have with:

type python python2 python3

If you don’t have any Python version installed, you may install Python version 3 with the following command:

sudo apt install python3

And you get the version details in the output:

[email protected]: ~$ python3--version
Python 3.10 .4

If you have to run a Python program, execute it like this:

python3 program.py