Proper way to set up PYTHONPATH for django project - python

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.

Related

unittesting fails on ModuleNotFound in the file to be tested imports

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.

ImportError: cannot import name 'util' from partially initialized module 'sqlalchemy' (most likely due to a circular import)

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

Build failed: code error for my code in Readthedocs

I am trying to link my Sphinx documentation with Readthedocs. I can build the documentation locally in my computer but when I try to have Readthedocs automatically generate the documentation I get the following error:
Configuration error:
There is a programmable error in your configuration file:
Traceback (most recent call last):
File "/home/docs/checkouts/readthedocs.org/user_builds/helstrom-quantum-centroid-classifier/envs/latest/lib/python3.7/site-packages/sphinx/config.py", line 368, in eval_config_file
execfile_(filename, namespace)
File "/home/docs/checkouts/readthedocs.org/user_builds/helstrom-quantum-centroid-classifier/envs/latest/lib/python3.7/site-packages/sphinx/util/pycompat.py", line 150, in execfile_
exec_(code, _globals)
File "/home/docs/checkouts/readthedocs.org/user_builds/helstrom-quantum-centroid-classifier/checkouts/latest/docs/conf.py", line 16, in <module>
import sphinx_gallery
ModuleNotFoundError: No module named 'sphinx_gallery'
I have imported sphinx_gallery in my conf.py file as well as added the extension 'sphinx_gallery.gen_gallery'. My conf.py file is located here:
https://github.com/leockl/helstrom-quantum-centroid-classifier/blob/master/docs/conf.py
And my Github project is located here:
https://github.com/leockl/helstrom-quantum-centroid-classifier
Where have I gone wrong?

Errors running django tests after python 2/3 upgrade, unittest.loader._FailedTest

I'm helping my group upgrade their application from python 2 to 3, and am running into errors with the unit tests. When I run the tests with:
python3 manage.py test groupapp --settings=settings.deploy_dev
I get the errors below. But if I run with:
python3 manage.py test groupapp.tests --settings=settings.deploy_dev
the errors do not occur. Its worth noting that these errors occur even after I deleted all our tests from the tests folder, and as far as I can tell they aren't connected to an actual test case.
I don't understand the difference between these two calls in python 3. In python 2 if I run the two calls I get the same result (no test failures).
groupapp_v2.groupapp.admin (unittest.loader._FailedTest) ... ERROR
groupapp_v2.groupapp.models (unittest.loader._FailedTest) ... ERROR
======================================================================
ERROR: groupapp_v2.groupapp.admin (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: groupapp_v2.groupapp.admin
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 462, in _find_test_path
package = self._get_module_from_name(name)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 369, in _get_module_from_name
__import__(name)
File "/Users/matthew/bitbucket/consortium-website/groupapp_v2/groupapp/admin/__init__.py", line 73, in <module>
admin.site.register(Grid, GridAdmin)
File "/Users/matthew/virtualenv/groupapp-python3.6/lib/python3.6/site-packages/django/contrib/admin/sites.py", line 110, in register
raise AlreadyRegistered('The model %s is already registered' % model.__name__)
django.contrib.admin.sites.AlreadyRegistered: The model Grid is already registered
======================================================================
ERROR: groupapp_v2.groupapp.models (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: groupapp_v2.groupapp.models
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 462, in _find_test_path
package = self._get_module_from_name(name)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 369, in _get_module_from_name
__import__(name)
File "/Users/matthew/bitbucket/consortium-website/groupapp_v2/groupapp/models/__init__.py", line 7, in <module>
from .deployable import *
File "/Users/matthew/bitbucket/consortium-website/groupapp_v2/groupapp/models/deployable.py", line 22, in <module>
class Keyword_LibraryM2M(Model):
File "/Users/matthew/virtualenv/groupapp-python3.6/lib/python3.6/site-packages/django/db/models/base.py", line 118, in __new__
"INSTALLED_APPS." % (module, name)
RuntimeError: Model class groupapp_v2.groupapp.models.deployable.Keyword_LibraryM2M doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.```
Test discovery in unittest (the default Django test runner) changes a lot from 2.7 to 3. What you're trying to do is in fact two different ways for calling test, from the Django documentation:
# Run all the tests in the animals.tests module
$ ./manage.py test animals.tests
# Run all the tests found within the 'animals' package
$ ./manage.py test animals
it is possible that your app has more tests discovered that the ones you're explicitly telling the runner to run, it's not a problem with your app, it's just how libraries are.

Python module not found (directory problems)

I have a Python 2.5 project with following directory structure:
database/__init__.py
database/createDBConnection.py
gui/mainwindow.py
When I try to run
python gui/mainwindow.py
I get the error
C:\PopGen>python gui/mainwindow.py
Traceback (most recent call last):
File "gui/mainwindow.py", line 12, in <module>
from database.createDBConnection import createDBC
ImportError: No module named database.createDBConnection
In mainwindow.py, there is following statement on line 12
from database.createDBConnection import createDBC
The problem occurs because Python can't find the database module.
Question: What can I do in order to fix this error?
Here's the code of the project: https://www.dropbox.com/sh/edfutlba960atp9/MwFpaepEpl
I tried to use
C:\PopGen>python -m gui.mainwindow
but got these errors
Traceback (most recent call last):
File "C:\Python25\lib\runpy.py", line 95, in run_module
filename, loader, alter_sys)
File "C:\Python25\lib\runpy.py", line 52, in _run_module_code
mod_name, mod_fname, mod_loader)
File "C:\Python25\lib\runpy.py", line 32, in _run_code
exec code in run_globals
File "C:\PopGen\gui\mainwindow.py", line 13, in <module>
from file_menu.wizard_window_validate import Wizard
ImportError: No module named file_menu.wizard_window_validate
There are several ways to fix this, but this is perhaps the easiest one.
Try adding this in mainwindow.py, prior to the import that is failing:
import sys
sys.path.append("C:/path/to/database/module")

Categories