Mouna Balghouthi
12/05/2023, 9:32 AMsrc
│ ...
└───<package_name>
│ └───pipelines
│ └───data_engineering
| └───nodes
│ │ node_X.py
└───tests
│ └───pipelines
│ └───data_engineering
| └───nodes
│ │ test_node_X.py
In my test file, I have to add the first 2 lines else I will get an Import error:
# Uncommenting the following 2 lines solves the error
# import sys
# sys.path.append("path_to/src")
import pytest
from <package_name>.pipelines.data_engineering.nodes.node_X import (
func_X,
)
Is there a cleaner way than having to append the path for each test file?
ERROR Message:
==================================== ERRORS ======================================================================================
________________________________________________ ERROR collecting src/tests/pipelines/data_engineering/nodes/test_node_X.py ________________________________________________
ImportError while importing test module 'path_to/src/tests/pipelines/data_engineering/nodes/test_node_X.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/home/ec2-user/miniconda3/envs/python310/lib/python3.10/importlib/__init__.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
src/tests/pipelines/data_engineering/nodes/test_node_X.py:7: in <module>
from <package_name>.pipelines.data_engineering.nodes.node_X import (
E ModuleNotFoundError: No module named '<package_name>'
Thank you.datajoely
12/05/2023, 9:44 AMdatajoely
12/05/2023, 9:45 AMMouna Balghouthi
12/05/2023, 9:46 AMdatajoely
12/05/2023, 9:50 AM/home/ec2-user/miniconda3/envs/python310/lib/python3.10
Mouna Balghouthi
12/05/2023, 9:58 AMMouna Balghouthi
12/05/2023, 10:05 AMdatajoely
12/05/2023, 10:06 AMdatajoely
12/05/2023, 10:06 AMMouna Balghouthi
12/05/2023, 10:18 AM=============================================================================== test session starts ===============================================================================
platform linux -- Python 3.10.13, pytest-7.4.3, pluggy-1.2.0
rootdir: path_to_directory_containing_src
configfile: pyproject.toml
plugins: cov-4.1.0
collected 1 item
src/tests/test_run.py . [100%]/home/ec2-user/miniconda3/envs/python310/lib/python3.10/site-packages/coverage/control.py:883: CoverageWarning: No data was collected. (no-data-collected)
self._warn("No data was collected.", slug="no-data-collected")
---------- coverage: platform linux, python 3.10.13-final-0 ----------
Name Stmts Miss Cover Missing
------------------------------------------------------------------------------------------------------------------
src/markdown/__init__.py 1 1 0% 3
src/markdown/__main__.py 30 30 0% 2-46
src/markdown/pipeline_registry.py 36 36 0% 3-95
src/markdown/pipelines/__init__.py 0 0 100%
src/markdown/pipelines/data_engineering/__init__.py 3 3 0% 3-7
src/markdown/pipelines/data_engineering/nodes/__init__.py 0 0 100%
src/markdown/pipelines/data_engineering/nodes/node_X.py 203 203 0% 3-789
However, when I put back my test file test_node_X.py I'd still get the import error. 🤷🏻♀️datajoely
12/05/2023, 10:19 AMMouna Balghouthi
12/05/2023, 10:21 AM# import sys
# sys.path.append("rootdir_path/src")
import pytest
from <package_name>.pipelines.data_engineering.nodes.node_X import (
func_X,
)
....
@pytest.mark.parametrize(...)
def test_funcX(...):
.....
datajoely
12/05/2023, 10:22 AMdatajoely
12/05/2023, 10:23 AMpip install -e .
(installing the local package in dynamic editable mode) at the top level of your project will make things easierMouna Balghouthi
12/05/2023, 10:24 AMMouna Balghouthi
12/05/2023, 10:47 AM> python print_sys_path.py
['rootdir_path', '/home/ec2-user/miniconda3/envs/python310/lib/python310.zip', '/home/ec2-user/miniconda3/envs/python310/lib/python3.10', '/home/ec2-user/miniconda3/envs/python310/lib/python3.10/lib-dynload', '/home/ec2-user/miniconda3/envs/python310/lib/python3.10/site-packages']
The line I am adding in my test file:
import sys
sys.path.append("rootdir_path/src")
That's why I am doubting my project structure but looks similar to the documentationMouna Balghouthi
12/05/2023, 10:48 AMpip install -e .
outputs this error that I'd need more time to look into:
Obtaining file:///home/ec2-user/environment/project_rootdir
Installing build dependencies ... done
Checking if build backend supports build_editable ... done
Getting requirements to build editable ... error
error: subprocess-exited-with-error
× Getting requirements to build editable did not run successfully.
│ exit code: 1
╰─> [14 lines of output]
error: Multiple top-level packages discovered in a flat-layout: ['common', 'markdown'].
To avoid accidental inclusion of unwanted files or directories,
setuptools will not proceed with this build.
If you are trying to create a single distribution with multiple packages
on purpose, you should not rely on automatic discovery.
Instead, consider the following options:
1. set up custom discovery (`find` directive with `include` or `exclude`)
2. use a `src-layout`
3. explicitly set `py_modules` or `packages` with a list of names
To find more information, look for "package discovery" on setuptools docs.
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error
× Getting requirements to build editable did not run successfully.
│ exit code: 1
╰─> See above for output.
note: This error originates from a subprocess, and is likely not a problem with pip.
datajoely
12/05/2023, 10:49 AMMouna Balghouthi
12/05/2023, 11:19 AMerror: subprocess-exited-with-error
To give you more context about the project structure:
package_name= my_ds_project
root_dir
| ...
| requirements.txt
| pyproject.toml
| my_ds_project
| │ ...
| └───spec_folder
| │ ...
| | requirements.txt
| └───my_ds_project
| │ ...
└───src
│ ...
| setup.py
└───<my_ds_project
│ └───pipelines
│ └───data_engineering
| └───nodes
│ │ node_X.py
└───tests
│ └───pipelines
│ └───data_engineering
| └───nodes
│ │ test_node_X.py
Ben Horsburgh
12/05/2023, 11:34 AMpath_to/src/tests/pipelines/data_engineering/nodes/test_node_X.py
looks wrong - I would expect tests/pipelines/data_engineering/nodes/test_node_X.py
.
My hunch is you are either running the tests from the wrong location, with the wrong command, or in an IDE without adding src
as a source rootJuan Luis
12/05/2023, 11:38 AMMultiple top-level packages discovered in a flat-layout: ['common', 'markdown'].
To avoid accidental inclusion of unwanted files or directories,
setuptools will not proceed with this build.
the project structure is non-standard, that's why pip install -e .
doesn't workJuan Luis
12/05/2023, 11:39 AMcommon
and markdown
directories?Mouna Balghouthi
12/05/2023, 2:15 PMpip install -e .
@Juan Luis Yes, I tried from different directories.
Yes common
and markdown
are on the same level inside the root_dir and is markdown
is my_ds_project directoryMouna Balghouthi
12/05/2023, 2:16 PMin an IDE without adding@Ben Horsburgh I am looking into thisas a source rootsrc
Mouna Balghouthi
12/06/2023, 2:58 PMkedro test
runs the tests successfully but pytest
not and I get this warning:
DeprecationWarning: Command 'kedro test' is deprecated and will not be available from Kedro 0.19.0. Use the command 'pytest' instead.
datajoely
12/06/2023, 2:59 PMkedro 0.19.x
it won’t go away, but it should be doing the same thingMouna Balghouthi
12/06/2023, 3:00 PMMouna Balghouthi
12/08/2023, 9:07 AM/src/tests/pipelines
and in /src/tests
We need to delete the init.py in /src/
(not sure why we had one there in the first place)
This would then mirror also the structure in https://github.com/WaylonWalker/kedro-spaceflights/tree/feature/project-template/src
Then it should work to just import the package_name under src since it automatically then takes the src folder as root.
Thank you @datajoely @Ben Horsburgh @Juan Luis for all your contributions .gratitude thank youdatajoely
12/08/2023, 9:51 AMdatajoely
12/08/2023, 9:51 AM