Ask coding questions

← Back to all posts
Importing through a function
tankerguy1917

Is there a way to import classes/variables from a separate folder/file using a function? I tried it this way as well as trying to returning the a, b, and c variables, but it still didn't work.

Voters
tankerguy1917
Comments
hotnewtop
Coder100

hi, perhaps you are looking for the __import__ function: https://www.geeksforgeeks.org/python-__import__-function/

also don't forget about the scope. Because you imported it in a function, it can't be used outside of the function. No way around this.*

SixBeeps

Is there any reason why you'd want to do that?

tankerguy1917

@SixBeeps I'm trying to create my own, I guess you'd call it a library, and I want to make it to where instead of having to manually import everything you can just use one function to import all of the important things.