mmkw43
Posts: 768
Joined: Tue Dec 24, 2013 6:18 pm

Interpreter ?

Tue May 11, 2021 2:59 pm

I need to start asking basic questions when I run across things I really don't get. Writing code is a hobby for me, so not really a pro. I'm an older guy that is just keeping my brain busy.

So what exactly is the python interpreter? /usr ....../python3.7

https://docs.python.org/3.8/tutorial/in ... nterpreter

What's the difference (if any) between that and running
python3.7 myscript.py ?

What does interpreter mean? -- thanks

edit -- or is just a fancy way to say "python" ?

User avatar
jahboater
Posts: 7079
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: Interpreter ?

Tue May 11, 2021 3:20 pm

People usually program computers in a simple "english like" language that is good for humans to read and write.
Such a language is Python, other common ones are C, C++, Fortran, Bash, Rust and many many others.

The computer does not understand english. It understands a series of binary numbers. These binary numbers encode the instruction (add, subtract, move, compare etc) and the operands the instruction works on, which are called registers or memory locations.
There is a human readable representation of these instructions, called assembler, that a few people program in.

Anyway, languages such as Python or C must be translated to the binary language understood by the computer processor.
This is done (broadly speaking) in two ways: interpretation or compilation.
Python is interpreted, C is compiled.

A compiler translates the program and leaves an executable file, that the user may run many times. Most of the commands you run on the machine such as ls, cp, cat, etc are compiled C programs. The compilation is done once only. And because its not done when the user runs the program, there is plenty of time for the compiler to optimize the program for you. For these reasons, compiled languages such as C are very fast.

An interpreter takes the Python "english like" source and translates it "on-the-fly", usually line by line, into the machine instructions and then executes them immediately. There is no separate compilation stage and the results appear immediately.
Some interpreters allow you to type in commands in a console and they will execute them directly. (called REPL). So python makes a powerful desk calculator.

You would normally place the Python program in a text file and pass that to the interpreter.
So you could say "python3 myprog.py" and it will run the whole program for you.

To avoid prefixing your program with "python3" every time you may add a line at the start of the Python text file

#!/usr/bin/python3

and the system will start the Python interpreter for you.

As you can imagine the time taken to load the large interpreter and do the translation is included every single time you run the program! There is obviously no time for significant optimization. So interpreted languages run much slower than compiled ones. A few interpreted languages (not python) do some limited optimization and partial compilations each time, and for repetitive programs these can be much faster (called JIT).

User avatar
scruss
Posts: 4108
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Interpreter ?

Tue May 11, 2021 6:29 pm

Python does do some pre-compilation to avoid re-interpreting every line. If you've ever noticed pyc files being generated around your Python code or libraries, those are binary translations of the source code that the Python interpreter can load and run quickly. They're not binary object code, as they don't run directly without the Python interpreter loading them.

Shell scripting languages like bash are interpreted line by line: they literally read one line of input, interpret what that line means, do the thing, then read the next line, and so on. You could, if you're careful/lucky/unwise, modify a running shell script and have it continue on from the same byte position in the modified script. If you modify a running Python program, however, the program will keep running as before, since by the time it's running, it's interpreted the source files and has no further use for them until the next run.

You can specify python3.7 or python3.8 when you call your script. All it will do is call a more specific version of the Python 3.x interpreter, which may not be present on someone else's system.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

mmkw43
Posts: 768
Joined: Tue Dec 24, 2013 6:18 pm

Re: Interpreter ?

Tue May 11, 2021 8:42 pm

So what is the advantage of an interpreted language? You would think that the mindset would be to have more efficiency and not to the translation on the fly. ? I'm glad I took the time to learn python/tkinter (vb.net was my introduction to writing code) but I kinda wish I would have attempted C because of the aspect of more source code protection. (Although I guess C can probably be reverse engineered also). But anyway, why an interpreted language in the first place?

Many thanks for the education.

User avatar
rpdom
Posts: 18739
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Interpreter ?

Wed May 12, 2021 5:49 am

mmkw43 wrote:
Tue May 11, 2021 8:42 pm
So what is the advantage of an interpreted language?
Various advantages/disadvantages

The main one is ease of testing and changing:

Interpreted

1. Change source code.
2. Run it.
3. Did it work? No: Go to 1 and fix it.
4. End

Compiled

1. Change source code.
2. Compile it.
3. Did it compile? No: Go to 1 and fix it.
4. Run it.
5. Did it work? No: Go to 1 and fix it.
6 End.

The compiled program will (usually) both run faster and start faster.
The compiler will detect a lot of coding errors for you before the program is ready to run.
Unreadable squiggle

User avatar
jahboater
Posts: 7079
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: Interpreter ?

Wed May 12, 2021 7:16 am

mmkw43 wrote:
Tue May 11, 2021 8:42 pm
but I kinda wish I would have attempted C because of the aspect of more source code protection. (Although I guess C can probably be reverse engineered also).
Why do you want that ?

Its true that (heavily optimized) C binaries are harder to reverse engineer, but its the last thing I think about when choosing a language.

Unless you have written a lot of crap code and are ashamed to let people see it.

mmkw43
Posts: 768
Joined: Tue Dec 24, 2013 6:18 pm

Re: Interpreter ?

Wed May 12, 2021 3:02 pm

Well it's for an electronic product. What's the point in giving it away?

I've had several products on the market in a specific industry that were all microcontroller based and the hard work I put into writing the code "belonged to me". No different from the recipe for Heinz 57.

I tried pyarmor which is problematic for my script, so it was a huge disappointment after spending so much time on a new venture that the source code could simply be copied (which I didn't know upfront about python).

Yeah, I know about EULA etc but that's hardly going to stop anyone who "wants one".
Not a huge deal because I'm retired because this was part fun/part biz but it sucks, frankly.

User avatar
jahboater
Posts: 7079
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: Interpreter ?

Wed May 12, 2021 4:27 pm

mmkw43 wrote:
Wed May 12, 2021 3:02 pm
Well it's for an electronic product. What's the point in giving it away?
But you are happy to use all the open source stuff written by others?

Since we are discussing Python, I guess, to avoid hypocrisy, you would not use the Python interpreter and instead write your own?

Most of the software that comes with the Pi is open source.

User avatar
B.Goode
Posts: 12101
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Interpreter ?

Wed May 12, 2021 4:42 pm

mmkw43 wrote:
Wed May 12, 2021 3:02 pm
Well it's for an electronic product. What's the point in giving it away?

[ ... ]

Not a huge deal because I'm retired because this was part fun/part biz but it sucks, frankly.

I'm retired.

I volunteer to help here in large part because on most days it's fun, and I learn a lot.

But I won't be volunteering to help this user...

mmkw43
Posts: 768
Joined: Tue Dec 24, 2013 6:18 pm

Re: Interpreter ?

Wed May 12, 2021 5:39 pm

If you read carefully, I wasn't being aggressive.

------ jeeeeze

edit --
fact is, my perspective is about the entire "product" -- not writing some application for business or an internet tie in.
but alas electronic products barely exist anymore so call me old fashioned in wanting to make it mine.

how many pcboards have you designed for your "python" endeavor? or membrane switches you've silk screened?

get my point?

User avatar
rpdom
Posts: 18739
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Interpreter ?

Wed May 12, 2021 6:01 pm

mmkw43 wrote:
Wed May 12, 2021 5:39 pm
If you read carefully, I wasn't being aggressive.

------ jeeeeze

edit --
fact is, my perspective is about the entire "product" -- not writing some application for business or an internet tie in.
but alas electronic products barely exist anymore so call me old fashioned in wanting to make it mine.

how many pcboards have you designed for your "python" endeavor? or membrane switches you've silk screened?

get my point?
Yes, you're too "talented" to need our help.
'K bye.

However, if you want to rethink that attitude, then maybe some people will help you.
Unreadable squiggle

Heater
Posts: 18261
Joined: Tue Jul 17, 2012 3:02 pm

Re: Interpreter ?

Wed May 12, 2021 6:54 pm

mmkw43 wrote:
Wed May 12, 2021 5:39 pm
how many pcboards have you designed for your "python" endeavor? or membrane switches you've silk screened?

get my point?
No. Your PCB and membrane switch design count have no bearing on the discussion at all.

But as we are brandishing work done as a stand in for reasoned argument....Speaking as someone who designed his first digital logic PCB as a teenager in 1972 and subsequently worked on a PCB CAD system in the late 1980's, and has done all kind of electronic and software things to this day, I think...

I have no problem with your wanting to keep you code secret on a micro-controller design. Use those code protection fuses if you like. Apparently the people who make language systems like the GCC or Clang compilers or Python interpreter or many others don't have a problem with this either. Mostly they don't demand that any code you create with their tools be open source.

How ever I do find it quite out of order to claim "it sucks" when taking the work of many, which they have published for the world to use, for free, without restriction. That certainly does not "suck" on the contrary it is frikken amazing. We have so many tools available to day, the likes of which we could hardly have imagined back in the day.

If you want to make your code secret somehow that is your problem to deal with. So deal with it. Don't slag of the creators of all the wonderful things they make for us.
Memory in C++ is a leaky abstraction .

mmkw43
Posts: 768
Joined: Tue Dec 24, 2013 6:18 pm

Re: Interpreter ?

Wed May 12, 2021 8:29 pm

I didn't know I was actually.
"Sucks" has a very broad meaning.
I wasn't being derogatory and you know it but I'm glad you had the chance to vent. Feel better?

Anyway -- thanks for the help.

I wasn't dissing python for cryin out loud.

So let me ask something (for my own education) (seriously).
How many "products" that use an SBC of some sort, use linux and python?
Or do they use linux and C? Or linux and ____?

Heater
Posts: 18261
Joined: Tue Jul 17, 2012 3:02 pm

Re: Interpreter ?

Wed May 12, 2021 9:15 pm

mmkw43 wrote:
Wed May 12, 2021 8:29 pm
I didn't know I was actually.
"Sucks" has a very broad meaning.
I wasn't being derogatory and you know it but I'm glad you had the chance to vent. Feel better?

Anyway -- thanks for the help.

I wasn't dissing python for cryin out loud.
Excuse my confusion. No I don't know it. I only get to read what you wrote. I have never know "X sucks" to be anything other than the expression of a negative, derogatory, opinion of X.
mmkw43 wrote:
Wed May 12, 2021 8:29 pm
So let me ask something (for my own education) (seriously).
How many "products" that use an SBC of some sort, use linux and python?
Or do they use linux and C? Or linux and ____?
There are millions, billions of them. They use Linux and Python and C and all manner of other languages.

An Android phone is an SBC that runs a linux kernel and the whole Java based operating system on top.

A lot of routers you can buy run a Linux kernel and whatever OS on top. For example the OpenWRT that you can run on a Pi is actually derived from some manufactures router product years ago.

All manner of other devices are using off the shelf industrial SBCs and Linux + whatever. I first put a C application on an in-house designed SBC running Linux in about 2002.

Just now the devices we ship run Linux with application components in C, Python, Javascript and Rust.
Memory in C++ is a leaky abstraction .

ejolson
Posts: 7254
Joined: Tue Mar 18, 2014 11:47 am

Re: Interpreter ?

Wed May 12, 2021 11:41 pm

Heater wrote:
Wed May 12, 2021 9:15 pm
mmkw43 wrote:
Wed May 12, 2021 8:29 pm
I didn't know I was actually.
"Sucks" has a very broad meaning.
I wasn't being derogatory and you know it but I'm glad you had the chance to vent. Feel better?

Anyway -- thanks for the help.

I wasn't dissing python for cryin out loud.
Excuse my confusion. No I don't know it. I only get to read what you wrote. I have never know "X sucks" to be anything other than the expression of a negative, derogatory, opinion of X.
mmkw43 wrote:
Wed May 12, 2021 8:29 pm
So let me ask something (for my own education) (seriously).
How many "products" that use an SBC of some sort, use linux and python?
Or do they use linux and C? Or linux and ____?
There are millions, billions of them. They use Linux and Python and C and all manner of other languages.

An Android phone is an SBC that runs a linux kernel and the whole Java based operating system on top.

A lot of routers you can buy run a Linux kernel and whatever OS on top. For example the OpenWRT that you can run on a Pi is actually derived from some manufactures router product years ago.

All manner of other devices are using off the shelf industrial SBCs and Linux + whatever. I first put a C application on an in-house designed SBC running Linux in about 2002.

Just now the devices we ship run Linux with application components in C, Python, Javascript and Rust.
Negative use: That vacuum cleaner sucks even more after the handle was broken.

Positive use: That vacuum cleaner sucks more even after the handle was broken.

Ambiguous: That vacuum cleaner sucks.

Does the same usage apply to Python?

Heater
Posts: 18261
Joined: Tue Jul 17, 2012 3:02 pm

Re: Interpreter ?

Thu May 13, 2021 4:32 am

ejolson wrote: Does the same usage apply to Python?
It's a different word. It's spelt the same and sounds the same but is from a different language.

I agree, Python suffers from both meanings of the word. Although not because of any difficulty hiding the source from those that use it.
Memory in C++ is a leaky abstraction .

User avatar
scruss
Posts: 4108
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Interpreter ?

Thu May 13, 2021 3:36 pm

mmkw43 wrote:
Wed May 12, 2021 8:29 pm
How many "products" that use an SBC of some sort, use linux and python?
It's hard to say how many, but it's amusing to see a product that has that very familiar pattern of USB ports and Ethernet port. I mean, c'mon: https://www.ncomputing.com/products/RX-series/RX300
Or seeing the crashed piece of corporate signage that's showing four Raspberries on a glitched screen.

There was a cautionary example recently of someone who did a product teardown (some kind of people counting camera? I can't find it now) of a product that was littered with "do not disassemble" warnings and legalese. They found it was a Raspberry Pi, whose SD card was littered with junk like personal music libraries, that used some barely-modified tutorial code for the camera clever bit. The worst part was that it set up an unsecured wireless AP with a tiny and guessable password that pretty much opened up your network to anyone nearby.

In the "Raspberry Pi - but good" product category, does the ViewPlus Tiger Box seem familiar? Before anyone gets all "how can they charge so much for this?!?!?!" outragey, this is an assistive device, so small market, high development cost. It costs a fraction of what (very few) comparable devices cost. Doing Braille printing right is incredibly hard.

If Eben's recent tweets about Citrix are anything to go by, I think we'll be seeing quite a few thin client products with Raspberry Pis in them ...
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

mmkw43
Posts: 768
Joined: Tue Dec 24, 2013 6:18 pm

Re: Interpreter ?

Thu May 13, 2021 4:00 pm

mp3 files in a final product :) I had deleted my trash can icon and thank god I remembered that.

in the business I'm in (or was), I know of 2 products using the pi. both use standard cases too, saving the dough on doing their own.

btw, my "gripe" was finding out too late in the game that python was open source. wasn't bashing that -- typical of me that I didn't research that up front. it's an incredible language and as a beginner, once you get your head around classes and the fact that it's needs a separate gui, it's very rewarding. unlike the vb.net that I knew, the thing I noticed about it is that if the code runs for 30 seconds, it will run till there's a power outage.

and --- "sucks" can also mean a light hearted "stinks".

User avatar
neilgl
Posts: 3254
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near The National Museum of Computing

Re: Interpreter ?

Thu May 13, 2021 4:30 pm

Unless I am missing something, python does not need a separate GUI, but you can use one of you want ?

User avatar
scruss
Posts: 4108
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Interpreter ?

Thu May 13, 2021 10:03 pm

mmkw43 wrote:
Thu May 13, 2021 4:00 pm
btw, my "gripe" was finding out too late in the game that python was open source.
The only thing you need to check for if you're making/selling a product is that none of your components use the GPL or a similar "share-alike" licence. Most libraries don't use GPL: at most they'll use LGPL, which allows you to redistribute without providing source. But if you include something that's GPL - like the super-handy GNU readline library - users have a right to request your source code.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

mmkw43
Posts: 768
Joined: Tue Dec 24, 2013 6:18 pm

Re: Interpreter ?

Thu May 13, 2021 11:41 pm

IF I go ahead and do a product with this, (it's a 2 part system connected with an RS485 link) I'll just send it out as a pyc file. I did do a spot of code that looks for the cpu number and if it isn't a match it shuts down and gave that file a weird name etc. I was thinking maybe I could put everything in a hidden folder in a weird location. Of course an easy search for a python file and your done if you're looking for it but just a few simple things may deter some that are nosey. Played with pyarmor but it's been flaky -- did everything right -- it's easy to use but it increased the file size quite a bit.

Heater
Posts: 18261
Joined: Tue Jul 17, 2012 3:02 pm

Re: Interpreter ?

Fri May 14, 2021 1:15 am

Be aware that pic files can be decompiled back to Python source easily with uncompyle6:
https://medium.com/cassandra-cryptoasse ... e5f45d1edb
https://pypi.org/project/uncompyle6/

If you really want to obfuscate the code you deliver it would be a much better idea to write it in a compiled language like C/C++, Rust, Pascal, etc.
Memory in C++ is a leaky abstraction .

User avatar
jahboater
Posts: 7079
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: Interpreter ?

Fri May 14, 2021 1:29 am

Heater wrote:
Fri May 14, 2021 1:15 am
If you really want to obfuscate the code you deliver it would be a much better idea to write it in a compiled language like C/C++, Rust, Pascal, etc.
Yes, and preferably stripped and heavily optimized (-O3 -s compiler options).

Your program will run much faster too.

See also sstrip (super strip):
https://github.com/aunali1/super-strip
which stops the likes of objdump and gdb working.
Last edited by jahboater on Fri May 14, 2021 1:58 am, edited 3 times in total.

ejolson
Posts: 7254
Joined: Tue Mar 18, 2014 11:47 am

Re: Interpreter ?

Fri May 14, 2021 1:34 am

mmkw43 wrote:
Thu May 13, 2021 11:41 pm
IF I go ahead and do a product with this, (it's a 2 part system connected with an RS485 link) I'll just send it out as a pyc file. I did do a spot of code that looks for the cpu number and if it isn't a match it shuts down and gave that file a weird name etc. I was thinking maybe I could put everything in a hidden folder in a weird location. Of course an easy search for a python file and your done if you're looking for it but just a few simple things may deter some that are nosey. Played with pyarmor but it's been flaky -- did everything right -- it's easy to use but it increased the file size quite a bit.
Just out of curiosity, how many thousand lines of code are you trying to protect?

mmkw43
Posts: 768
Joined: Tue Dec 24, 2013 6:18 pm

Re: Interpreter ?

Fri May 14, 2021 2:11 pm

What are the basics of Cython in a few words? I know nothing about it other than some simple statements I read about "converting python to c" (although I'm sure it's not that simple) Does it offer more protection or worth learning about?

And to answer, the one project has about 3K lines but much of it is repetition because it's an 8 channel device and it also uses tkinter.

I actually (for the fun of it) may delve into C -- I did before briefly but got distracted.

Return to “Python”