Weird Error in with 'range' in embedded python

I have this code:

from math import *
def profile(x):
    prof=[]
    for i in range(6):
        ang=3.14*i/3.0
        prof.append([3+cos(ang),sin(ang)])
   
    return prof

when i call this function, i get:

NameError: name ‘range’ is not defined

when i use [0,1,2,3,4,5] instead of range(6)
my code works, but its not generic anymore .

how can that be ?
where is the range generator supposed to be defined and whats going wrong in my place ?

At first glance I do not know why this would happen. But your post title mentions that you are using embedded Python - what distribution and version of Python are you using, exactly? I wonder if it could be something distribution specific.

Also for what it is worth - global built-in functions are also accessible through the standard library builtins module: https://docs.python.org/3.11/library/builtins.html

1 Like

Its really odd for range to be missing. As was said: it’s a built-in.

Worst case you can write your own range function:

def myrange(x):
    idx = 0
    while idx < x:
        yield idx
        idx += 1

Of course if you need more than this basic version, you can extend it to add the step, count fields too.

(Written on my phone, so ignore small mistypes)

Accurately, it is not pythonic but still workable and gives identical results in any other platforms.

Yes, this does the trick!
I did
from builtins import *

Apparently this is done implicitly with the interpreter and has to be done separately for embedded. Thank you . I am using Fedora core37, but that should not matter