forthprgrmr
Posts: 3
Joined: Thu Jan 26, 2023 6:32 pm

import Thonny MicroPython

Thu Jan 26, 2023 6:40 pm

I can't import files I create. importing from loaded modules works okay.
If I make a.py with something in it.
Then I make b.py with the line: import a
at the top of it, it doesn't work giving: ImportError: no module named a
I'm doing this all in one directory. I tried copying a.py into other locations, but that didn't work either.

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

Re: import Thonny MicroPython

Thu Jan 26, 2023 8:06 pm

forthprgrmr wrote:
Thu Jan 26, 2023 6:40 pm
I can't import files I create. importing from loaded modules works okay.
If I make a.py with something in it.
Then I make b.py with the line: import a
at the top of it, it doesn't work giving: ImportError: no module named a
I'm doing this all in one directory. I tried copying a.py into other locations, but that didn't work either.


"If I make a.py with something in it."

Where do you save a.py?

It needs to be on the working directory ON THE PICO ITSELF. (Or in the /lib subdirectory of the filesystem on the Pico.)

Saving it on "This Computer" (one of the choices offered by Thonny) will not make it importable by MicroPython running on the Pico.

forthprgrmr
Posts: 3
Joined: Thu Jan 26, 2023 6:32 pm

Re: import Thonny MicroPython

Thu Jan 26, 2023 9:48 pm

Surely the source code for a project doesn't have to be on the target!
Placing the file I'm trying to import on the Pico does not work either.
If I combine the files into one and load that into Thonny, then it does work.
The thing I'm really trying to do is to integrate sensors into my program. The source file for the accelerometer is only available (from Adafruit) for circuitpython, not micropython. But the source is available. Putting that large source into my small program works fine. But I don't want to expand sensor libraries (I have a number of sensors I'm using) all into one file - messy.

User avatar
thagrol
Posts: 8867
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK

Re: import Thonny MicroPython

Thu Jan 26, 2023 11:41 pm

forthprgrmr wrote:
Thu Jan 26, 2023 9:48 pm
Surely the source code for a project doesn't have to be on the target!
You appear to misunderstanf some python fundamentals.

Python is an interpreted language not a compiled one. Source and object code is the same thing.*

Imports occur at run time. The interpreter must be able to find the required file(s) or the import will fail. If you import foo the interpreter will look for a file called foo.py starting in the cwd.

With pico and thonny it can depend on how thonny is configured. If you're running your code in the Pico's REPL the file should be in the root folder of the micropython file system (this is not the file system exposed by the bootsel button). If you're running the code in thonny, it must be in the cwd or any of the standard locations for python libraries.

It's my experience that importing a custom module/file just works (see https://github.com/thagrol/fakewake/tree/master/pico) though I've no idea whether mixing micropython and circuitpython is a good idea or not.

You should focus on finding (and telling us) why the import fails and with what error(s). Without that all we can do is guess which is likely to waste everyone's time.

*: Some python interpreters compile to byte code but unlike statically linked C (and other languages) programs this does not pull in the libraries/modules. Those are still only pulled in at run time.
Knowledge, skills, & experience have value. If you expect to profit from someone's you should expect to pay for them.

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

forthprgrmr
Posts: 3
Joined: Thu Jan 26, 2023 6:32 pm

Re: import Thonny MicroPython

Fri Jan 27, 2023 4:37 am

Obviously I've missed some Python fundamentals.
I've been writing in a threaded language (Forth) professionally for 45 years now. I'm quite familiar with such things.
Python is only appealing to me (and it's a big appeal) because of the breadth of the libraries available.
In my retirement I thought it would be nice to create some open source astronomy projects, and the short path was to use libraries available in python. The Pico is appealing too. I've created a simple carrier board holding the Pico and 4 motor drivers, OLED display, etc etc because I got tired of designing full custom solutions.

And I'm not trying to mix circuitpython and micropython. I'm not sure why you've commented about my doing that.
I'm a novice at python. I took a source library from circuitpython repository and placed it into a source file in my micropython and it works.
BUT
I can't include it. No matter what I've tried.
Everything has to be one file, or things included with the system.

I tried backing off and creating a simple file - a.py that contained almost nothing.
And then I created b.py that only contained: include a

I don't mean to waste anyone's time. I came with, I thought, a reasonable question: why can't I import?
I can import when the library is created with the PyPl package manager. Nothing else works.
I also can't find those libraries on my system. They are there, but it's not obvious. That's probably one of the reasons I thought the interpreter was some kind of token threading.

I put the python source file I'm trying to import ( and the simple example above) all over my Raspbian system (desktop, folder with the source I'm calling from) and on the root directory of the Pico.
As I mentioned, all I get is: ImportError: no module named xxxx

I probably spent 4 or 5 hours attempting to solve this before I bothered to ask a question on this board.
I'm not the expert here. I thought someone else might be.
Obviously I'm wasting your time. I'll go away.

But if you have Forth questions, I'm happy to answer them. I'm quite good at that.

User avatar
thagrol
Posts: 8867
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK

Re: import Thonny MicroPython

Fri Jan 27, 2023 12:28 pm

First, you weren't wasting my time.

Second, I appologise if I caused any offense.
forthprgrmr wrote:
Fri Jan 27, 2023 4:37 am
And I'm not trying to mix circuitpython and micropython. I'm not sure why you've commented about my doing that.
I'm a novice at python. I took a source library from circuitpython repository and placed it into a source file in my micropython and it works.
That's why I commented about mixing circuit and micro python.
BUT
I can't include it. No matter what I've tried.
Everything has to be one file, or things included with the system.
OK.

For clarity I'm using:
  • RPiOS lite
  • Thonny 3.3.10
  • MicroPython v1.19.1 on 2022-10-28; Raspberry Pi Pico W with RP2040
  • Thonny configuered to run editor content using the REPL on the pico
  • main.py:

    Code: Select all

    print('started main.py')
    import lib
  • lib.py:

    Code: Select all

    print('importing lib.py')
  • Both the above files saved only to the root directory of the micropython file system on the pico.
Running from thonny using the green button results in:

Code: Select all

>>> %Run -c $EDITOR_CONTENT
started main.py
importing lib.py
Resetting the pico and running from the REPL by import main results in:

Code: Select all

>>> import main
started main.py
importing lib.py
This leads me to three possibilities:
  1. You're using a different build of micropython which is bugged.
  2. Letter cases in your file name and import statement do not match.
  3. Your import statement includes the .py suffix e.g.

    Code: Select all

    import foo.py
    It should not.
Lastly, whether running from thonny or running directly from the micropython file system the location of the file you're trying to import on the Pi's (or Windows, Mac, etc) file system does not matter as the micro python interpreter on the pico has no access to it.
Knowledge, skills, & experience have value. If you expect to profit from someone's you should expect to pay for them.

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

Return to “MicroPython”