Python Forum
Can I get some clarification on importing functions from external files.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can I get some clarification on importing functions from external files.
#1
I'm familiar with programming syntax and constructs but very new to Python. I am trying to figure out how to modularize some functions into separate files so they can be "included" in a main program script.

This is my "all in one script"

    # main.py> 
    import machine
    import time

    led = machine.Pin(13, machine.Pin.OUT)

    def blink_led_n_times(num, t_on, t_off, msg):
        
        counter = 0
        
        while (counter < num):
            led.on()
            time.sleep(t_on)
            led.off()
            time.sleep(t_off)
            counter += 1
            
        print(msg)
        
    blink_led_n_times(10,.5,.5,'Done Blinking.')
I can run this, it works as expected.

So lets say for example I want to move the function blink_led_n_times() to it's own file.

Now I have "blink.py"

    # blink.py>
    def blink_led_n_times(num, t_on, t_off, msg):
        
        counter = 0
        
        while (counter < num):
            led.on()
            time.sleep(t_on)
            led.off()
            time.sleep(t_off)
            counter += 1
            
        print(msg)
So I want to include/import that into my main program script.

    # main.py> 
    import machine
    import time
    from blink import blink_led_n_times

    led = machine.Pin(13, machine.Pin.OUT)

    blink_led_n_times(10,.05, .05, 'w00t')
When I run main.py I get this error:

Error:
Traceback (most recent call last): File "<stdin>", line 8, in <module> File "blink.py", line 7, in blink_led_n_times NameError: name 'led' isn't defined
So my first real question is: 1. Why is blink_led_n_times() unable to read/connect with the led object?

I'll modify blink_led_n_times() to accept an led_object as it's first parameter.

blink.py is now set to

    # blink.py>
    def blink_led_n_times(led_obj, num, t_on, t_off, msg):
        
        counter = 0
        
        while (counter < num):
            led_obj.on()
            time.sleep(t_on)
            led_obj.off()
            time.sleep(t_off)
            counter += 1
            
        print(msg)
and main.py is now set to

    # main.py> 
    import machine
    import time
    from blink import blink_led_n_times

    led = machine.Pin(13, machine.Pin.OUT)

    blink_led_n_times(led, 10,.05, .05, 'Done Blinking.')
So run it, and I get this error:

Error:
Traceback (most recent call last): File "<stdin>", line 8, in <module> File "blink.py", line 8, in blink_led_n_times NameError: name 'time' isn't defined
My second question: 2. Why is blink_led_n_times() unable to use time when I have clearly imported time into main.py

So I modify blink.py

    # blink.py>
    import time

    def blink_led_n_times(led_obj, num, t_on, t_off, msg):
        
        counter = 0
        
        while (counter < num):
            led_obj.on()
            time.sleep(t_on)
            led_obj.off()
            time.sleep(t_off)
            counter += 1
            
        print(msg)
And when I run it, it works correctly.

Last Questions.

-3. So if I want to import my own functions, do I have to declare all of their imports externally like I have done here with blink.py importing time?

-4. Is there a way to just "include" python code from a separate file, similar to how require or include works in PHP (that's the language I am most familiar with)

Thanks for reading! All help is greatly appreciated.
Reply
#2
There is no led object in blink.py. there is one in main.py, but blink.py does not import main.py.

import creates a module object, a dictionary like thing that can be used to access variables objects defined in the module. Import does npt pull in the module code to be compiled with your code.

Answer 2: same answer as above.

Answer 3: when you write modules, you need to import any modules it udes. Does not matter if that module is main.py or blink.py

Answer 4: Not that I know of. Even if it were possible you would not want to do so. It creates an external dependency on the module, making any long term mantenance difficult. Any changes to the group import would require a code review of everything using the import.
wh33t likes this post
Reply
#3
Quote:import creates a module object, a dictionary like thing that can be used to access variables objects defined in the module. Import does npt pull in the module code to be compiled with your code.

Hrm OK, I'll have to think about that a little further. I am confused by this behaviour, not too sure why.

Quote:Answer 3: when you write modules, you need to import any modules it udes. Does not matter if that module is main.py or blink.py

So executing a series of scripts that import the same external module multiple times is A-OK?

Quote:Answer 4: Not that I know of. Even if it were possible you would not want to do so. It creates an external dependency on the module, making any long term mantenance difficult. Any changes to the group import would require a code review of everything using the import.

Gotcha. So when writing external functions make them as self contained as possible?
Reply
#4
import is very different than C #include. #include pulls in code from a header file to be compiled into your module. import creates an object that is an interface to the imported module. When blink calls time.sleep(1), it does the following.

Blink says "Hey there time, can I use your sleep function?". Time says "Sure, whoever you are. Here it is. Have fun."

Importing the same module in multiple scripts is common and very efficient.

Decoupling modules is always good design. main.py should not import time. Only import wat you use directly. Blink needs time, main does not.
wh33t likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  using variables with functions imported from different files. Scordomaniac 3 1,282 May-24-2022, 10:53 AM
Last Post: deanhystad
  importing functions from a separate python file in a separate directory Scordomaniac 3 1,388 May-17-2022, 07:49 AM
Last Post: Pedroski55
  Looking for clarification related to performance Pymon 5 2,053 Apr-04-2022, 04:47 PM
Last Post: deanhystad
  Read Tensorflow Documentation - Clarification IoannisDem 0 1,174 Aug-20-2021, 10:36 AM
Last Post: IoannisDem
  *args implementation and clarification about tuple status amjass12 10 4,068 Jul-07-2021, 10:29 AM
Last Post: amjass12
  Just need some clarification Tonje 3 2,069 Oct-01-2020, 03:52 PM
Last Post: deanhystad
  Global Variables - Some Points Needing Clarification. adt 4 2,972 Nov-30-2019, 01:23 PM
Last Post: adt
  Use Variables Generated from Functions in different files to use on the main file AykutRobotics 3 2,949 Jan-01-2019, 04:19 PM
Last Post: AykutRobotics
  Importing files with Pycharm sylas 3 9,758 Jan-14-2018, 09:31 AM
Last Post: sylas
  Clarification Hammuel 2 2,881 Oct-30-2017, 12:22 PM
Last Post: Hammuel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020