Local Imports in Python: Utilizing Modules Effectively

slide1 n.w
1 / 15
Embed
Share

"Explore the benefits and limitations of local imports in Python programming. Learn different ways to import constants and modules, and understand the importance of module execution when used as a script. Discover how importing code enhances modularity, reusability, and expandability, while delving into the constraints of local imports."

  • Python Programming
  • Local Imports
  • Module Execution
  • Modularity
  • Reusability

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. ? local imports

  2. Partner chat: simple imports Consider a directory: __init__.py ( ) cooking.py contains constant ETERNAL_FLAME ( ) main.py want to use ETERNAL_FLAME here With your partner, list as many ways as you can that you could import the ETERNAL_FLAME constant to be used in main.py. (3 min) 2

  3. Notes (notes in plenum) 3

  4. Importing modules you can always import code from other modules (.py files) in your current directory Options for e.g. importing eternal_flame 1. import cooking + cooking.ETERNAL_FLAME + cooking.FIRE + cook.ETERNAL_FLAME + cook.FIRE 2. import cooking as cook 3. from cooking import ETERNAL_FLAME + ETERNAL_FLAME X FIRE 4. from cooking import * + ETERNAL_FLAME + FIRE 4

  5. Exercise 2a: Simple local imports Follow the instructions in Exercise 2a Local importing.md (There is no need to submit a pull request for this exercise) Be ready to discuss in plenum. (10 min) 5

  6. Notes (notes in plenum) 6

  7. Exercise 2b: Unprotected code Follow the instructions in Exercise 2b Unprotected code.md (There is no need to submit a pull request for this exercise) Be ready to discuss in plenum. (7 min) 7

  8. Notes (notes in plenum) 8

  9. names & mains Python executes all code in a module on import! (cute graphic at end) Any code under if __name__ == __main__ : will be ignored when importing will be executed when the module is run as a script if __name__ == __main__ : i_will_not_be_imported = True print( Does not print when importing ) print( Prints when run as script ) 9

  10. Utility & limitations Being able to import code is great. Increases modularity, reusability, testability, expandability HOWEVER. Local imports have a huge limitation. Let s explore this together. Please follow along! 10

  11. Utility & limitations Being able to import code is great. Increases modularity, reusability, testability, expandability HOWEVER. Local imports have a huge limitation. Let s explore this together. Please follow along! We can t import from another folder! Need to organize our code into a package and install it. 11

  12. ? packages and editable installation

  13. Order of execution # content of import_pkg.py import brewing Terminal > python3 ex_outside.py # content of __init__.py import_pkg.py brewing __init__.py cooking.py potion.py 17

  14. Order of execution # content of import_pkg.py import brewing.cooking Terminal > python3 ex_outside.py # content of __init__.py import_pkg.py brewing __init__.py cooking.py potion.py # content of cooking.py eternal_flame = eternal_flame 18

  15. Order of execution # import_pkg.py import brewing.make_potion Terminal > python3 ex_outside.py #__init__.py import_pkg.py brewing __init__.py # cooking.py stuff cooking.py make_potion.py # make_potion.py import cooking def coffee(): 19

More Related Content