Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to import Python files from cube.py #8443

Open
drummerwolli opened this issue Jul 9, 2024 · 1 comment
Open

Allow to import Python files from cube.py #8443

drummerwolli opened this issue Jul 9, 2024 · 1 comment

Comments

@drummerwolli
Copy link

Describe the bug
I would like to move some code out of the cube.py file as it becomes very large and hard to maintain. But I fail to make it work in the cube docker container to import the outsourced files. also, unit testing my functions becomes impossible, because if i try to import the functions from cube.py the imports fail (there is no possibility to import this line: from cube import config).

To Reproduce

  1. create the following files in one repo:

cube.py

from cube import config
from cube_utils import test_function

@config('query_rewrite')
def query_rewrite(query: dict, ctx: dict) -> dict:
    query = test_function(query)
    return query

cube_utils.py

def test_function(query: dict) -> dict:
    # do something
    return query

Dockerfile

FROM cubejs/cube:v0.35.57

COPY cube.py /cube/conf/
COPY cube_utils.py /cube/conf/
  1. build the docker file:

docker build -f test.Dockerfile -t cube .

  1. run the docker container:
docker run -it cube                                         

Cube Error ---------------------------------------

Error: Python error: ModuleNotFoundError: No module named 'cube_utils'
Traceback (most recent call last):
  File "cube.py", line 2, in <module>
    from cube_utils import test_function

Expected behavior
Cube API should start without a problem with the above setup.

Screenshots
n/a, see above

Minimally reproducible Cube Schema

not needed, see above.

Version:
v0.35.57

@drummerwolli
Copy link
Author

drummerwolli commented Jul 10, 2024

one solution: add this line in your cube.py file before adding imports from other files:

sys.path.append(os.path.dirname(os.path.abspath(__file__)))

@igorlukanin igorlukanin changed the title split cube.py in multiple files not possible Allow to import Python files from cube.py Jul 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants