简体   繁体   中英

How can I import a Python class that is in two directories above and one below?

How can I import a CustomerHelper class inside customer_helper.py from customer_helper_test.py ? It's possible? I used from ..helpers..tests..app.helpers.customer_helper import CustomerHelper but it's invalid syntax.

Here is the folders organized:

program/
    app/
        helpers/
            customer_helper.py
            __init__.py
    __init__.py
    tests/
        helpers/
            customer_helper_test.py
            __init__.py
        __init__.py
    __init__.py

Thanks in advance!

Always strive to use absolute imports.

from program.app.helpers.customer_helper import CustomerHelper

If for some reason you absolutely can't, then

from ...app.helpers.customer_helper import CustomerHelper

Note that app must be a package regardless.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM