0

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 :)

Kenly
  • 24,317
  • 7
  • 44
  • 60
Selman
  • 274
  • 2
  • 4
  • 17

2 Answers2

2

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.

mcursa-jwt
  • 141
  • 5
  • i created a empty __init__.py into my /tests dir. When i use your code its give me error. ModuleNotFoundError: No module named 'models' – Selman Oct 18 '22 at 10:00
  • do relative imports work now? – mcursa-jwt Oct 18 '22 at 10:01
  • 1
    from your code it seems like there are many layers to your application, maybe try editing the post to include the whole structure if possible? – mcursa-jwt Oct 18 '22 at 10:04
  • if you are using the debug or run function on your editor, you might want to take note of this: https://stackoverflow.com/questions/11536764/how-to-fix-attempted-relative-import-in-non-package-even-with-init-py it matters where you run the .py script from, and your debug/launch configuration should allow you to specify the exact way you need it to be run. – mcursa-jwt Oct 18 '22 at 10:06
  • @Selman sorry forgot to mention you – mcursa-jwt Oct 18 '22 at 10:06
  • i updated my question. You can see the full structure. I don't know what is relative imports but i will learn quickly and i will give you feedback. Thank you for being so helpful ! @marcus jwt – Selman Oct 18 '22 at 10:11
2

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
Kenly
  • 24,317
  • 7
  • 44
  • 60