Why I cannot import a function from a class in python?
python import module from directory
python import function from file
python-import all functions from file
python import example
python import package
importerror: cannot import name
python import class from another file
Here's a file named "importable" where I try to import the method test function.
class SoonImported: def __init__(self, x): self.x = x def methodTest(self): print(self.x)
In another file I have this line of code that cannot be changed (because my example is based on a homework).
from importable import SoonImported, methodTest
When I try to run this script it gives me an Import error and says that it "cannot import name 'methodtest'". How should I correct the soonImported class in order to make it work?
Thanks for your help.
It looks like
methodTest is defined inside of
SoonImported. So it would be addressed like this
from importable import SoonImported and then
methodTest would be called like
instance = SoonImported() instance.methodTest()
What is __init__.py ? – Python Tips, file, this will be declared at the top of the code, under any shebang lines or general comments. The most Pythonic way to import classes from other directories is by using packages. Inside our directory we have two additional subdirectories: air and water. Inside the first directory, there is file plane.py with the class Plane. Inside the directory, there is an __init__.py file. This file is essential in Python 2 and older versions of
You can't import a method from a class without the rest of the class because the method only exists within the context of the class. Think of importing like borrowing. You want to borrow a lawn mower from your friend. Except that what you have there is like trying to borrow a lawn mower from your friend without making friends with anybody first. If you don't have any friends, how can you expect to borrow lawn mowers from anybody?
treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. The argument to the import function can be a single name, or a list of multiple names. Each of these names can be optionally renamed via as. For example, this would be a valid import statement in start.py: import packA as pA, packA.a1, packA.subA.sa1 as sa1. Example: start.py needs to import the helloWorld() function in sa1.py
Assuming your homework is correctly written, it seems likely that the methodTest should be a function rather than a method of the SoonImported class. I'm guessing in the second file you have lines somewhere of something similar to:
SoonImported_object = SoonImported("Some Text") methodTest(SoonImported_object)
If this is the case, then methodTest is not a method, but a function and should be defined as something like the following;
class SoonImported: def __init__(self, x): self.x = x def methodTest(SoonImported_object): print(SoonImported_object.x)
I'm embarassed to admit this, but maybe it will help someone else from doing the same stupid thing. I started out writing funky.py in the Python classes provide all the standard features of Object Oriented Programming: the class inheritance mechanism allows multiple base classes, a derived class can override any methods of its base class or classes, and a method can call the method of a base class with the same name.
Just import the class and use it like your normally would
class SoonImported: def __init__(self, x): self.x = x def method_test(self): print(self.x)
from importable import SoonImported a = SoonImported(1) a.method_test()
python3.7 vash.py 1
We can define this function in a module then import it into the various the file, we can have definitions and implementations of classes, variables, or functions. we are importing only the function that is needed rather than import the whole module (which contains more useless functions which python will waste time importing them) Whether you import a module or import a function from a module, Python will parse the whole module. Either way the module is imported.
Now enter the Python interpreter and import this module with the last item can be a module or a package but can't be a class or function or Traps for the Unwary in Python’s Import System¶ Python’s import system is powerful, but also quite complicated. Until the release of Python 3.3, there was no comprehensive explanation of the expected import semantics, and even following the release of 3.3, the details of how sys.path is initialised are still somewhat challenging to figure out.
The import statement is syntactic sugar for this function. The importlib.abc module contains all of the core abstract base classes used by import If the module cannot be loaded, ImportError is raised, otherwise the loaded module is returned. This assumes that the module is in the same directory as mainprogram.py, or is a default module that comes with python. You leave out the '.py' at the end of the file - it is ignored. You normally put all import statements at the beginning of the python file, but technically they can be anywhere.
Use classes & functions defined in another file. • A Python module is imported. • After importing className, you can just use it without a module prefix. It's brought into the private and can't be accessed outside of class. • Note: Names with The Python programming language comes with a variety of built-in functions. Among these are several common functions, including: These built-in functions, however, are limited, and we can make use of modules to make more sophisticated programs. Modules are Python .py files that consist of Python code. Any Python file can be referenced as a module.
- Why would even want to import a method? You have to create a
SoonImportedinstance before you can use the method anyway. Importing a method wouldn't achieve anything useful.
- What are you trying to do? Methods are properties of the class and of the class instances. If you import the class, the methods are included.
- How is methodTest called in the file you aren't allowed to edit? I think this will help us know how it should be defined. Is it called as methodTest() or methodTest(something)? The self object is passed automatically to a method inside a class, but not for a general function definition.
- methodTest is just called in a file that I cannot edit. We write the code and then copy paste it to a page where it is tested. That's just how it is and I can do nothing to change it. But methodTest is called in original assignment methodTest(stringname). But that happens only after it is imported.
- Cannot do that. I can change only the soonImported class (and the file where it is). It is a homework.
- @mrSimple If you can only change the class, why do you need to import it?
- @mrSimple It literally isn't possible to import a method. Either you misunderstood the homework assignment, or the homework assignment is impossible.
- Glad I could help.