import behaviour different between unix port and ESP32 port?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
spierepf
Posts: 22
Joined: Mon Jul 08, 2019 3:22 pm

import behaviour different between unix port and ESP32 port?

Post by spierepf » Sun Mar 29, 2020 6:53 pm

I am trying to use micropython's unix port as a means of testing my code without having to upload the code to my ESP32 each time.

With some gentle nudging (thanks @tve) I've made some progress.

I'm noticing however, that with libraries like urequests, on the unix platform I need to import them as follows:

Code: Select all

from urequests import urequests
while on an actual ESP32 I need to import them as:

Code: Select all

import urequests
In either case the following statement works as expected:

Code: Select all

print(urequests.get("http://example.com").text)
That has led me to import the urequests library as:

Code: Select all

import sys

if 'linux' == sys.platform:
  from urequests import urequests
else:
  import urequests
which will likely get cumbersome as more libraries need to be incorporated into my project.

Can anyone let me in on what is different between the two platforms that would cause this?

spierepf
Posts: 22
Joined: Mon Jul 08, 2019 3:22 pm

Re: import behaviour different between unix port and ESP32 port?

Post by spierepf » Sun Mar 29, 2020 7:12 pm

Fun fact, my convoluted import only appears to work:

Code: Select all

MicroPython v1.11 on 2020-03-29; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> from urequests import urequests
>>> urequests.get("https://example.com").text
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "./lib/urequests/urequests.py", line 104, in get
  File "./lib/urequests/urequests.py", line 124, in urlopen
  File "./lib/urequests/urequests.py", line 30, in __init__
AttributeError: 'module' object has no attribute 'IPPROTO_SEC'
[\code]

spierepf
Posts: 22
Joined: Mon Jul 08, 2019 3:22 pm

Re: import behaviour different between unix port and ESP32 port?

Post by spierepf » Sun Mar 29, 2020 7:16 pm

Here is a github project that I hope will make things a little clearer:

https://github.com/spierepf/micropython-template

You'll want the develop branch.

User avatar
tve
Posts: 216
Joined: Wed Jan 01, 2020 10:12 pm
Location: Santa Barbara, CA
Contact:

Re: import behaviour different between unix port and ESP32 port?

Post by tve » Sun Mar 29, 2020 7:47 pm

I think you're missing an __init__.py file in your module directory.

spierepf
Posts: 22
Joined: Mon Jul 08, 2019 3:22 pm

Re: import behaviour different between unix port and ESP32 port?

Post by spierepf » Sun Mar 29, 2020 8:24 pm

The urequests library is installed by micropip.py (https://github.com/peterhinch/micropyth ... icropip.py), so should it create the __init__.py file?

Even if I `touch pyboard/lib/urequests/__init__.py` I get the same result.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: import behaviour different between unix port and ESP32 port?

Post by jimmo » Mon Mar 30, 2020 1:23 am

I might need to do some more research but my memory was that micropip.py was for installing MicroPython packages on CPython.

The unix port should have "upip" available by default. Can you try

Code: Select all

>>> import upip
>>> upip.install('micropython-urequests')
Installing to: /home/jimmo/.micropython/lib/
Warning: micropython.org SSL certificate is not validated
Installing micropython-urequests 0.6 from https://micropython.org/pi/urequests/urequests-0.6.tar.gz
>>> import urequests
>>> urequests.get(...)

Post Reply