0

I am feeling defeated by Python to the point where I am not sure what else to try. I am running Python 3.9 and I just cant for the life of me get the imports to work correctly. Here is my directory structure:

hello-world-proj
|
|.. core
   |
   |..
       __init__.py
       hello_world_main.py
|
|.. test
   |
   |..
       __init__.py
       test_hello_world_main.py

hello_world_main.py

def hello_world_main():
   return "myString"

if __name__ == "__main__":
   print(hello_world_main()) # call hello_world_main()

test_hello_world_main.py

import unittest
import os
from core import hello_world_main

class HelloWorldTest(unittest.TestCase):
   self.assertEquals(hello_world_main(), "myString")

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

When I run python test_hello_world_main.py I get hit with the error:

ImportError: cannot import name hello_world_main

I already did export PYTHONPATH=$PYTHONPATH:/path/to/hello-world-proj

I am defeated and no idea what to do now. Why is this happening?

tomiyos654
  • 37
  • 5

3 Answers3

0

Solution one:

Having export PYTHONPATH=$PYTHONPATH:/path/to/hello-world-proj already set, edit the __init__.py in the core like:

from .hello_world_main import hello_world_main

Solution two:

Run your test from the same directory and edit the test file like:

import unittest
import os
from core.hello_world_main import hello_world_main

class HelloWorldTest(unittest.TestCase):
   self.assertEquals(hello_world_main(), "myString")

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

And run your command inside the hello-world-proj :

python test/test_hello_world_main.py
aminrd
  • 4,300
  • 4
  • 23
  • 45
  • It is very weird. It only doesnt work if I include it in the ```HelloWorldTest``` class. It works if I remove it it and put it outside like a regular script. Why is this happening? So it actually works if ```b.py``` is ```from core.hello_world_main import hello_world_main print(hello_world_main()) ``` But it doesnt work if you icnlude it in the test like: ```class HelloWorldTest(unittest.TestCase): self.assertEquals(hello_world_main(), "myString") ``` – tomiyos654 Aug 17 '21 at 18:38
0

If you just want that specific function from hello_world_main.py, your import statement should be from core.hello_world_main import hello_world_main, and if you want everything in the file, try from core.hello_world_main import *.

ratiugo
  • 53
  • 1
  • 4
0

TLDR;

  1. Add a setup.py
  2. Install your package with: pip install -e .

To avoid fiddling with the import arguments I would instead create a setup.py (you probably have/need one anyway) in your top directory (hello-world-proj).

setup.py

from setuptools import setup, find_packages

setup(name="hello-world-proj", packages=find_packages())

In your project root you then pip install (preferable in your virtual environment) your project with a symbolic link to your project location with:

pip install -e .

Note that you don't have to do this after each time you change a file - just once. Read more in this answer

After that you can use your current from core import hello_world_main in you test file:

import unittest
from core import hello_world_main

class HelloWorldTest(unittest.TestCase):

    def test_hello_world(self):
        self.assertEqual(hello_world_main.hello_world_main(), "myString")

if __name__ == '__main__':
    unittest.main(exit=False)
NLindros
  • 1,683
  • 15
  • 15