0

Here is my code in the main file:

from File2.Test import test

test()

And here is the code for the file containing the class:

class Test:
    def test(self):
        print('Test')

As you can see, I don't just want to import the class, I want to import something from the class. When I try the syntax above, I get this error: ModuleNotFoundError: No module named 'File2.Test'; 'File2' is not a package. If there is anyway to just import test() from File2, please let me know. Any help would be appreciated!

1 Answer 1

1

from File2.Test import test

Python interprets this as you asking for a function called test from a file called Test.py in a directory called File2 (which also happens to have an __init_.py), thinking that File2 is a package.

Rather, you are trying to import a class method from a different file. The preferred way of calling such a method is to import the class definition and the call the method fromt eh class:

from File2 import Test

test = Test.test

Now, you have a way of calling test. However, as long as test is not static, you'll run into issues in actually calling it

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.