Page 1 of 1

NameError: name is not defined

Posted: Mon Mar 05, 2018 2:54 am
by mfwebn
boot.py:

Code: Select all

# boot.py -- run on boot-up
print('MicroPython boot')
APIs:

Code: Select all

mp_uint_t f_rd_read_byte(void *data){
	mp_uint_t r_data;
	UINT read_len = 1;
    FRESULT res;
	res = f_read((FIL *)data, &r_data, 1, &read_len);
	if(res != FR_OK || read_len == 0 || r_data == (mp_uint_t)EOF){
		return MP_READER_EOF;
	}
	else{
		return r_data;
	}
}

void f_rd_close(void *data){
	f_close((FIL *)data);
	m_free(data);
}

void reader_new_file(mp_reader_t *reader,const char *filename)
{
	FIL *fp;
	.....(open file)
	reader->data = (void *)fp;
	reader->readbyte = f_rd_read_byte;
	reader->close = f_rd_close;
}

mp_lexer_t *mp_lexer_new_from_file(const char *filename)
{
	mp_reader_t reader;
	reader_new_file(&reader,filename);
	return mp_lexer_new(qstr_from_str(filename), reader);
}
I use the pyexec_file function to execute boot.py, but I return the error:

Code: Select all

exec start
Traceback (most recent call last):
  File "boot.py", line 1, in <module>
NameError: name '# boot.py -- run on boot-up
print('MicroPython boot')' is not defined

Re: NameError: name is not defined

Posted: Tue Jul 06, 2021 6:08 am
by markelvy
Python knows the purposes of certain names (ex. built-in functions ). Other names are defined within the program (ex. variables). If Python encounters a name that it doesn't recognize, you'll probably get NameError: global name 'xx' is not defined error. In most cases, this error is triggered when Python sees a variable name (Global or Local) and doesn't know what it's for. These errors can happen if you forget to initialize a variable , if you misspell a variable, or if you misspell a reserved word such as "True". Before you use the python global variable in your function for reading, it must be first initialized somewhere: either outside of the function or inside it.

Re: NameError: name is not defined

Posted: Tue Jul 06, 2021 7:01 am
by jimmo
mfwebn wrote:
Mon Mar 05, 2018 2:54 am
I use the pyexec_file function to execute boot.py, but I return the error:
It looks like something is going wrong with the lexer and it thinks the entire contents of the file is a single identifier.

It looks like you're doing a port of MicroPython to a different platform (using fatfs directly?)... can you provide more information about what you're doing (more code would be useful). What architecture is it?

If you have the ability to use a debugger on the target platform (or even just ading some printf), then tracing the lexer and parser might be useful... For example, on entry to mp_lexer_to_next, if you add

Code: Select all

void mp_lexer_to_next(mp_lexer_t *lex) {
    printf("%ld %ld\n", lex->line, lex->column);
then you should see

Code: Select all

1 1
2 1
2 6
2 7
2 25
2 26
3 1
for your boot.py.
markelvy wrote:
Tue Jul 06, 2021 6:08 am
Python knows the purposes of certain names (ex. built-in functions ).
Thanks, and what you said is generally correct, but unfortunately I don't think this isn't what's going on here.