.py script  What exactley happens when the script is marked executable in Linux? - Page 1
Forum section: electronics / projects

author: Jester
author_rank: Frequent Contributor
timestamp: August 23, 2024, 02:24:10 pm
content: 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_ArduinoConnectorEverything 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 extensionMy 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?

author: tszaboo
author_rank: Super Contributor
timestamp: August 23, 2024, 02:36:52 pm
content: 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 this1a) 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.

author: madires
author_rank: Super Contributor
timestamp: August 23, 2024, 02:46:27 pm
content: 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.

author: ataradov
author_rank: Super Contributor
timestamp: August 23, 2024, 02:48:45 pm
content: 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.

author: zapta
author_rank: Super Contributor
timestamp: August 23, 2024, 03:55:04 pm
content: What ataradov said. If you want to make it really looking like an independent binary that doesn’t need Python installation, look herehttps://pyinstaller.org/en/stable/

author: Jester
author_rank: Frequent Contributor
timestamp: August 23, 2024, 06:02:39 pm
content: 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 belowI'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:if BinSelKnob:if SetBinSelKnobValue == 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 halpinsif DLEDcount > 0:for port in range(DLEDcount):c.newpin("dled.{}".format(port), hal.HAL_BIT, hal.HAL_IN)oldDLEDStates = 0

author: zilp
author_rank: Frequent Contributor
timestamp: August 23, 2024, 06:27:17 pm
content: Quote from: ataradov on August 23, 2024, 02:48:45 pmThe 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.

author: xvr
author_rank: Frequent Contributor
timestamp: August 23, 2024, 08:45:29 pm
content: QuoteYour 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 thepscommand.

author: brucehoult
author_rank: Super Contributor
timestamp: August 23, 2024, 11:21:41 pm
content: Quote from: tszaboo on August 23, 2024, 02:36:52 pmYes, you can.Code:bruce@i9:~/programs$ cat mypy#!/usr/bin/env python3import sysdef fact(n):if n == 1:return nelse:return n * fact(n-1)n = int(sys.argv)print(f"Hi from Python, fact({n}) = {fact(n)}")bruce@i9:~/programs$ chmod +x mypybruce@i9:~/programs$ ./mypy 10Hi from Python, fact(10) = 3628800bruce@i9:~/programs$QuoteIt makes it feel like a native program to use.