Python import error modules

1

I'm having trouble importing the modules into my project. I was creating some tests and I can not import the main from my project to test an end point, from my test file teste_ex.py . Here is the structure of the project:

backend_api/
    api/
        __init__.py
        main.py
    testes/
        __init__.py
        test_ex.py

So in my test_ex.py I'm trying to import main as follows:

import api.main
from webtest import TestApp

def test_functional_concursos_api():
    app = TestApp(main.app)
    assert app.get('/hello').status == '200 OK'

I only get ImportError: No module named 'api'

    
asked by anonymous 02.10.2016 / 01:47

1 answer

2

I do not recommend importing modules belonging to a "parent" folder.

That said, if there is no other solution, my advice is to create a package with your module and make a relative import .

Then you can import it this way:

from .api import main

But only after creating the module.

The . before the name of the "folder" or module refers to a parent folder. You can use more than one to raise levels higher.

    
02.10.2016 / 20:52