Django initialization stuck at import netrc - django

My django app starts very slowly. I ran python -v manage.py check and it stucks at line
import 'netrc' # <_frozen_importlib_external.SourceFileLoader object at 0x7efff40f3d30>
I found this related question : Django "Performing System Checks" is running very slow
Here it says he removed the elastic beanstalk urls from ALLOWED_HOSTS and it fixed. I did that too but my app still stucks at that import statement. What else can cause that?

Related

While importing 'myapp.app' an import error was raised

I'm building a flask application, located in a subdirectory within my project called myapp. Running gunicorn --bind 0.0.0.0:$PORT myapp.app:app works fine, no errors, regardless of what FLASK_APP is set to. When I try to use Flask's CLI, however, it fails to find my app (reasonable), but when I set FLASK_APP to myapp.app., it appears to be doubling up the import path, resulting in an import error:
FLASK_APP=myapp.app flask run
* Serving Flask app 'myapp.app' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
Usage: flask run [OPTIONS]
Try 'flask run --help' for help.
Error: While importing 'myapp.myapp.app', an ImportError was raised.
How can I solve this? Is this a bug in Flask?
There was a __init__.py in my project directory that was causing this.
I dug into the source code of flask in cli.py and found the following code in prepare_import:
# move up until outside package structure (no __init__.py)
while True:
path, name = os.path.split(path)
module_name.append(name)
if not os.path.exists(os.path.join(path, "__init__.py")):
break
Because I had an __init__.py in my project directory (also called myapp), this made flask try to import myapp.app from the ancestor of my project, resulting in myapp.myapp.app.

Error while deploying Django app on cpanel(shared hosting)

I m new to django. I created a web with dajngo,and successfully deployed it in the server
The python app has been successfully setup and virtual environment has been setup.
but while running the web it gives me "Server Error (500)" I don't know whats the problem.
I think error is in "wsgi.py" file but i'm unable to idenify it.
My wsgi file:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'karan_web.settings')
application = get_wsgi_application()
my "passenger_wsgi.py" file is:
import imp
import os
import sys
sys.path.insert(0, os.path.dirname(__file__))
wsgi = imp.load_source('wsgi', 'karan_web/wsgi.py')
application = wsgi.application
can someone help me with it;
Sorry for the late answer, I had figured out the as to this just forgot to post it.
As I stated in my question, the actual problem was in passenger_wsgi.py, the django server starts with the wsgi.py and act as the gate way to the Django server.
So whenever a Django project is uploaded on the hosting server, It creates a passenger_wsgi.py file and one default wsgi.py, address of which is provided in passenger_wsgi.py by default.
So we just need to change that address and provide the address of our own wsgi.py in the project
In my case it was
import os
import sys
from karan_web import wsgi
application = wsgi.application
just edit in passenger_wsgi.py the following code.
from karan_web.wsgi import application
You need to check if your code syntax is correct and running properly. If its still doesn't work try to delete and recreate your database in cpanel and check if you have made all necessary migrations, don't forget to restart your python app. If after all these it still doesn't work check if all your files have the correct file permission(766).

Run a python script just after run server django

I have a python script with indefinite loop it keeps in checking for the data, now I am confused how do I execute it so that it keeps running if the server is running.
I think the script should run just after I run server in django but how do I run this script?
Any suggestions?
django-admin runserver is only for development use. Assuming this is a development environment, probably simplest to just run your script in a separate console, or make a simple shell script that starts the django server, backgrounds that process, then runs your script.
#!/bin/sh
django-admin runserver &
/path/to/my/script.py &
You'll need to kill those processes manually before re-running that script.
In a production environment, use WSGI to run Django and something like supervisord to run the other script. You could configure the OS init system (probably systemd) to ensure both of these tasks start on reboot and remain running.
The first big part is to pack your indefinite loop into a function, and use huey/celery/etc to make it a 'task' that runs asynchronously.Then, call this task to run in your views.py/models.py/manage.py.
HOW TO LET THE TASK RUN ASYNCLY:
HUEY:[https://huey.readthedocs.io/en/latest/]
HOW TO RUN THE TASK WHEN YOU START THE SERVER (2 ways):
Edit your manage.py ,add your function call at the line right behind the import of manage.py.
#!/usr/bin/env python
import os
import sys
print('Interesting things happens.') # THIS IS WHERE YOU RUN YOUR tasks.checkdata.
if __name__ == '__main__':
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'OTAKU.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
However , maybe you want to leave the manage.py alone. Create a new app in your project, with views.py and models.py.(Remember to add this app to your INSTALLED_APPS in settings.py)
In views.py :
print("The view is loading.")# THIS IS WHERE YOU CAN RUN YOUR tasks.checkdata.
In models.py:
print("The models is loading.")# THIS IS WHERE YOU CAN RUN YOUR tasks.checkdata.
And when you run your server now, this is what you might see:
Interesting things happens.
The models is loading.
Interesting things happens.
The models is loading.
Performing system checks...
The view is preparing itself.
System check identified no issues (0 silenced).
July 15, 2019 - 11:52:03
Django version 2.1.3, using settings 'rayos.settings'
Starting development server at http://0.0.0.0:9988/
Quit the server with CONTROL-C.
Replace the print part of the script with your data checking functions and it will work.

django html5 cache in django 1.7

I've got some problems implementing 3rd party django package django-html5-appcache.
Documentation especify that migrate command must be executed, but when i execute command:
python manage.py migrate html5_appcache
Outputs:
"No migrations to apply"
However I decided to complete installation steps. but testing it maniffest file appears to be empty (according to docs, urls suppose to autodiscover):
CACHE MANIFEST
# version: $0$
# date: $-$
NETWORK:
*
And Chrome Console Outputs:
Creating Application Cache with manifest http://127.0.0.1:8000/manifest.appcache
127.0.0.1/:1 Application Cache Checking event
127.0.0.1/:1 Application Cache Downloading event
127.0.0.1/:1 Application Cache Progress event (0 of 0)
127.0.0.1/:1 Application Cache Cached event
Im using Django 1.7
Any body has expirience with this django package?
I suspect that you're supposed to put the following code in your urls.py to get autodiscover.
Enable appcache discovery by adding the lines below in urls.py:
import html5_appcache
html5_appcache.autodiscover()
(Source: documentation here: https://django-html5-appcache.readthedocs.org/en/latest/installation.html)
Also, python manage.py migrate commands are used to alter the DB structure and should not affect your urls.py.

ImportError when using Haystack 2.0.0 with Django 1.5 and Gunicorn WSGI

I use django-haystack 2.0.0 to index my site, and it has been working great until I upgraded to Django 1.5 and started using the WSGI interface. If I just use the django_gunicorn command it works great, but the Django documentation "highly recommends" I use the gunicorn command.
When I start my site with the gunicorn command, Haystack throws the following error on any page load:
ImportError: cannot import name signals
I have no problems importing signals from the Django or Python shells. I use virtualenv and install all packages locally inside that environment. My wsgi.py file looks just like the default one in the django admin, except that I add the local path to the python path as such:
path = os.sep.join(os.path.abspath(__file__).split(os.sep)[:-2])
if path not in sys.path:
sys.path.append(path)`
Any help you could provide would be very appreciated, thank you!
I don't use gunicorn, but I had the same problem when I used the HAYSTACK_SIGNAL_PROCESSOR setting to point to a custom class that I wrote. That class imported one of my models, which eventually propagated up the import chain, to import my settings module, thus causing a circular import.
When using a setting such as HAYSTACK_SIGNAL_PROCESSOR that points to a class, make sure that class standsalone, and doesn't import either directly or indirectly the Django settings file.