'Cant import function to pytest file

I have a directory structure like follows -

jobs/
  src/
    main/
      airflow-dags/
        __init__.py
        create_table.py
tests/
  test_create_table.py

My test_create_table.py file

import pytest
import sys, os
sys.path.insert(1, 'jobs/src/main/airflow-dags')
sys.path.insert(0, os.path.dirname(__file__))
from create_table import create_table_func


class TestCreatePartitionTable:
    def test_create_table(self):
        return True

I can't seem to import my create_table file so I can run tests on the functions for this file. I have added both those sys inserts that I found while searching for a solution, but neither is working. I tried both of them individually as well, no luck. What am I missing?



Solution 1:[1]

Package names are relative to the project root by default, so you need something like this:

from jobs.src.main.airflow-dags.create_table import create_table_func

However, the - in airflow-dags causes a problem because python doesn't allow a dash in a package name. I suggest renaming it to airflow_dags.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1