'Python3.6 error: ModuleNotFoundError: No module named 'src'

I know similar questions have been asked before... But I had a quick doubt... I have been following this link: https://www.python-course.eu/python3_packages.php

my code structure:

my-project
  -- __init__.py
  -- src
      -- __init__.py
      -- file1.py
  -- test
      -- __init__.py
      -- test_file1.py

test_file1.py:

import unittest
from src.file1 import *

class TestWriteDataBRToOS(unittest.TestCase):
    def test_getData(self):
    sampleData = classInFile1()
    sampleData.getData()
    self.assertNotEqual(sampleData.usrname, "")

if __name__ == '__main__':
    unittest.main()

Here I get the error:

ModuleNotFoundError: No module named 'src'

If I change to :

import sys
sys.path.insert(0, '../src')
import unittest
from file1 import *

then it works!

Can someone help me understand why it won't work like how it has been described in the link pasted above or any alternative way instead of writing the sys.path.insert(0, '../src') statement.

Thanks!

Edit:

after executing from my-project dir: python -m unittest test/test_file1/TestWriteDataBRToOS I am getting the error as updated below.

Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/usr/lib/python2.7/unittest/__main__.py", line 12, in <module>
main(module=None)
File "/usr/lib/python2.7/unittest/main.py", line 94, in __init__
self.parseArgs(argv)
File "/usr/lib/python2.7/unittest/main.py", line 149, in parseArgs
self.createTests()
File "/usr/lib/python2.7/unittest/main.py", line 158, in createTests
self.module)
File "/usr/lib/python2.7/unittest/loader.py", line 130, in loadTestsFromNames
suites = [self.loadTestsFromName(name, module) for name in names]
File "/usr/lib/python2.7/unittest/loader.py", line 91, in loadTestsFromName
module = __import__('.'.join(parts_copy))
ImportError: Import by filename is not supported.


Solution 1:[1]

You have to run the test from the my-project folder, rather than from the test folder.

python -m unittest test.test_file1.TestWriteDataBRToOS

Solution 2:[2]

Also you can you do:

export PYTHONPATH="${PYTHONPATH}:/path/to/your/project/"

or in Windows:

set PYTHONPATH="${PYTHONPATH}:/path/to/your/project/"

Solution 3:[3]

This is because it is not able to locate the module named 'src' probably because the path to the 'src' folder isn't specified correctly. If you directly write src.file1, then your 'src' file should be present in the same directory in which your current python file(test_file1.py) is located. If it isn't in the same directory, then you have to specify the entire directory. The reason why sys.path.insert(0, '../src') worked is because .. will move you up one directory and that's where your src folder might be. If you are working directory for test_file1.py is /usr/bin/python then the location of your src folder would be /usr/bin/src and since it isn't the same as your current working directory, the python file is not able to locate the 'src' module.

Solution 4:[4]

this is my folder structure

while I was trying to extract fun present in common.py file in training .py I got the error saying src module error The code was: from src.utils.common import read_config Then I tried this : from utils.common import read_config It worked?

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 blhsing
Solution 2 Carolina Acosta
Solution 3 Sanat Deshpande
Solution 4 Shubham Chitaguppe