geotek1
Posts: 5
Joined: Sun Sep 27, 2020 4:48 pm

Import “Machine” Question?

Thu Jun 17, 2021 5:02 pm

Greetings

As a newbie to a Pico, I need some assistance.
I am trying to experiment with both CircuitPython and Micropython.
I have typed and try to run some programs - some of these programs call ‘import machine’ and when Try to run the program I get an error message - no module ‘machine ‘ found.
Where does this ‘machine’ come from? Where does it come from- is there a library that I am missing?
Thanks in advance
Tom

fivdi
Posts: 447
Joined: Sun Sep 23, 2012 8:09 pm
Contact: Website

Re: Import “Machine” Question?

Thu Jun 17, 2021 7:56 pm

machine is a built-in module in MicroPython which means it's part of MicroPython itself. CircuitPython and MicroPython similar but not exactly the same. One difference is that CircuitPython has no built-in module called machine.

User avatar
B.Goode
Posts: 12306
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Import “Machine” Question?

Thu Jun 17, 2021 8:44 pm

fivdi wrote:
Thu Jun 17, 2021 7:56 pm
machine is a built-in module in MicroPython which means it's part of MicroPython itself. CircuitPython and MicroPython similar but not exactly the same. One difference is that CircuitPython has no built-in module called machine.


.... for microcontrollers flashed with CircuitPython the corresponding hardware functionality is probably in the board module instead.

User avatar
scruss
Posts: 4190
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Import “Machine” Question?

Thu Jun 17, 2021 8:56 pm

… so basically, if you get the ImportError: no module named 'machine' error, you're running on CircuitPython.

I'd stick to one or the other, since the language differences that CircuitPython introduced make it incompatible. CircuitPython has better support for add-on hardware, though.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

fivdi
Posts: 447
Joined: Sun Sep 23, 2012 8:09 pm
Contact: Website

Re: Import “Machine” Question?

Thu Jun 17, 2021 9:17 pm

B.Goode wrote:
Thu Jun 17, 2021 8:44 pm
.... for microcontrollers flashed with CircuitPython the corresponding hardware functionality is probably in the board module instead.

Not really, board is all about board specific pins.

Code: Select all

>>> import board
>>> board.
A0              A1              A2              A3
BAT_SENSE       GP0             GP1             GP10
GP11            GP12            GP13            GP14
GP15            GP16            GP17            GP18
GP19            GP2             GP20            GP21
GP22            GP25            GP26            GP26_A0
GP27            GP27_A1         GP28            GP28_A2
GP3             GP4             GP5             GP6
GP7             GP8             GP9             I2C
LED             SCL             SDA             USER_SW
VBUS_DETECT
>>> board.

The microcontroller module in CircuitPython is porbably closest to machine in MicroPython

Code: Select all

>>> import microcontroller
>>> microcontroller.
Pin             Processor       ResetReason     RunMode
cpu             cpus            delay_us
disable_interrupts              enable_interrupts
nvm             on_next_reset   pin             reset
watchdog
>>> microcontroller.
scruss wrote:
Thu Jun 17, 2021 8:56 pm
… so basically, if you get the ImportError: no module named 'machine' error, you're running on CircuitPython.

Correct.

Code: Select all

>>> import machine
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: no module named 'machine'
>>> 

hippy
Posts: 10319
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Import “Machine” Question?

Fri Jun 18, 2021 11:24 am

scruss wrote:
Thu Jun 17, 2021 8:56 pm
I'd stick to one or the other, since the language differences that CircuitPython introduced make it incompatible.
It is really the libraries and various API rather then the language which are different but that can make a program written for one incompatible with the other, so considering them different languages isn't unreasonable.

MicroPython isn't CircuitPython isn't Python. It is possible to write code which will run 'as is' on all three but it does make things tricky and means more work. I don't have CircuitPython currently installed but, as well as try-except, these can help identify which is which -

print( os.uname() )
print( sys.platform )
scruss wrote:
Thu Jun 17, 2021 8:56 pm
CircuitPython has better support for add-on hardware, though.
In theory CircuitPython libraries should be usable with MicroPython but I don't believe all are and it's a bit of a faff installing and using them -

viewtopic.php?f=146&t=312345

As I don't have a Pico set-up for hardware development, don't have access to hardware, I haven't done much with those other than make things easier to install.

Return to “MicroPython”