Before you mark it as duplicate please read my problem: Show
I am trying to import a class from a file from a subdirectory
and in putting in main.py:
I am getting the error:
When I try to use just:
I get this error:
I have put an empty If main and file are in the same folder this work:
but i want them to be in separate files. Can someone tell me what i am doing wrong? When the program is very complex, we often divide it into chunks or modules and are saved into multiple files. This reduces the complexity and makes it easy to debug or identify errors in the code. Suppose you want to use a class that is in another file. For this, you need to import that class into the file where you want to use it. In this tutorial, we are going to discuss some methods which are used to import classes. Importing classes from other programs allows us to use them within the current program. Thus, helping in improved readability and reusability of code. Importing can be done within the same or from different folders. If you want to learn more about python Programming, visit Python Programming Tutorials. Some of the common ways to import a class from another file in Python are listed below
Importing a specific class by using the import commandYou just have to make another .py file just like MyFile.py and make the class your desired name. Then in the main file just import the class using the command line from MyFile import Square. #Code starts here MyFile.py class Square: def __init__(self,val): self.val=val def getVal(self): return self.val*self.val main.py from MyFile import Square newClass = Square(5) val = newClass.getVal() print(val) #Code ends here
IMPORT MULTIPLE CLASSES FROM ONE FILE USING IMPORT COMMANDWhen you have multiple classes in the MyFile.py. Then instead of writing import command with every class you just have to import the file that contains the classes and in the main.py file use these classes by making their respective objects. #Code starts here MyFile.py class Square: def __init__(self,val): self.val=val def getVal(self): return self.val*self.val class Add_Sub: def add(self, a, b): return a + b def sub(self, a, b): return a - b main.py import MyFile # creating object for Square class object1 = MyFile.Square(5) print(f"From class Square: {object1.getVal()}") # creating object for Add_Sub class object2 = MyFile.Add_Sub() print(f"From class Add_Sub: Addition {object2.add(2,3)}") print(f"From class Add_Sub: Subtraction {object2.sub(2,3)}") #Code ends here
IMPORT All CLASSES FROM ONE FILE USING IMPORT * COMMANDHere you just have to write an asterisk (*) with the import command i.e, import*. This command allows access to all the classes and you don’t need to write the name of the class with every function. You just have to make the object with that respective class. #Code starts here MyFile.py class Square: def __init__(self,val): self.val=val def getVal(self): return self.val*self.val class Add_Sub: def add(self, a, b): return a + b def sub(self, a, b): return a - b main.py from MyFile import * # creating object for Square class object1 = Square(5) print(f"From class Square: {object1.getVal()}") # creating object for Add_Sub class object2 = Add_Sub() print(f"From class Add_Sub: Addition {object2.add(2,3)}") print(f"From class Add_Sub: Addition {object2.sub(2,3)}") #Code ends here
IMPORT All CLASSES FROM ANOTHER FOLDER in parent directory USING IMPORT SYS COMMANDHere Inner_Project is the folder that is holding the file of our classes. The main file is in another folder ‘Project2’ which is also the parent folder of the Inner_Project. Before importingwe have to include the __init__.py file in the Inner_Project that will contain our classes to tell the interpreter that our Project is within the same package. sys.path.insert(0,“..”) command tells the interpreter to look from the parent folder to take out the desired class. Address: Application/Inner_Project/MyFile.py MyFile.py class Square: def __init__(self,val): self.val=val def getVal(self): return self.val*self.val class Add_Sub: def add(self, a, b): return a + b def sub(self, a, b): return a - b Address: Application/Project2/main.py #Code starts here main.py import sys sys.path.insert(0,"..") from Inner_Project.MyFile import Square object = Square(5) print(object.getVal()) #Code ends here
IMPORTing A CLASS DYNAMICALLYHere the __import__(module_name) command is used to import the module or file, whereas getattr() command is used to get the attributes of the class. #Code starts here module.py # class inside the module class MyClass: def myclass(str): print(f"Hello How are you {str} ?") Dynamic.py class Dyclass: def __init__(self, module_name, class_name): module = __import__(module_name) # __import__ method used to getmodule my_class = getattr(module, class_name) #getting attribute by getattr() my_class.myclass('Usman') obj = Dyclass("module", "MyClass") #Code ends here
Please let us know your valuable feedback. How do you call an object from another file in Python?Approach: Create a Python file containing the required functions. Create another Python file and import the previous Python file into it. Call the functions defined in the imported file.
How do I call a Python function from another directory?The most Pythonic way to import a module from another folder is to place an empty file named __init__.py into that folder and use the relative path with the dot notation. For example, a module in the parent folder would be imported with from .. import module .
How do you import a custom class in Python?How to import a class from another file in Python. sys. path. append("."). from my_file import myClass.. newClass = myClass(5). val = newClass. getVal(). print(val). How do you inherit a class from another package in Python?To create a class that inherits from another class, after the class name you'll put parentheses and then list any classes that your class inherits from. In a function definition, parentheses after the function name represent arguments that the function accepts.
What does __ init __ do in Python?The __init__ method is the Python equivalent of the C++ constructor in an object-oriented approach. The __init__ function is called every time an object is created from a class. The __init__ method lets the class initialize the object's attributes and serves no other purpose. It is only used within classes.
|