I have a Django project with 2 apps and was writing some functions in a utils.py file in one of the apps. I wanted to break this up into two separate files in their own subdirectory so I created a new directory 'utils' a level below the app directory and placed the two utils1.py and utils2.py files in there.
I had some issues with importing something from the other app so I ended up scrapping this idea and moving everything back into one file in the base directory of the original app, exactly like it was before. Now when I runserver it is not picking up any new files that are created within apps. Not just the ones that I recreated but any new files. Files that were created prior to the change are running just fine.
So, in summary new utils.py files that I recreated in the app directory are not running when the dev server is started, and when I try to run one of them manually they run like any other python file, but imports from other locations in the project are not being recognized.
No other changes were made and new files were running perfectly fine before the directory changes.
After the changes:
├── app1
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ ├── models.py
│ ├── permissions.py
│ ├── serializers.py
│ ├── tests.py
│ ├── urls.py
│ ├── utils.py
│ └── views.py
├── manage.py
├── project
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── app2
├── __init__.py
├── admin.py
├── apps.py
├── utilities <--- added
├── util1.py
└── util2.py
├── migrations
├── models.py
├── serializers.py
├── tests.py
├── urls.py
└── views.py
After reverting back to previous structure (not working):
├── app1 <--- new files created here aren't running
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ ├── models.py
│ ├── permissions.py
│ ├── serializers.py
│ ├── tests.py
│ ├── urls.py
│ ├── utils.py
│ └── views.py
├── manage.py
├── project
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── app2 <--- new files created here aren't running
├── __init__.py
├── admin.py
├── apps.py
├── util1.py <--- moved back into app directory
├── migrations
├── models.py
├── util2.py <--- moved back into app directory
├── serializers.py
├── tests.py
├── urls.py
└── views.py
I've tried clearing the pycache files, restarting the dev server, restarting terminal, etc. to no avail.
I figured out what was going on. My assumption was that any new python files in an installed app would be automatically run, but something from the file needs to be imported first from somewhere else in the project. Before the changes I had an import in the utils.py file so the dev server was running it, but after the changes there were no imports from elsewhere in the project. Issue is fixed and working now.
Related
I have a Django 2.0 project using celery 4.2.1 and redis 2.10.6. The django project has two apps, memorabilia and face_recognition. I have it all successfully running tasks with django running on my development machine. I uploaded everything to my git server, then installed the apps on my laptop from git, updated all requirements, etc. Both are Ubuntu machines. I am not using django-celery.
When I try to run celery -A MemorabiliaJSON worker -l debug,
I get an exception saying ModuleNotFoundError: No module named 'face_recognition.tasks'
I am not sure how to fix this, as the same code base is running on my development machine.
My file structure is:
├── celery.sh
├── face_recognition
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ ├── models.py
│ ├── __pycache__
│ ├── tasks.py
│ ├── tests.py
│ └── views.py
├── __init__.py
├── manage.py
├── memorabilia
│ ├── admin.py
│ ├── apps.py
│ ├── fields.py
│ ├── fixtures
│ ├── __init__.py
│ ├── logs
│ ├── migrations
│ ├── models.py
│ ├── __pycache__
│ ├── storage.py
│ ├── tasks.py
│ ├── templates
│ ├── tests
│ ├── urls.py
│ ├── validators.py
│ ├── views.py
│ ├── widgets.py
├── MemorabiliaJSON
│ ├── celery.py
│ ├── default_images
│ ├── documents
│ ├── __init__.py
│ ├── __pycache__
│ ├── settings
│ ├── static
│ ├── urls.py
│ ├── views.py
│ ├── wsgi.py
├── __pycache__
│ ├── celery.cpython-36.pyc
│ └── __init__.cpython-36.pyc
├── requirements.txt
└── tests
MemorabiliaJSON/celery.py
# http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from django.apps import apps
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'MemorabiliaJSON.settings.tsunami')
app = Celery('MemorabiliaJSON')
app.config_from_object('django.conf:settings', namespace='CELERY')
#app.autodiscover_tasks(lambda: [n.name for n in apps.get_app_configs()])
app.autodiscover_tasks()
#app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
(memorabilia-JSON) mark#octopus:~/python-projects/memorabilia-JSON
face_recognition/__init__.py
default_app_config = 'face_recognition.apps.FaceRecognitionConfig'
memorabilia/__init__.py
default_app_config = 'memorabilia.apps.MemorabiliaConfig'
INSTALLED_APPS has these two apps
'memorabilia.apps.MemorabiliaConfig',
'face_recognition.apps.FaceRecognitionConfig',
This is my project structure:
├── run.py
└── test_blueprint
├── __init__.py
├── __init__.pyc
├── mainsite
│ ├── controllers
│ │ ├── controllers.py
│ │ ├── controllers.pyc
│ │ ├── __init__.py
│ │ └── __init__.pyc
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── static
│ │ ├── css
│ │ │ ├── bootstrap.min.css
│ │ │ └── signin.css
│ │ └── js
│ └── templates
│ └── mainsite
│ └── homepage.html
├── static
│ ├── css
│ │ ├── bootstrap.min.css
│ │ └── signin.css
│ └── js
├── templates
│ └── mainsite
│ └── index1.html
└── usermanagement
├── controllers
│ ├── controllers.py
│ ├── controllers.pyc
│ ├── __init__.py
│ └── __init__.pyc
├── __init__.py
├── __init__.pyc
├── static
└── templates
Here I have two blueprints, mainsite and usermanagement. I have registered them in file __init__.py under the test_blueprint folder which is the main folder (one below the blueprint_project folder which has run.py).
__init__ file under test_blueprint
from flask import Flask
app = Flask(__name__)
from test_blueprint.mainsite.controllers.controllers import mod
from test_blueprint.usermanagement.controllers.controllers import mod
app.register_blueprint(mainsite.controllers.controllers.mod, url_prefix="/")
app.register_blueprint(usermanagement.controllers.controllers.mod, url_prefix="/usermanagement")
Now post that I have under each blueprint folder I have created __init__.py in which I have defined Blueprints along with template folder. However, it does not take the template from there. It keeps on throwing error 'template not found'. So, I have created template folder under test_blueprint folder. It perfectly picks index.html from templates/mainsite folder.
Also, if I provide /home/user/flaskenv/blueprint_project/... templates folder which is an absolute path for my template under blueprint folder, it works fine.
Not sure if the bug is in Flask. I saw a video on YouTube and expected my flask project to behave the same. Unfortunately it didn't.
This is what my controller.py/views.py file looks like for mainsite:
from flask import Blueprint, render_template, url_for
mod = Blueprint('mainsite', __name__, static_folder='static',template_folder='templates')
#mod.route("/")
def home():
return render_template('mainsite/homepage.html')
Now if I used os.path.abspath to locate the template folder under the blueprint folder, and then passed that path as variable to template_folder, it works fine.
Now my expectation as per flask documentation is that just mentioning template_folder='templates' should automatically locate templates under the blueprint folder or it entirely defeats the purpose.
I solved by renaming the html file by unique name
Folders structure:
.
├── db.sqlite3
├── homepage
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── manage.py
├── photoarchive
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── somesite
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-34.pyc
│ │ ├── settings.cpython-34.pyc
│ │ ├── urls.cpython-34.pyc
│ │ └── wsgi.cpython-34.pyc
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── tests
├── functional_test.py
├── __init__.py
├── __pycache__
│ ├── functional_test.cpython-34.pyc
│ ├── __init__.cpython-34.pyc
│ └── validators.cpython-34.pyc
└── validators.py
functional_test.py
from selenium import webdriver
from django.test import TestCase
import pdb
class HomePageTest(TestCase):
def setUp(self):
self.browser = webdriver.Firefox()
self.browser.implicitly_wait(3)
pdb.set_trace()
def tearDown(self):
self.browser.quit()
def test_home_page(self):
#Edith goes to home page.
self.browser.get("http://localhost:8000")
#Edith sees "Hello, world" in the browser title.
estimated_browser_title ="Hello, world"
real_browswer_title = self.browser.title
self.assertIn(estimated_browser_title, real_browswer_title)
I run the test:
(venv) michael#michael:~/workspace/mysite/somesite$ python manage.py test tests
Creating test database for alias 'default'...
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
Destroying test database for alias 'default'...
Could you help me understand why my tests are not executed. I set a pdb breakpoint. The interpreter doesn't stop at that breakpoint. Well, tests are ignored.
Could you give me a kick here?
Django's test runner will only discover tests inside apps that are included in INSTALLED_APPS. Rather than putting your code in a functional_test.py file inside a tests directory, you should put it in a file called tests.py inside one of the app directories.
I have tested ShimmerCat (https://www.shimmercat.com/) and sc-tool (https://pypi.python.org/pypi/sc-tool/) using this (https://www.shimmercat.com/en/info/articles/getting-started/). I am trying to deploy the application locally but a 404 error occurs.
This is the structure of my project:
├── db.sqlite3
├── devlove.yaml
├── manage.py
├── testapp
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ ├── models.py
│ ├── templates
│ │ └── home.html
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── testproject
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ ├── wsgi.py
└── static
│ ├── base.css
And this is my devlove.yaml file:
shimmercat-devlove:
domains:
www.test.com:
root-dir: testproject
consultant: 8080
cache-key: xxxxxxx
I haven't made any changes to settings.py
Do you have any suggestions on how to solve the 404 error? Thank you in advance.
With web-servers, you usually have your static files served by the webserver itself and the dynamic part of the application being proxied by the web server to your application.
So, a starting setup for your project would be:
shimmercat-devlove:
domains:
static.test.com:
root-dir: static
cache-key: xxxxxxx
www.test.com:
port: 8080
Everything indented under "static.test.com" would be served as static resources, therefore you need to modify your settings.py file to identify "static.test.com" as your static domain:
STATIC_URL="https://static.test.com/"
In addition to that, you would need to run your Django application in one of the usual ways, simultaneously with the server. For example, you could start by launching:
$ python manage.py runserver
and then in a new terminal invoke ShimmerCat's from the project's directory:
$ shimmercat devlove
I'm a 2 Scoops of Django 1.8 reader. Chapter 29 (what about those random utilities) suggests to create a core app to store commonly used code. It also suggests that you can use this syntax to import code from it:
e.g.
from core.models import TimeStampedModel
How ever it seems that this relative import does not work. I'm using cookiecutter-django and I needed to do:
from projectname.apps.core.models import TimeStampedModel
I tried adding my APPS_DIR to the path:
sys.path.insert(str(APPS_DIR))
But that resulted in import conflicts given that now there were 2 modules with the same name, new_app and projectname.apps.new_app.
I just want to avoid explicit imports. Is there a way to include the Installed Apps in the python path without creating import conflicts? what are best practices regarding external apps imports?
edit: adds project structure
.
├── README.rst
├── manage.py
├── config
│ ├── __init__.py
│ ├── settings
│ │ ├── __init__.py
│ │ ├── common.py
│ │ ├── local.py
│ │ ├── test.py
│ │ ├── production.py
│ │ └── staging.py
│ ├── urls.py
│ ├── views.py
│ └── wsgi.py
├── projectname
│ ├── __init__.py
│ ├── apps
│ │ ├── __init__.py
│ │ ├── core
│ │ │ └── __init__.py
│ │ └── new_app
│ │ └── __init__.py
│ ├── static
│ │ └── ...
│ └── templates
│ └── ...
├── requirements
│ ├── base.txt
│ ├── local.txt
│ ├── production.txt
│ └── test.txt
└── tests
└── ...
If you want to use relative imports you need to import it this way
from .core.models import TimeStampedModel
This would take the relative path from which the code is being executed, unlike absolute imports which are not supported in Django 1.8