Author Topic: .py script What exactley happens when the script is marked executable in Linux?  (Read 471 times)

0 Members and 1 Guest are viewing this topic.

Offline JesterTopic starter

  • Frequent Contributor
  • **
  • Posts: 885
  • Country: ca
I'm a hardware guy, but dabble with firmware as required (mostly C), and this is my first look at anything python related.

I'm working on a project based on this ->  https://github.com/AlexmagToast/LinuxCNC_ArduinoConnector

Everything is working as expected.

The .py script runs on a LinuxCNC machine and communicates with an Arduino via USB, when it receives a message from the Arduino it passes it on to LinuxCNC via the HAL interface.

The .py script requires small tweaks to match the number and nature of the messages to/from the Arduino when the Arduino code is tweaked.  All good.

The setup instructions for this scheme get the user to do the following after a change:

sudo chmod +x arduino-connector.py                                                         # my understanding is that the +x changes the permissions to make the file executable?
sudo cp arduino-connector.py /usr/bin/ arduino-connector                        #  my understanding is that this just copies the file to /usr/bin and drops the .py extension

My questions:
1) Does marking this as executable compile the script into some sort of binary executable code that is then executed like any other executable file?
1a) If so can I assume that the script runs much faster when marked executable?
2) What's the purpose of dropping the .py extension?

 

Online tszaboo

  • Super Contributor
  • ***
  • Posts: 7864
  • Country: nl
  • Current job: ATEX product design
1) Does marking this as executable compile the script into some sort of binary executable code that is then executed like any other executable file?
No, it doesn't. You cannot execute python scripts like this
1a) If so can I assume that the script runs much faster when marked executable?
No.
2) What's the purpose of dropping the .py extension?
No idea, doesn't make sense.

If you have a script, which is a few kilbyte txt file with, which is readable, with a py extension, you cannot make it executable just like that.
You have to call "python myscrpt.py" to run it, and you are running the python interpreter.
There are some programs that will package the interpreter together with your script and make a linux "exe" out of it. The typical size in my experience is 30 MB or so for a basic program.
 
The following users thanked this post: Jester

Offline madires

  • Super Contributor
  • ***
  • Posts: 8096
  • Country: de
  • A qualified hobbyist ;)
The file will be still a script. When the executable bit is set the computer will check if the file is a real executable or a script. If it's a script it will look for a shebang (#!) at the beginning and then calls that interpreter. Dropping the .py extension isn't necessary, could be for aesthetics.
 
The following users thanked this post: Jester

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 11687
  • Country: us
    • Personal site
The reason for renaming is usually to give the tool more native feel. You call it like a normal executable and don't think about it as a script. This only really matters when it is something you are expected to call by hand.

Just setting exec bit is not enough, it is still a bunch of text. But the shell would read the first line of that text and check if there is a Shebang line - https://en.wikipedia.org/wiki/Shebang_(Unix)

For Python it is usually "#!/usr/bin/env python". If it is present, it will run the specified interpreter on that script, making it appear as if it was a compiled program.
« Last Edit: August 23, 2024, 04:07:51 pm by ataradov »
Alex
 
The following users thanked this post: Jester

Offline zapta

  • Super Contributor
  • ***
  • Posts: 6281
  • Country: us
What ataradov said. If you want to make it really looking like an independent binary that doesn’t need Python installation, look here https://pyinstaller.org/en/stable/
 
The following users thanked this post: Jester

Offline JesterTopic starter

  • Frequent Contributor
  • **
  • Posts: 885
  • Country: ca
The machine this script is running on is used for LinuxCNC and I don't want to add any unnecessary burden to the processor. FWIW PC is a HP Elite 8300 I don't have a sense if this script has negligible effect on resources? When I run the task manager with a CNC program running the CPU hovers at less than 5% CPU and 20% memory usage, so my hunch is negligible effect?

The python script (attached), has a fair number of if statements that execute code only if enabled, see a couple of examples below

I'm not using many of these features, so much of the code will be bypassed via the if statements by the python script processor. I'm contemplating removing the unused features entirely including those if statements not only to make the script more readable but to stream line it a bit.

Thoughts and comments please.



Code: [Select]
if BinSelKnob:
if SetBinSelKnobValue[0] == 0:
for port in range(BinSelKnobPos):
c.newpin("binselknob.0.{}".format(port), hal.HAL_BIT, hal.HAL_OUT)
else :
c.newpin("binselknob.{}.{}" .format("0","out"), hal.HAL_S32, hal.HAL_OUT)


# setup Digital LED halpins
if DLEDcount > 0:
for port in range(DLEDcount):
c.newpin("dled.{}".format(port), hal.HAL_BIT, hal.HAL_IN)
oldDLEDStates[port] = 0
 

Offline zilp

  • Frequent Contributor
  • **
  • Posts: 329
  • Country: de
Just setting exec bit is not enough, it is still a bunch of text. But the shell would read the first line of that text and check if there is a Shebang line - https://en.wikipedia.org/wiki/Shebang_(Unix)

The shell is not involved in this. Rather, it's the other way around: In the case of a shell script, that is how the shell is invoked as the interpreter for the script. The handling of interpreter invocation via shebang line is done by the kernel, just like any other executable file format.
 
The following users thanked this post: Jester

Offline xvr

  • Frequent Contributor
  • **
  • Posts: 407
  • Country: ie
    • LinkedIn
Quote
The machine this script is running on is used for LinuxCNC and I don't want to add any unnecessary burden to the processor.
Your script does some initialization and then waits for a command from the Arduino (via the serial port). In this state, it will not consume any CPU resources.
The Python interpreter will consume some memory, but it should not be too much. In any case, you can find out the memory consumption with the ps command.
 
The following users thanked this post: Jester

Online brucehoult

  • Super Contributor
  • ***
  • Posts: 4413
  • Country: nz
1) Does marking this as executable compile the script into some sort of binary executable code that is then executed like any other executable file?
No, it doesn't. You cannot execute python scripts like this

Yes, you can.

Code: [Select]
bruce@i9:~/programs$ cat mypy
#!/usr/bin/env python3
import sys

def fact(n):
  if n == 1:
    return n
  else:
    return n * fact(n-1)

n = int(sys.argv[1])
print(f"Hi from Python, fact({n}) = {fact(n)}")
bruce@i9:~/programs$ chmod +x mypy
bruce@i9:~/programs$ ./mypy 10
Hi from Python, fact(10) = 3628800
bruce@i9:~/programs$

Quote
2) What's the purpose of dropping the .py extension?
No idea, doesn't make sense.

It makes it feel like a native program to use.
 
The following users thanked this post: Jester


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf