5

Im new to python and have looked at various stack overflow posts. i feel like this should work but it doesnt. How do you import a class from another file in python? This folder structure

src/example/ClassExample
src/test/ClassExampleTest

I have this class

class ClassExample:
    def __init__(self):
        pass

    def helloWorld(self):
        return "Hello World!"

I have this test class

import unittest
from example import ClassExample

class ClassExampleTest(unittest.TestCase):

    def test_HelloWorld(self):
        hello = ClassExample()
        self.assertEqual("Hello World!", hello.helloWorld())

if __name__ == '__main__':
    unittest.main()

When the unit test runs the object is None:

AttributeError: 'NoneType' object has no attribute 'helloWorld'

What's wrong with this? How do you import a class in python?

falsetru
  • 357,413
  • 63
  • 732
  • 636
slipperypete
  • 5,358
  • 17
  • 59
  • 99
  • 1
    Try `import example; print(example)` to see you actually imported the `example` module, not other module that is in python import path. – falsetru Nov 25 '20 at 04:31
  • I don't see how `hello` can be None. – John Gordon Nov 25 '20 at 04:32
  • The code shown here looks reasonable. Make sure you are importing from the correct file. Change file names to verify you are not reading from the wrong file. – RufusVS Nov 25 '20 at 04:33
  • when I do what @falsetru says it prints "" – slipperypete Nov 25 '20 at 04:33
  • It sounds like the site-package example module actually has a "ClassExample" class. What are the odds?? – RufusVS Nov 25 '20 at 04:35
  • Are those directories or Is that `src/example/ClassExample.py` and `src/test/ClassExampleTest.py`? Is there some other install going on? Your test shouldn't see `example` as a module as it stands in your source tree. Its behaving as through there is an `example.py` with a function called `CaseExample` in your test directory. – tdelaney Nov 25 '20 at 04:36
  • Okay, so you've installed this package before test and are getting `example` from python's site-packages. There is also an `__init__.py`. `from example import ClassExample` is importing "ClassExample" from `__init__.py` 's namespace. So what is in `__init__.py`? – tdelaney Nov 25 '20 at 04:40
  • @tdelaney nothing is in __init__.py – slipperypete Nov 25 '20 at 04:48
  • Does this answer your question? [Importing class from another file](https://stackoverflow.com/questions/41276067/importing-class-from-another-file) – Roshin Raphel Nov 25 '20 at 04:51
  • How do you execute `ClassExampleTest.py` (and in what directory)? – tdelaney Nov 25 '20 at 04:59

1 Answers1

6

If you're using Python 3, then imports are absolute by default. This means that import example will look for an absolute package named example, somewhere in the module search path.

So instead, you probably want a relative import. This is useful when you want to import a module that is relative the module doing the importing. In this case:

from ..example.ClassExample import ClassExample

I'm assuming that your folders are Python packages, meaning that they contain __init__.py files. So your directory structure would look like this.

src
|-- __init__.py
|-- example
|   |-- __init__.py
|   |-- ClassExample.py
|-- test
|   |-- __init__.py
|   |-- ClassExampleTest.py
Nikolas Stevenson-Molnar
  • 4,235
  • 1
  • 22
  • 31