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 unittestfrom 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 syssys.path.insert(0, '../src')import unittestfrom 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_codeexec code in run_globalsFile "/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 parseArgsself.createTests()File "/usr/lib/python2.7/unittest/main.py", line 158, in createTestsself.module)File "/usr/lib/python2.7/unittest/loader.py", line 130, in loadTestsFromNamessuites = [self.loadTestsFromName(name, module) for name in names]File "/usr/lib/python2.7/unittest/loader.py", line 91, in loadTestsFromNamemodule = __import__('.'.join(parts_copy))ImportError: Import by filename is not supported.
Best Answer
You have to run the test from the my-project
folder, rather than from the test
folder.
python -m unittest test.test_file1.TestWriteDataBRToOS
If you are encountering a 'ModuleNotFoundError: No module named 'src'' error, here are some steps to fix it:
1. Check if the 'src' module is installed in your Python environment. You can do this by running the command 'pip list' in your terminal and checking the list of installed packages. If 'src' is not listed, you need to install it.
2. Install the 'src' module by running the command 'pip install src' in your terminal. This will download and install the module from the Python Package Index (PyPI).
3. After installing the 'src' module, make sure you import it correctly in your Python script or program. You can use the statement 'import src' at the beginning of your code.
4. If you are still encountering the 'ModuleNotFoundError' after following these steps, double-check the spelling and capitalization of the module name. Python is case-sensitive, so 'src' and 'Src' are different.
Also you can you do:
export PYTHONPATH="${PYTHONPATH}:/path/to/your/project/"
or in Windows:
set PYTHONPATH="${PYTHONPATH}:/path/to/your/project/"
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 your 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.
The best way to get rid of this error:
1- create .env file and add the following line in that file:
export PYTHONPATH="$PYTHONPATH:$PWD"
Save the file and exit.
2- In the current directory of the code open a terminal and run:
set -asource .env
this is my folder structure
while I was trying to extract fun present in common.py file in training .pyI got the error saying src module errorThe code was: from src.utils.common import read_configThen I tried this : from utils.common import read_configIt worked😊