Calling module from another module

I have some modules on a Raspberry Pi pico in one directory.
From the main module, I want to run the script from another module.I have tried to use the code subproces.run(python, “module name”), but that doesn’t work.

This is the code from the module I called ‘update rtc module’:

from machine import I2C, Pin
from ds3231_gen import *
import time

sda_pin=Pin(4)
scl_pin=Pin(5)

i2c = I2C(0, scl=scl_pin, sda=sda_pin)
time.sleep(0.5)

ds = DS3231(i2c)

ds.set_time()

print (“Date en Time saved in RTC (DS3231)”)

In the main program I want to call this ‘update rtc module’.

I hope somebody can help me with the syntax.

Thank you.

Lo

Is all the code involved python? can you import the code you want to run and call it, instead of calling subprocess?

The argument of run should be a list:

subprocess.run([path_to_python, path_to_module])

If the module is called update rtc module.py it might not work on some versions (at least afaik), because of the whitespace in the name / in the command. I would either

a) Rename the file to something like update_rtc_module.py

b) Use the command python “update_rtc_module.py” instead, meaning you’ll do subprocess.run(f”{python_executable} ‘{filename}’”.split())

I would advise you to just rename files if possible. It makes it a lot easier for a lot of tools to be able to use the file, especially in the terminal. Needing to wrap file/path names in "” is something that I sadly have to do in some cases, and it makes me go mad.

Using split() here will break up the file name if it has spaces. You want [sys.executable, filename] (or shell=True).

Thank you all for your replies. The Import statement works fine for me, but the other answers were also helpful. Now I understand the subprocess better.
Thanks again.
Lo

Thank you Phildini. The import statement works fine!