How to get functions from a python file without import

BigXKu

I'm making a program that detects python files and reads their classes and functions for then to be called. How do I list the functions from another python file without importing it? and how do I call them?

Structure of the program:

  • Index.py (main py file)
  • /Content
    • /Modules
      • Modules.py
      • ChickenModule.py (module I want to get functions from)

Modules.py list all the python files from /Modules and stores them on a list. Index.py calls the functions in Modules.py (Just to keep things less messy)

ChickenModule.py has a Class named ChickenStuff that prints "I'm a chicken" whenever it's self.(something) called.

The goal of the program is for the user to be able to put .py files in modules and when running index.py the functions and classes of said .py files will be listed.

maninthecomputer

There's a built in library called importlib that might be what you need. I'm assuming that you want classes listed. You'll need inspect for this.

import os
import os.path as path
import importlib
import inspect

# get a list of all files in Content/Modules
filelist = os.listdir("Content/Modules")

# import every file ending in .py
for fname in filelist:
  if path.splitext(fname)[1] == 'py':
    my_module = importlib.import_module(path.splitext(fname)[0]) # load the module
    for _, obj in inspect.getmembers(my_module): # iterate through members
      if isinstance(obj, type): # check if members is a class
        print(obj)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get a list of classes and functions from a python file without importing it

How to import python functions from different file (in Ubuntu)

How to get the function definition in a python file without import libraries?

How to use python functions from a separate file without prefix on replit

Import only functions from a python file

import functions in file from parent directory python

Import different functions from another file in Python?

Import functions from one python file to another

Python Import functions from own file

Is there a way to import all functions(using *) from a file without the imports of that file?

How to import and get variables from another python file?

How to get only the numbers(without whitespaces) from a text file in python

How to get a specific file name from a path without the extensions python

Python / Tkinter - how to import files or functions without executing them first?

Correct way to import functions from file in python3

Import all functions from a python file one layer up

Python. Cannot import multiple functions from the file

How do I import lines from a text file without format characters (backslashes/apostrophes) in Python3

How to import shell functions from one file into another?

How to import functions from different file and use in discord.py

How to import python file from multiple urls

How to import python file from git submodule

How to import methods from a file in python 3.6

How to import python file from folder

How to import a file from the same directory in python?

How to import multiple matrices from a file in Python

Python: how import excel file from the web?

how to import data from text file without any delimiter or separator?

How to call functions from another file in python