简体   繁体   中英

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!

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 - .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM