Python Forum
function on a different tab ? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: function on a different tab ? (/thread-39962.html)

Pages: 1 2 3 4


RE: function on a different tab ? - Gribouillis - May-24-2023

You must import sys first.


RE: function on a different tab ? - trix - May-24-2023

i have now:

import sys
import os

if '__file__' not  in globals():
    __file__ = sys.modules[__name__].__file__

print('Cwd is {}'.format(os.getcwd()))
print('Current program is {}'.format(__file__))
print('Sys.path is {}'.format(sys.path))
 
import examplemod
result:

Error:
Traceback (most recent call last): File "<stdin>", line 5, in <module> KeyError: __main__



RE: function on a different tab ? - deanhystad - May-24-2023

You might find this interesting.

https://wiki.micropython.org/gollum/Importing-Modules


RE: function on a different tab ? - trix - May-24-2023

i'm reading it already, tnx.


RE: function on a different tab ? - Gribouillis - May-24-2023

Then you could perhaps just add the correct path to the directory containing the modules in sys.path, perhaps
import sys
sys.path.append('/modules')
at the top of maintest.py (or should it be 0:/modules ?)

Also better uninstall that micropython-pathlib module that you installed. Not sure what it is.


RE: function on a different tab ? - trix - May-24-2023

the article says:

>>> import sys
>>> sys.path
['0:/', '0:/lib']


and i get:

>>> import sys
>>> sys.path
['', '.frozen', '/lib']


then:

So lets create a file, mod1.py which contains the following:

def hello():
print('hello from mod1')

and copy it to your internal flash


copy to your internal flash.....how to do that ?
just: save as


RE: function on a different tab ? - deanhystad - May-24-2023

These are pico questions. Maybe you will have better luck on a pico forum?

https://forums.raspberrypi.com/viewforum.php?f=143

BTW, I am sorry for earlier bad advice. I forgot that you were using micropython on a pico. The easiest route might be to use one file. I have no idea how to install modules on your device. I played with micropython on the LEGO Spike controller, and installing modules on that is a pain. I would write everything in one file, and after rigorous testing, I might pull out some of the code and install it as a module.


RE: function on a different tab ? - trix - May-24-2023

maybe i have to uninstall thonny, and then install again ?

edit:
yes,..maybe i have to ask my question on the pico forum.

thanks for so far.


RE: function on a different tab ? - trix - May-26-2023

it is working now Dance

here you can find my topic.

https://forums.raspberrypi.com/viewtopic.php?p=2109672#p2109672


RE: function on a different tab ? - Gribouillis - May-26-2023

(May-26-2023, 05:18 PM)trix Wrote: it is working now Dance
That's great! Shocked I don't even understand the answers... What did you do to make it work?