Search code examples
functionmicropythonunameraspberry-pi-pico

Why doesn't this micropython function work?


I have written a function in micropython that attempts to retrieve uname information and strip out stuff I don't want and then return a value:

 import os

 def get_uname():      
     my_uname = os.uname()[3]
     my_uname = my_uname.replace("(GNU 9.3.0 MinSizeRel)", "") 
     my_uname = my_uname.replace(" on ", "-")
     my_uname = my_uname.replace(" ", "") 
     return my_uname

Every time I try to import it as a module I get an error message:

 import sw-ver
 Traceback (most recent call last):
 File "<stdin>", line 1
 SyntaxError: invalid syntax

However if I copy and paste the function directly into repl, it is successful, no errors:

 >>> import os
 >>> def get_uname():  
 ...     my_uname = os.uname()[3]
 ...     my_uname = my_uname.replace("(GNU 9.3.0 MinSizeRel)", "")
 ...     my_uname = my_uname.replace(" on ", "-")
 ...     my_uname = my_uname.replace(" ", "")
 ...     return my_uname
 ...     
 ...     
 ... 
 >>> get_uname()
 'v1.14-2021-02-05'
 >>> 

I have tried it every way i can think of, I renamed the file just in case that was causing the problem. Does anyone have a suggestion on how to troubleshoot this?

thanks!


Solution

  • If you have put your function in a file sw-ver.py, you will have difficulty importing it with import sw-ver as - is used for arithmetic and so on, even in this context. If you cannot rename your file, see here, but the simplest answer is to follow the style guide and use only lowercase letters for modules, or at least _ instead of -.