45

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.
Latra
  • 492
  • 3
  • 14
user3868051
  • 1,147
  • 2
  • 22
  • 43

5 Answers5

38

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

python -m unittest test.test_file1.TestWriteDataBRToOS
blhsing
  • 91,368
  • 6
  • 71
  • 106
22

Also you can you do:

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

or in Windows:

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

Carolina Acosta
  • 382
  • 2
  • 4
  • You can get the current path with `pwd`, so you can `cd` to project root, then use the `pwd` command to get the directory and use it – Caridorc Feb 20 '23 at 02:53
6

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.

Sanat Deshpande
  • 186
  • 1
  • 1
  • 8
  • 2
    Yes I understand that... but I am getting this error only when I run the code via terminal on my Ubuntu VM, when I run on my local host m/c that is MAC using an IDE 'pycharm' then it works just fine! – user3868051 Jun 26 '18 at 19:25
  • 1
    Because your IDE would have added that path to its sys.path. You can check that by printing out your sys.path and pythonpath. – GuSuku Dec 27 '19 at 15:46
6

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 -a
source  .env
-1

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

  • I just removed src from the code – Shubham Chitaguppe Apr 22 '22 at 06:50
  • 2
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 22 '22 at 09:01