#Create & import library in Python
🔻CLICK TO WATCH!
Tutorial - Part - 4 |
↯ BEFORE ADDING Advance FEATURE WE NEED TO LEARN TO MAKE OUR OWN LIBRARY
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
- First package name = My_library
- then folder = file_1
- & 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 |
- import os
- import random
- from random import randint
- def play_songs():
- n = random.randint(0,10)
- print(n)
- music_dir = 'D:/Japanese Songs/audio'
- songs = os.listdir(music_dir)
- print(songs)
- os.startfile(os.path.join(music_dir,songs[n]))
from AI_assistant.Features.Play_songs import play_songs
- elif "play songs" in query:
- play_songs()