Python import error modules

Asked

Viewed 931 times

1

I’m having trouble importing the modules into my project. I was creating some tests and I can’t import the main of my project to test an end point from my test file teste_ex.py. Follows 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 to 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 just get ImportError: No module named 'api'

1 answer

2

I don’t 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 this way:

from .api import main

But only after you create the module.

The . before the name of the "folder" or module refers to a parent folder. More than one can be used to move up levels.

  • That would be creating a Unit and importing into it?

  • If by "Unit" you mean "an uninstalled module," I believe so.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.