Django error with test module while running tests - python

When i run python manage.py tests i get an error saying that some test module is not found.
I am using PyCharm, Django 2.1.4 and W10 on Ubuntu.
The error:
======================================================================
ERROR: projectname.projectname (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: projectname.projectname
Traceback (most recent call last):
File "/usr/lib/python3.6/unittest/loader.py", line 462, in _find_test_path
package = self._get_module_from_name(name)
File "/usr/lib/python3.6/unittest/loader.py", line 369, in _get_module_from_name
__import__(name)
ModuleNotFoundError: No module named 'projectname.projectname'
What I've tried
python manage.py runserver and it runs just fine.
Add projectname to INSTALLED_APPS
Create and app called tests
My project structure
Django
│   ├── requirements.txt
│   └── projectname
│   ├── __init__.py
│   ├── manage.py
│   └── projectname
│   ├── apps
│   │   ├── accounts
│   │   │   ├── admin.py
│   │   │   ├── apps.py
│   │   │   ├── __init__.py
│   │   │   ├── migrations
│   │   │   │   ├── __init__.py
│   │   │   ├── models
│   │   │   │   ├── __init__.py
│   │   │   │   ├── profiles.py
│   │   │   │   └── users.py
│   │   │   ├── serializers
│   │   │   │   └── __init__.py
│   │   │   ├── tests.py
│   │   │   ├── urls.py
│   │   │   └── views
│   │   │   └── __init__.py
│   │   ├── __init__.py
│   ├── db.sqlite3
│   ├── __init__.py
│   ├── settings
│   │   ├── base.py
│   │   ├── development.py
│   │   ├── production.py
│   ├── static
│   ├── templates
│   ├── urls.py
│   └── wsgi.py
I just want to run my tests like in any other django project...
I have never encountered this problem before so any help is appreciated! :)

Well, well...it turns out that changing the folder was the solutio, though i have projects working that share the same folder name so i don't really know what happend with this one.
Before:
Django
│ └── projectname
│ └── projectname
After:
Django
│ └── othername
│ └── projectname

Related

NotSupportedError at / deterministic=True requires SQLite 3.8.3 or higher

I am trying to deploy a Django application using the default SQLite database to Elastic Beanstalk. The application works fine locally, however, on server, I get the error as :
Any idea what's wrong? Can we not deploy SQLite app on AWS EBS?
Here is my project structure that is getting deployed
├── .ebextensions
│   ├── django.config
├── app
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── __init__.py
│   ├── models.py
│   ├── static
│   │   └── app
│   │   ├── app.js
│   │   ├── logo.svg
│   │   └── style.css
│   ├── templates
│   │   └── app
│   │   ├── hello.html
│   │   ├── index.html
│   │   └── job_detail.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── db.sqlite3
├── env
│   ├── All Virtual env related files
├── images
│   ├── Photo_on_5-9-14_at_2.31_PM.jpg
│   ├── Photo_on_5-9-14_at_2.32_PM.jpg
│   └── Photo_on_5-9-14_at_2.32_PM_v4McLzE.jpg
├── myproject-test
│   ├── __init__.py
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── manage.py
├── requirements.txt
├── staticfiles
│   |-- All static files collected using collectstatic
│   ├── app
│   │   ├── app.js
│   │   ├── logo.svg
│   │   └── style.css
│   └── subscribe
│   ├── email-icon.png
│   ├── main.css
│   └── main.js
├── subscribe
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── form.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── 0002_subscribe_option_alter_subscribe_email_and_more.py
│   │   ├── 0003_alter_subscribe_option.py
│   ├── models.py
│   ├── static
│   │   └── subscribe
│   │   ├── email-icon.png
│   │   ├── main.css
│   │   └── main.js
│   ├── templates
│   │   └── subscribe
│   │   ├── subscribe.html
│   │   └── thank_you.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── templates
│   └── base.html
├── upload
│   └── images
│   ├── Photo_on_5-9-14_at_2.31_PM.jpg
│   └── Photo_on_5-9-14_at_2.31_PM_3.jpg
└── uploads
├── __init__.py
├── admin.py
├── apps.py
├── forms.py
├── images
│   └── Photo_on_5-9-14_at_2.31_PM_3.jpg
├── migrations
│   ├── 0001_initial.py
│   ├── 0002_uploadfile.py
│   ├── 0003_alter_uploadfile_file.py
│   ├── __init__.py
├── models.py
├── templates
│   └── uploads
│   ├── add_file.html
│   └── add_image.html
├── tests.py
├── urls.py
└── views.py
Deployment process:
Zip all the necessary files
Create AWS EBS application with Python
Add environment variables
Create app and access URL
Please help.

Template not found after deploying to Heroku

After reading through this question, I have some insight as to why when trying to launch my app after deploying to Heroku, I get the following error: raise TemplateNotFound(template), jinja2.exceptions.TemplateNotFound: index.html.
They are saying that the templates should be at the project directory root as shown here. Is there anyway I can modify where Heroku is trying to look for the templates directory? I modified my project structure since my app was growing in size and starting to look a bit unorganized.
Here is a look at my current project structure:
Project
│
├── App
│   ├── DbMs
│   │   ├── db_actions.py
│   │   └── user_operations.py
│   ├── UI
│   │   ├── static
│   │   │   ├── css
│   │   │   │   └── main.css
│   │   │   ├── fonts
│   │   │   │   └── Gotham_Medium_Regular.json
│   │   │   ├── images
│   │   │   │   ├── favicons
│   │   │   │   │   ├── android-chrome-192x192.png
│   │   │   │   │   ├── android-chrome-512x512.png
│   │   │   │   │   ├── apple-touch-icon.png
│   │   │   │   │   ├── favicon-16x16.png
│   │   │   │   │   ├── favicon-32x32.png
│   │   │   │   │   ├── favicon.ico
│   │   │   │   │   └── site.webmanifest
│   │   │   │   ├── header.jpeg
│   │   │   │   ├── logo.png
│   │   │   │   └── radar-chart.png
│   │   │   └── javascript
│   │   │   ├── FontLoader.js
│   │   │   ├── TextGeometry.js
│   │   │   ├── create.js
│   │   │   ├── exploreMusic.js
│   │   │   └── tracks.js
│   │   └── templates
│   │   ├── base.html
│   │   ├── create.html
│   │   ├── index.html
│   │   ├── information.html
│   │   └── tracks.html
│   ├── __init__.py
│   ├── authenticate.py
│   ├── routes.py
│   ├── services.py
│   └── templates
├── Pipfile
├── Procfile
├── README.md
├── Testing
│   ├── __init__.py
│   └── testing.py
├── __pycache__
│   ├── authenticate.cpython-39.pyc
│   ├── config.cpython-39.pyc
│   ├── main.cpython-39.pyc
│   ├── services.cpython-39.pyc
│   └── user_operations.cpython-39.pyc
├── config.py
├── package-lock.json
├── package.json
├── requirements.txt
├── run.py
└── setup.py
For more context, I have everything running perfectly fine locally, I only see this error after deployment. Thanks to anyone who can provide insight.
EDIT: I tried moving the templates folder into the App directory as well as the project root and changing the following line in __init__.py both times:
template_dir = os.path.abspath('../Project/templates/')
After deploying each time, I changed the location of templates, still see the same error from Heroku: raise TemplateNotFound(template) jinja2.exceptions.TemplateNotFound: index.html
So it turns out that Heroku is picky with modifying the location of the templates & static folders.
Whats was very misleading about this issue was that I could run everything fine locally but as soon as I deployed to Heroku, the entire App would break..
The way I fixed this was by moving templates & static into the App directory. I also had to make sure that i changed the following line from:
template_dir = os.path.abspath('../Soulify/App/templates/')
static_dir = os.path.abspath('../Soulify/App/UI/static/')
app = Flask(__name__, template_folder=template_dir, static_folder=static_dir)
to:
app = Flask(__name__)
Also note that the template folder has to be named templates exactly or Heroku will fail. Again, pretty annoying.

How to run pip installed elasticsearch?

I'm trying to use Django + Haystack + Elasticsearch.
So I installed Elasticsearch 2.4 with pip becouse Django gave me errors about that it can't import Elasticsearch. Now it can and I can run ./manage.py rebuild_index in my Django project and it gives this output:
Indexing 23 journal entrys
GET /haystack/_mapping [status:404 request:0.006s]
but only if I somehove run elasticsearch. So I installed elsasticsearch2 from AUR packages as well and run that. But as I suspected when I try to get all documents by running: curl -X GET "localhost:9200/_cat/indices?v" which returns:
health status index pri rep docs.count docs.deleted store.size pri.store.size
yellow open haystack 5 1 0 0 795b 795b
If I understand correctly it is empty.
I went where pip installs packages(/usr/lib/python3.6/site-packages) and found two folders related to Elasticsearch:
elasticsearch
├── client
│   ├── cat.py
│   ├── cluster.py
│   ├── indices.py
│   ├── ingest.py
│   ├── __init__.py
│   ├── nodes.py
│   ├── __pycache__
│   │   ├── cat.cpython-36.pyc
│   │   ├── cluster.cpython-36.pyc
│   │   ├── indices.cpython-36.pyc
│   │   ├── ingest.cpython-36.pyc
│   │   ├── __init__.cpython-36.pyc
│   │   ├── nodes.cpython-36.pyc
│   │   ├── snapshot.cpython-36.pyc
│   │   ├── tasks.cpython-36.pyc
│   │   └── utils.cpython-36.pyc
│   ├── snapshot.py
│   ├── tasks.py
│   └── utils.py
├── compat.py
├── connection
│   ├── base.py
│   ├── http_requests.py
│   ├── http_urllib3.py
│   ├── __init__.py
│   ├── pooling.py
│   └── __pycache__
│   ├── base.cpython-36.pyc
│   ├── http_requests.cpython-36.pyc
│   ├── http_urllib3.cpython-36.pyc
│   ├── __init__.cpython-36.pyc
│   └── pooling.cpython-36.pyc
├── connection_pool.py
├── exceptions.py
├── helpers
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-36.pyc
│   │   └── test.cpython-36.pyc
│   └── test.py
├── __init__.py
├── __pycache__
│   ├── compat.cpython-36.pyc
│   ├── connection_pool.cpython-36.pyc
│   ├── exceptions.cpython-36.pyc
│   ├── __init__.cpython-36.pyc
│   ├── serializer.cpython-36.pyc
│   └── transport.cpython-36.pyc
├── serializer.py
└── transport.py
elasticsearch-2.4.1.dist-info
├── DESCRIPTION.rst
├── INSTALLER
├── METADATA
├── metadata.json
├── pbr.json
├── RECORD
├── top_level.txt
└── WHEEL
I don't see a start_elasticsearch.sh or bin/elasticsearch so how can I start it?

Issue with turning a test file into a package with __init__.py

I am following along with this tutorial:
http://www.marinamele.com/taskbuster-django-tutorial/create-home-page-with-tdd-staticfiles-templates-settings
When I run:
(tb_test)metersk:functional_tests metersky$ python test_all_users.py
.
----------------------------------------------------------------------
Ran 1 test in 2.313s
OK
The test works as it should. The test being :
from selenium import webdriver
import unittest
class NewVisitorTest(unittest.TestCase):
def setUp(self):
self.browser = webdriver.Firefox()
self.browser.implicitly_wait(3)
def tearDown(self):
self.browser.quit()
def test_it_worked(self):
self.browser.get('http://localhost:8000')
self.assertIn('Welcome to Django', self.browser.title)
if __name__ == '__main__':
unittest.main(warnings='ignore')
However, when I follow the step to add a blank __init__.py file to the directory, and I run the test like so: python manage.py test functional_tests
I get a the following failure:
Traceback (most recent call last):
File "/Users/metersky/code/taskbuster_project/taskbuster/settings/base.py", line 20, in get_env_variable
return os.environ[var_name]
File "/Users/metersky/.virtualenvs/tb_test/bin/../lib/python3.4/os.py", line 633, in __getitem__
raise KeyError(key) from None
KeyError: 'SECRET_KEY'
File "/Users/metersky/code/taskbuster_project/taskbuster/settings/__init__.py", line 2, in <module>
from .base import *
File "/Users/metersky/code/taskbuster_project/taskbuster/settings/base.py", line 31, in <module>
SECRET_KEY = get_env_variable('SECRET_KEY')
File "/Users/metersky/code/taskbuster_project/taskbuster/settings/base.py", line 23, in get_env_variable
raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: set the SECRET_KEY environment variable
I don't quite understand how calling the test script differently from the command line would prevent the program from finding the secret key environment variable. It clearly works when I call the script on its own.
Folder structure:
├── TaskBuster.sublime-project
├── TaskBuster.sublime-workspace
├── db.sqlite3
├── functional_tests
│   ├── __init__.py
│   └── test_all_users.py
├── manage.py
├── requirements
│   ├── base.txt
│   ├── development.txt
│   ├── production.txt
│   └── testing.txt
└── taskbuster
├── __init__.py
├── __pycache__
│   ├── __init__.cpython-34.pyc
│   ├── settings.cpython-34.pyc
│   ├── urls.cpython-34.pyc
│   └── wsgi.cpython-34.pyc
├── db.sqlite3
├── settings
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-34.pyc
│   │   ├── base.cpython-34.pyc
│   │   └── development.cpython-34.pyc
│   ├── base.py
│   ├── development.py
│   ├── production.py
│   ├── staging.py
│   └── testing.py
├── static
│   ├── css
│   │   ├── bootstrap-theme.css
│   │   ├── bootstrap-theme.css.map
│   │   ├── bootstrap-theme.min.css
│   │   ├── bootstrap.css
│   │   ├── bootstrap.css.map
│   │   ├── bootstrap.min.css
│   │   └── main.css
│   ├── favicon.ico
│   ├── fonts
│   │   ├── glyphicons-halflings-regular.eot
│   │   ├── glyphicons-halflings-regular.svg
│   │   ├── glyphicons-halflings-regular.ttf
│   │   └── glyphicons-halflings-regular.woff
│   ├── img
│   └── js
│   ├── main.js
│   └── vendor
│   ├── bootstrap.js
│   ├── bootstrap.min.js
│   ├── jquery-1.11.2.min.js
│   ├── modernizr-2.8.3-respond-1.4.2.min.js
│   └── npm.js
├── templates
│   ├── 404.html
│   ├── base.html
│   ├── humans.txt
│   └── robots.txt
├── urls.py
└── wsgi.py

Django: Application labels aren't unique

I have been working on the issue of duplicate labels in Django and from this answer I have added the following files to my "jobs" project folder:
jobs/apps.py
# jobs/apps.py
from django.apps import AppConfig
class JobsConfig(AppConfig):
name = 'jobs'
verbose_name = "jobs2"
jobs/init.py
# jobs/__init__.py
default_app_config = 'jobs.apps.JobsConfig'
This hasn't really helped much and I still get the error when trying syncdb:
"duplicates: %s" % app_config.label)
django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: jobs
Also, changing from "name = 'jobs'" to "name = 'jobs2'" just gives me the error:
ImportError: No module named jobs2
File Structure
/opt/Webapp
├── userfiles
├── templates
│   └── admin
│   └── base.html
├── static
│   ├── admin_tools
│   │   ├── images
│   │   │   └── apto.gif
│   │   └── css
│   │   └── theming.css
│   └── admin
│   └── css
│   └── base.css
├── smartrecruitment
│   ├── wsgi.py
│   ├── urls.py
│   ├── settings.pyc
│   ├── settings.py
│   ├── __init__.pyc
│   └── __init__.py
├── requirements.txt
├── manage.py
├── jobs
│   ├── views.py
│   ├── urls.py
│   ├── tests.py
│   ├── testhelpers.py
│   ├── templates
│   │   └── jobs
│   │   ├── test.html
│   │   ├── success.html
│   │   ├── registration.html
│   │   ├── registrationcomplete.html
│   │   └── application.html
│   ├── tables.py
│   ├── static
│   │   └── jobs
│   │   ├── styles
│   │   │   ├── index.css
│   │   │   ├── hide_admin_original.css
│   │   │   └── application.css
│   │   ├── style.css
│   │   └── images
│   │   └── apto.gif
│   ├── models.py
│   ├── migrations
│   │   ├── __init__.py
│   │   ├── 0002_auto__del_field_registrant_name__add_field_registrant_first_name__add_.py
│   │   └── 0001_initial.py
│   ├── lists.py
│   ├── __init__.pyc
│   ├── __init__.py
│   ├── forms.py
│   ├── apps.pyc
│   ├── apps.py
│   └── admin.py
├── fileuploads
│   ├── tests.py
│   ├── templates
│   │   └── fileuploads
│   │   ├── index.html
│   │   ├── details.html
│   │   ├── base.html
│   │   └── add.html
│   ├── models.pyc
│   ├── models.py
│   ├── __init__.pyc
│   ├── __init__.py
│   ├── forms.pyc
│   ├── forms.py
│   ├── context_processors.py
│   └── admin.pyc
├── dashboard.pyc
└── dashboard.py
You have a mix of old-style (south: 0002_auto_del...) and new-style (django: 0001_initial) migrations in your jobs app. Easiest fix would be to delete all numbered migrations rm jobs/migrations/0???_*.py* and recreate migrations by running manage.py makemigrations

Categories