3

I saw a lot of questions about importing modules error but I could not manage to solve the problem with Gitlab CI pipeline.

My project structure:

├───config
├───docs
├───src
    __init__.py
│   ├───dataset
        __init__.py
│   ├───exceptions
│   ├───resources
│   └───utils
        __init__.py
└───tests
    __init__.py
    └───resources
__init__.py

I would like to run tests using pytest. I invoke this command python -m pytest -p no:cacheprovider or using unittest 'python -m unittest discover -v' from root directory and also tried to invoke from test directory. In both cases I have a problem with importing class from dataset module. What's interesting, I have two tests file.

First file imports:

import os import unittest

from src.utils.FileManager import FileManager

Second imports:

from src.dataset.DatasetHelper import DatasetHelper

The first file is passing but the second is failing with error:

from dataset import DatasetHelper ModuleNotFoundError: No module named 'dataset'

So the thing is that other modules like utils from src are imported correctly, only the dataset has a problem. I am struggling with this a few days and I completely out of ideas. I also tried to change instead of from dataset to from src.dataset. It didn't work. I can run tests in PyCharm by clicking right mous button and just run tests but not on CI environment.

What I tried:

  • Add modules to $PYTHONPATH like
    sys.path.insert(0, "/builds/USER/PROJECT/src/dataset") 
    sys.path.insert(0, "/builds/USER/PROJECT/src") 
    sys.path.insert(0, "/builds//USER/PROJECT/tests")
    

The content of PYTHONPATH before adding it was:

Current $PYTHONPATH: ['/builds/USER/PROJECT/config', '/usr/local/lib/python36.zip', '/usr/local/lib/python3.6', '/usr/local/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/site-packages']

The first module in list is config because I run script from this directory to add above modules to path. Doesn't Help

  • Run test command from root directory and add prefix src to imports in tests directory. Doesn't Help
David Buck
  • 3,752
  • 35
  • 31
  • 35
  • Check out this question for different solutions for the `No module named` issue: [PATH issue with pytest 'ImportError: No module named YadaYadaYada'](https://stackoverflow.com/questions/10253826/path-issue-with-pytest-importerror-no-module-named-yadayadayada). You have clobbered your project with unnecessary `__init__.py` files, also the `sys.path` manipulations are not necessary. – hoefling Sep 02 '18 at 15:51
  • The right way to do it is to have a `setup.py` and do a `python setup.py develop` in your CI. This will ensure your library is installed. – astrochun Apr 30 '21 at 01:17

2 Answers2

2

from dataset import DatasetHelper
ModuleNotFoundError: No module named 'dataset'

Either in src.__init__ or more proabably in src.dataset.__init__ there is the import statement from dataset import DatasetHelper. You have to rewrite it as from src.dataset import…

phd
  • 82,685
  • 13
  • 120
  • 165
1

You can try using a relative import in your __init__.py file that's inside the tests directory.

The syntax depends on the current location as well as the current location of the module,package, or object you're trying to import. Here are some examples:

from .some_module import some_class
from ..some_package import some_function
from . import some_class

Source: https://realpython.com/absolute-vs-relative-python-imports/

ejimenez90
  • 124
  • 1
  • 3