I am writing a small unittest for my method move(). The application is running but when I do python -m unittest app.test.test_move_items, it fails with the following error:
ImportError: Failed to import test module: test_move
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/unittest/loader.py", line 154, in loadTestsFromName
module = __import__(module_name)
File "/Users/daniel/app/test/test_move.py", line 13, in <module>
from app.move_items import move
File "/Users/daniel/apps/app/move.py", line 29, in <module>
from helpers.calculator import determine_move_amount
ModuleNotFoundError: No module named 'helpers'
If i do python app/move.py, the output is correct.
The folder structure is as follows:
app
helpers
|-calculator.py
move_items.py
test
|-test_move_items.py
move_items.py
from helpers.calculator import determine_move_amount
def move(list_of_items, move_by):
pass
There is init.py file in app and the test directory as well.
Related
I have the following folder structure:
The content of my files are as follows:
library.py:
def hello():
return "Hello World"
main.py:
from library import hello
def main_func():
return hello()
test_main.py:
import unittest
from src.main import main_func
class TestStringMethods(unittest.TestCase):
def test_upper(self):
self.assertEqual(main_func(), "Hello World!")
if __name__ == "__main__":
unittest.main()
Now, when I run my unittests using the following command:
python3 -m unittest discover test
I get the following error:
ImportError: Failed to import test module: test_main
Traceback (most recent call last):
File "/Users/user/.pyenv/versions/3.8.11/lib/python3.8/unittest/loader.py", line 436, in _find_test_path
module = self._get_module_from_name(name)
File "/Users/user/.pyenv/versions/3.8.11/lib/python3.8/unittest/loader.py", line 377, in _get_module_from_name
__import__(name)
File "/Users/user/Desktop/example/test/test_main.py", line 3, in <module>
from src.main import main_func
File "/Users/user/Desktop/example/src/main.py", line 1, in <module>
from library import hello
ModuleNotFoundError: No module named 'library'
Can someone please explain how I can get this to work? I am deploying functions to AWS lambda so need the import structure as it is otherwise it won't work. Is there a way to configure unittests to work with this import structure?
I am using the ChatterBot library to create a chatbot and I have come across this error. I tried to create a chatbot object in my program but I get this compile time error
Traceback (most recent call last):
File "d:\TwitterBot1\Bot1.py", line 123, in <module>
bot = ChatBot('Bot1')
File "D:\Python\lib\site-packages\chatterbot\chatterbot.py", line 34, in __init__
self.storage = utils.initialize_class(storage_adapter, **kwargs)
File "D:\Python\lib\site-packages\chatterbot\utils.py", line 54, in initialize_class
return Class(*args, **kwargs)
File "D:\Python\lib\site-packages\chatterbot\storage\sql_storage.py", line 22, in __init__
from sqlalchemy import create_engine
File "D:\Python\lib\site-packages\sqlalchemy\__init__.py", line 8, in <module>
from . import util as _util # noqa
ImportError: cannot import name 'util' from partially initialized module 'sqlalchemy' (most likely due to a circular import) (D:\Python\lib\site-packages\sqlalchemy\__init__.py)
I looked into the file that is in the error and the issue is with this import line
from . import util as _util # noqa
and when I try running the __init__.py file, I get this error
Traceback (most recent call last):
File "d:\Python\Lib\site-packages\sqlalchemy\__init__.py", line 8, in <module>
from . import util as _util # noqa
ImportError: attempted relative import with no known parent package
all of the other imports in the __init__.py file have some file directive of sorts after the "." following the "from" instruction. I am not sure if this file was missing some directive or what.
Can anyone point me in the direction of a fix?
I figured this out. Turns out that sqlalchemy 1.4(verson of this time) is not compatible with the current version of chatterbot so I deleted and reinstalled sqlalchemy with version 1.2. Then there was a time.clock error in one of the files in sqlalchemy so I replaced that with time.perf_counter() and everything works now
My folder structure is as follows.
main
main.py
library
utils.py
utils.py has the following code
from aws_requests_auth.aws_auth import AWSRequestsAuth
def testAuth():
print('some code code')
main.py has the following code
from library.utils import testAuth
def start():
print ('some code')
I have run pip install aws_requests_auth in both my main and library folder.
When i tried to run python3 main.py I got this error
Traceback (most recent call last):
File "main.py", line 1, in <module>
from library.utils import testAuth
File "/Users/Docs/library/utils.py", line 1, in <module>
from aws_requests_auth.aws_auth import AWSRequestsAuth
ModuleNotFoundError: No module named 'aws_requests_auth'
How do i ensure 3rd party libraries are installed properly for sub folders? Thanks!
I have the following python file.
import sys
sys.path.insert(1, '/usr/local/google_appengine')
sys.path.insert(1, '/Users/vinay/google-cloud-sdk/platform/google_appengine/')
sys.path.insert(1, '/Users/vinay/google-cloud-sdk/platform/google_appengine/lib/yaml/lib')
from google.appengine.api import users
This is a unit test python file.
However, when I run the unit test I get the following error
/Users/vinay/App-Engine/xxxx/x/env/bin/python xxx/Applications/PyCharm.app/Contents/helpers/pycharm/utrunner.py /Users/vinay/App-Engine/xxxx/x/tests/test_oauth2callback.py::TestOauth2callback true
Testing started at 10:12 PM ...
Traceback (most recent call last):
File "/Applications/PyCharm.app/Contents/helpers/pycharm/utrunner.py", line 129, in <module>
module = loadSource(a[0])
File "/Applications/PyCharm.app/Contents/helpers/pycharm/utrunner.py", line 43, in loadSource
module = imp.load_source(moduleName, fileName)
File "/Users/vinay/App-Engine/xxxx/x/tests/test_oauth2callback.py", line 6, in <module>
from google.appengine.api import users
ImportError: No module named appengine.api
Process finished with exit code 1
In my file django app tests.py, I import another module I create called helpers. helpers is on the same directory level as tests.py. When I run ./manage.py tests I get this error:
$ ./manage.py test
Creating test database for alias 'default'...
E
======================================================================
ERROR: api.tests (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/case.py", line 58, in testPartExecutor
yield
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/case.py", line 577, in run
testMethod()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/loader.py", line 32, in testFailure
raise exception
ImportError: Failed to import test module: api.tests
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/loader.py", line 312, in _find_tests
module = self._get_module_from_name(name)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/loader.py", line 290, in _get_module_from_name
__import__(name)
File "/Users/bli1/Development/projects/cherngloong/cherngloong/api/tests.py", line 8, in <module>
from helpers.APIHelpers import KeyGrabber
ImportError: No module named 'helpers'
Here is my project structure:
Basically, tests.py can't find the helpers module. I'm not sure what is the best/proper/standard way to avoid this problem within a django project
Try this:
api.helpers.APIHelpers import KeyGrabber
in Django packages often start with the app name. You can also try relative imports.