Home > front end >  ImportError: attempted relative import with no known parent package PYTHON
ImportError: attempted relative import with no known parent package PYTHON

Time:10-18

I am trying to import some functions and variables from the parent directory.

Here is what my dir looks like:

enter image description here

I want the import:

-- send_invoice func from bifatura_methods.py

-- start func from xml_generator.py

-- data_json_1, data_json_2 dicts from inputs.py

TO send_ticari_satis_invoice.py.

My working dir is:

/home/selman/PycharmProjects/15.0/custom_addons/Invoice_Integration/models/tests

When I tried for imports like:

from ..bifatura_methods import send_invoice
from ..xml_generator import start
from inputs import data_json_1, data_json_2

Output:

ImportError: attempted relative import with no known parent package

When I tried this:

from custom_addons.Invoice_Integration.models.bifatura_methods import send_invoice
from custom_addons.Invoice_Integration.models.xml_generator import start
from custom_addons.Invoice_Integration.models.tests.inputs import data_json_1,data_json_2

Output:

ImportError: cannot import name 'models' from 'odoo' (unknown location)

Any advice and help are welcome! Thank you community :)

CodePudding user response:

since your \tests is essentially a module you're importing into, you should add an empty __init__.py inside \tests.

I personally would use explicit imports:

from models.bifatura_methods import send_invoice
from models.xml_generator import start

IMO, this would help you keep your sanity if you end up having a lot more submodules.

CodePudding user response:

You can check the Odoo guidelines that we use from odoo.addons to import from Odoo addons (custom addons should also work)

Example:

from odoo.addons.Invoice_Integration.bifatura_methods import send_invoice
  • Related