Can't seem to import module even after `sys.path.insert()`.

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
HH0718
Posts: 2
Joined: Tue Nov 03, 2020 6:57 pm

Can't seem to import module even after `sys.path.insert()`.

Post by HH0718 » Tue Nov 03, 2020 7:18 pm

I'm not new to python but I am to micropython.

I am using an ESP32 dev board with the latest stable uPython firmware.

I am also using the latest pycharm pro IDE with the uPython plugin 1.1.2 (previously tried 1.1.1 with same results).

Please consider the following folder structure:

Code: Select all

-- Source
    -- BME280.py <--- sensor module
    -- __init__.py <--- self explanatory but does not seem to work 
    -- ota_updater.py <--- over the air update module "https://github.com/rdehuyss/micropython-ota-updater" only thing I changed was the "main" directory variable name to "source" as to not confuse or possibly conflict with `main.py` the root folder
    --temp_sensor.py <--- main code
--main.py
Here's what main.py looks like:

Code: Select all

import network

from source import ota_updater

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
    print('connecting to network...')
    wlan.connect('<SSID>', '<PASSWORD>')
    while not wlan.isconnected():
        pass
print('network config:', wlan.ifconfig())


def download_and_install_update_if_available():
    token = '<TOKEN>'
    o = ota_updater.OTAUpdater('https://github.com/HH0718/bme_temp_sensor', headers={'Authorization': 'token {}'.format(token)})
    o.download_and_install_update_if_available('<SSID>', '<PASSWORD>')


def start():
    global project
    from source.temp_sensor import temp_sensor
    project = temp_sensor(wlan)


def boot():
    download_and_install_update_if_available()
    start()


project = None
boot()
I know the code could be cleaned up but I'm more interested in functionality first and then I'll clean it up.

The problem I get is it will not find my `source` module. This is the error:

Code: Select all

Traceback (most recent call last):
  File "main.py", line 3, in <module>
ImportError: no module named 'source.ota_updater'
I have tried adding the `source` into path using `sys.path.insert(0, './source')` to no avail.

I kid you not I have spent the better half of my day trying to understand how to solve this and my google skills have failed me.

I understand that some forum responses say don't use folder and keep it simple, but due to the way this works, I believe I must have subfolders.

other forums say not to use Pycharm and to use REPL, but I'm unfamiliar with the CLI methods. I am willing to give it a try and learn it and I have a feeling it's a pycharm issue, but I'm not sure what else to do and I need some guidance.

Thank you all for your help.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Can't seem to import module even after `sys.path.insert()`.

Post by Roberthh » Tue Nov 03, 2020 7:42 pm

Once you add the directory source to the sys.path list, you can simply write;

import ota_updater

HH0718
Posts: 2
Joined: Tue Nov 03, 2020 6:57 pm

Re: Can't seem to import module even after `sys.path.insert()`.

Post by HH0718 » Tue Nov 03, 2020 7:49 pm

Thank you for your prompt response!

Unfortunately that did not work. Please see below.

Code: Select all

>>> import sys
>>> sys.path
['', '/lib']
>>> import os
>>> os.listdir()
['main.py', 'source\temp_sensor.py', 'source\\BME280.py', 'source\\__init__.py', 'source\\ota_updater.py', 'source']
>>>
>>> sys.path
['', '/lib']
>>> sys.path.insert(0, './source')
>>> import ota_updater
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: no module named 'ota_updater'
Any other thoughts or suggestions?

Thank you for taking the time to respond.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Can't seem to import module even after `sys.path.insert()`.

Post by Roberthh » Tue Nov 03, 2020 7:57 pm

Two observations:

1. The path name separator is / not \. And there is no subdirectory called source.
2. You can insert source, not ./source into sys.path. But that should be equivalent.

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

Re: Can't seem to import module even after `sys.path.insert()`.

Post by jimmo » Tue Nov 03, 2020 10:32 pm

HH0718 wrote:
Tue Nov 03, 2020 7:49 pm
>>> os.listdir()
['main.py', 'source\temp_sensor.py', 'source\\BME280.py', 'source\\__init__.py', 'source\\ota_updater.py', 'source']
As roberthh says - there is no directory on your device named "source", and what's happened is that you have files in the root directory somehow named "source\\foo.py". I suspect you're right that this is a pycharm issue.

Post Reply