• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Importing libraries

Q&A Forum › Category: ESP32 › Importing libraries
0 Vote Up Vote Down
David Flory asked 5 years ago

Sorry if this is a typical nooby question. I was looking at one of Rui’s projects and this was required in boot.py

import machine
from machine import Pin, I2C

Why the second line? If the whole library is imported, surely Pin and I2C are already available.

Question Tags: machine, pin
3 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi David.
Yes, you are right.
However, there are differences when it comes to the first and second line.
For example, if you just import the first line, if you want to use the Pin module, you need to do as follows to create a Pin object:

button = machine.Pin(4, machine.Pin.IN)

If you have the second line that you specifically import the Pin module, you do as follows (you don’t need to use machine):

button = Pin(4, Pin.IN)

If in your code you just use the Pin and I2C modules, you don’t need the first line.

I don’t know which specific project you are talking about. But probably you don’t need the first line.

I hope this is clear.
Regards,
Sara

0 Vote Up Vote Down
David Flory answered 5 years ago

Thanks Sara, that makes sense now you have explained it.

Just as an aside, if memory was at a premium, would it be better to import just once and use the full syntax, or is it so inconsequential to make any difference?

0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Great!
I’ll mark this issue as resolved.
If you need further help, you just need to open a new question in our forum.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.