AI Assistant | Part - 4 : Create & import library in Python

#Create & import library in Python

🔻CLICK TO WATCH!

Tutorial - Part - 4

↯ BEFORE ADDING Advance FEATURE WE NEED TO LEARN TO MAKE OUR OWN LIBRARY

Q.difference between package and library
->Package is a collection of modules. It must contain an init.py file as a flag so that the python interpreter processes it as such. The init.py could be an empty file without causing issues. A library is a collection of packages



#Explanation

we are making a library where we keep our packages and use them easily [library is a collection of packages]

# What we simply did is!

Made a folder name as My_library as the main folder, under this folder we have one sub_folder name: Package, and in it, we have __init__.py, file_1.py & file_2.py

Where we defined a function name functiom_1() in file_1.py and called this function in main.py and in main.py we imported

  • from My_library.package.file_1 import function_1, function_2

  1. First package name  = My_library
  2. then folder = file_1
  3. & then the function name = functiom_1()

for pycharm Just call the function in main.py without importing it automatically recommends you(just click on it), as seen in the image but for vs_code you need to manually type!

It will auto_recommend you(in pycharm)

⇛ WHY WE MADE __init__.py FILE!

The __init__.py file makes Python treat directories containing it as modules. Furthermore, this is the first file to be loaded in a module, so you can use it to execute code that you want to run each time a module is loaded, or specify the submodules to be exported.

Implementation in AI


#Feature: Play_song.py
Make this NEW_file inside Features
  1. import os
  2. import random
  3. from random import randint

  4. def play_songs():
  5. n = random.randint(0,10)
  6. print(n)
  7. music_dir = 'D:/Japanese Songs/audio'
  8. songs = os.listdir(music_dir)
  9. print(songs)
  10. os.startfile(os.path.join(music_dir,songs[n]))
#Add in main.py
Import and call the function
from AI_assistant.Features.Play_songs import play_songs
  1. elif "play songs" in query:
  2. play_songs()
Similarly, you need to keep adding new features in a new filE AND CALL AND IMPORT IN MAIN.PY

#Full code of part - 4

#JARVIS-PART-4-CODE

PART-5 COMING SOON! WITH ADVANCED FEATURES
COMMENT IF YOU FACE ANY PROBLEM

Post a Comment

If you have any doubts, please let me know

Previous Post Next Post