How to call linux bash from micropython

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
skylin008
Posts: 88
Joined: Wed Mar 11, 2015 6:21 am

How to call linux bash from micropython

Post by skylin008 » Tue Nov 22, 2016 4:07 am

Hello,every one! I want to using efax software,How to call efax command line from micropython.Thank you very much!

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How to call linux bash from micropython

Post by pythoncoder » Tue Nov 22, 2016 7:17 am

Code: Select all

>>> import uos
>>> uos.system('ls -l')
total 40
-rw-r--r-- 1 adminpete adminpete 1006 Feb 20  2016 ffilib.py
drwxr-xr-x 2 adminpete adminpete 4096 Aug  7  2015 json
-rw-r--r-- 1 adminpete adminpete  196 Jul 29 11:27 pickle.py
drwxr-xr-x 2 adminpete adminpete 4096 Oct 13 07:18 __pycache__
-rw-r--r-- 1 adminpete adminpete   76 Oct 24 07:12 rats1.py
-rw-r--r-- 1 adminpete adminpete   95 Oct 24 07:14 rats2.py
-rw-r--r-- 1 adminpete adminpete   67 Oct 24 07:16 rats.py
-rw-r--r-- 1 adminpete adminpete 4886 Aug  7  2015 re.py
lrwxrwxrwx 1 adminpete adminpete   65 May 15  2016 upython -> /mnt/qnap2/data/Projects/MicroPython/micropython/unix/micropython
0
Peter Hinch
Index to my micropython libraries.

skylin008
Posts: 88
Joined: Wed Mar 11, 2015 6:21 am

Re: How to call linux bash from micropython

Post by skylin008 » Tue Nov 22, 2016 7:27 am

Thanks pythoncoder support.I will be try and check it out!

skylin008
Posts: 88
Joined: Wed Mar 11, 2015 6:21 am

Re: How to call linux bash from micropython

Post by skylin008 » Tue Nov 22, 2016 8:55 am

Hello pythoncoder,
when I run the uos.system function,the error logs as follow:

>>> import os
>>> import uos
>>> uos.system()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'system'


How to solve this issue,Thanks!

User avatar
platforma
Posts: 258
Joined: Thu May 28, 2015 5:08 pm
Location: Japan

Re: How to call linux bash from micropython

Post by platforma » Tue Nov 22, 2016 9:36 am

The module is called "uos". You need to pass the command you're trying to call to the system() function.

Code: Select all

>>> import uos
>>> uos.system('uname -a')
Linux arch 4.7.6-1-ARCH #1 SMP PREEMPT Fri Sep 30 19:28:42 CEST 2016 x86_64 GNU/Linux
Are you, perhaps, trying to run this on pyboard?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How to call linux bash from micropython

Post by pythoncoder » Tue Nov 22, 2016 5:28 pm

@platforma Good point. I did assume that the original post referred to the Unix build owing to the reference to Bash ;)

@skylin008 If you're running MicroPython on a Pyboard, ESP8266 or other hardware, you can't access a Bash prompt because these boards don't run Linux. They run MicroPython directly on the microprocessor; there is no operating system and no Bash shell. It will produce the error message you're seeing.
Peter Hinch
Index to my micropython libraries.

torwag
Posts: 220
Joined: Fri Dec 13, 2013 9:25 am

Re: How to call linux bash from micropython

Post by torwag » Thu Dec 08, 2016 7:24 pm

I guess what he wanted to do is to call a bash script on the host pc triggered by an event within e.g. a pyboard.
Easiest to to that would be to write a small python script for the host machine, which communicates via the virtual serial port with the pyboard. As soon as the pyboard sends a command, the script will execute the commands like shown in Peters example.

Post Reply