Python Forum
Is possible to run the python command to call python script on linux?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is possible to run the python command to call python script on linux?
#1
I have a method to call the python script using subprocess.popen like:

   

it's somehow working well on windows (running with docker)
but in linux, when the toggle was on (meaning status = 1), the script was not called.
is there anyway to call the python script by code instead of running it manual by create new .sh file and call it on linux terminal? Huh
Reply
#2
This little Python:

#! /usr/bin/python3
#This program says hello and asks for your name.
print('This is Python speaking: Hello World')
print('What is your name?') #Ask for their name.
myName = input()
print('It is good to meet you,' + myName)
can run in bash by creating a bash script like this:

Quote:echo "This is bash speaking, I am now starting hello_world.py"
myPython/helloWorld.py

Make sure the script and the bash script are executable. I have the path for my bash scripts in my $PATH, /home/pedro/myBashscripts, so bash always finds them.

Don't forget the shebang: /usr/bin/python3 or bash and the computer go very weird!
Reply
#3
(Jan-23-2024, 09:19 AM)Pedroski55 Wrote: This little Python:

#! /usr/bin/python3
#This program says hello and asks for your name.
print('This is Python speaking: Hello World')
print('What is your name?') #Ask for their name.
myName = input()
print('It is good to meet you,' + myName)
can run in bash by creating a bash script like this:

Quote:echo "This is bash speaking, I am now starting hello_world.py"
myPython/helloWorld.py

Make sure the script and the bash script are executable. I have the path for my bash scripts in my $PATH, /home/pedro/myBashscripts, so bash always finds them.

Don't forget the shebang: /usr/bin/python3 or bash and the computer go very weird!

I'm not quite understand. It mean i need to create new bash script and run it on linux terminal?
Reply
#4
Save the following in your bash script folder as, for example, helloWorld.sh

Quote:# this is a bash script
echo "This is bash speaking, I am now starting helloWorld.py"
# put the path to your Python programme here
myPython/helloWorld.py

Now run the helloWorld.sh in bash as below, and the Python programme helloWorld.py will run.

Below is what you see in bash when you run helloWorld.sh:

Quote:pedro@pedro-HP:~$ helloWorld.sh
This is bash speaking, I am now starting helloWorld.py
This is Python speaking: Hello World
What is your name?
Peter
It is good to meet you,Peter
pedro@pedro-HP:~$

Like I said, helloWorld.sh and helloWorld.py must both be executable.

Don't forget the shebang at the top of your Python programme: #! /usr/bin/python3
cuten222 likes this post
Reply
#5
Yes, it is possible to run a Python script using the python command on Linux.
Use this basic syntax - python script.py
You have to replace script.py with the name of your Python script
Reply
#6
Yes, it is possible to run Python scripts from the command line on Linux using the python command. Here's a basic example:

1.Open a terminal.
2.Navigate to the directory where your Python script is located, using the cd command:
cd path/to/your/python/script

3.Run the Python script using the python command:
python your_script.py

If you're using Python 3, you might need to use the python3 command:
python3 your_script.py

Replace your_script.py with the actual name of your Python script.

In some cases, you might want to make the script executable and run it directly without specifying the Python interpreter. To do this, you need to add a shebang line at the beginning of your Python script. Here's an example:

1.Open your Python script in a text editor.
2.Add the following line at the very beginning of the script:
#!/usr/bin/env python3

This line tells the system to use the Python 3 interpreter.

3.Save the script.
4.Make the script executable using the chmod command:
chmod +x your_script.py

Now, you can run the script directly:
./your_script.py

Make sure to replace your_script.py with the actual name of your Python script.
Reply
#7
Safety note:

shell=True should not be used under any circumstances.

Safer:
from subprocess import Popen


Popen(["python", "your_app.py"])
Safer with the help of shlex:
import shlex
from subprocess import Popen


command = shlex.split("python your_app.py")
print(command)

Popen(command)
If you make your script executable, then add the shebang, which runs the right interpreter. e.g., you can also run Perl, PHP, Ruby and many other interpreted languages with the help of the shebang.

The shebang for bash:
#!/bin/bash
The shebang for python:
#!/usr/bin/env python3
The shebang for PHP:
#!/usr/bin/env php
Pedroski55 likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems passing arguments containing spaces to bash script and then on to python kaustin 6 416 Apr-03-2024, 08:26 PM
Last Post: deanhystad
  ChromeDriver breaking Python script genericusername12414 1 321 Mar-14-2024, 09:39 AM
Last Post: snippsat
  using PowerShell from Python script for mounting shares tester_V 8 548 Mar-12-2024, 06:26 PM
Last Post: tester_V
  No Internet connection when running a Python script basil_555 8 663 Mar-11-2024, 11:02 AM
Last Post: snippsat
Question Running Python script through Task Scheduler? Winfried 8 524 Mar-10-2024, 07:24 PM
Last Post: Winfried
  python script is hanging while calling a procedure in database prasanthi417 4 525 Jan-17-2024, 02:33 PM
Last Post: deanhystad
  run part of a script with a different version of Python jdog 2 461 Jan-09-2024, 08:49 PM
Last Post: jdog
  How to check if Skype call is connected or disconnected in Python? boral 1 406 Dec-28-2023, 08:31 AM
Last Post: buran
  Help Running Python Script in Mac OS emojistickers 0 351 Nov-20-2023, 01:58 PM
Last Post: emojistickers
  python command interface cwc2 5 1,230 Sep-20-2023, 05:19 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020