В каталоге модулей init .
Код: Выделить всё
# __init__.py
import os
list_files = os.listdir(path="/home/inf-everything/projects/python/module")
def filter_py(file_name:str) -> bool:
if file_name.endswith(".py") and file_name != "__init__.py":
return True
else:
return False
def remove_py_extension(file_name:str) -> str:
file_name_list = list(file_name)
sorted_file_list = file_name_list[len(file_name_list)-4::-1]
sorted_file_list2 = sorted_file_list[::-1]
sorted_file_name = "".join(sorted_file_list2)
return sorted_file_name
python_files = list(filter(filter_py,list_files))
python_files_without_py_extension = list(map(remove_py_extension,python_files))
__all__ = python_files_without_py_extension
< /code>
Вывод, когда я запускаю его < /p>
/home/inf-everything/projects/python/.venv/bin/python /home/inf-everything/projects/python/module/__init__.py
['test1', 'morgue_algo']
< /code>
Вот код файла, который импортирует модуль < /p>
import module
#dir(test1)[:7:-1]]
list_files = module.__all__
print(module.__all__)
method_to_use = input("enter a method : ")
string = input("enter your string :") # prompting the user with the "string" to enter something which will be stored in the form of string
try :
module.test1.hello(string)
pass
except Exception as e:
print(e)
#type: ignore
Подробнее здесь: https://stackoverflow.com/questions/796 ... ed-on-user