run django manage.py command in cron - django

I've written few management commands to run from cron. I'm using pipenv virtual environment
running from terminal directly is working great.
cd <project_path> pipenv run python manage.py <my_command>
I added same script as cron
cd /home/project_path && pipenv run python manage.py <my_command>
But this is giving error as
/bin/bash: pipenv: command not found
I also tried following command
cd /home/project_path && python manage.py <my_command>
which is giving error as
File "manage.py", line 14
) from exc
^
SyntaxError: invalid syntax

Put file run.py in root folder with settings.py file (NOTE! Your project structure may be different):
#!/usr/bin/env python
import os
import sys
import settings
p = os.path.abspath(os.path.join(os.path.dirname(__file__)))
sys.path.insert(0, '%s' % p)
sys.path.insert(0, '%s/apps' % p)
from django.core.wsgi import get_wsgi_application
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from django.conf import settings
application = get_wsgi_application()
module_name = sys.argv[1]
exec('import %s' % module_name)
exec('%s.%s' % (module_name, ' '.join(sys.argv[2:])))
Then go to your app folder and make file cron.py with test() function
def test():
print ('Hello world')
And final input next command to console:
python run.py your_app_name.cron "test()"

what solved my issue is setting absolute path to every module like
cd <project_path> && /root/.local/bin/pipenv run /home/user/.local/share/virtualenvs/myproject-IuTkL8w_/bin/python manage.py <my_command>

Related

django runserver command is not starting the server

I'm running Django and I can't figure out why it's coming out like that.
It turns out that Python and Django are installed.
input : python manage.py runserver
output : Python
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'cmapsite.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)
if __name__ == '__main__':
main()
Did you activate your Virtual Environment in CMD terminal ?
If No then First Activate your Virtual Environment, It may be a reason of not starting Server.
If you already created the virtual environment then which is the error that you keep getting ? Upload the Error.

django: I accidentally deleted the manage.py file. how to recover it?

I accidentally deleted the manage.py file with vim nerdtree. apparently, there isn't a way to recover it I I don't know what to do.
can I somehow create a new one or restore it?
You can easily copy this file by changing the "start.settings" with your "application.settings". The other codes is always the same :)
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'starter.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)
if __name__ == '__main__':
main()
You can create a new Django project by using the following command:
django-admin startproject someappname
and copy the file into your project and ensure to change the app name on the manage.py file

Django Gunicorn no module named error

My project directory structure looks like following:
My wsgi file:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
application = get_wsgi_application()
and my manage.py file:
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
try:
from django.core.management import execute_from_command_line
except ImportError:
# The above import may fail for some other reason. Ensure that the
# issue is really that Django is missing to avoid masking other
# exceptions on Python 2.
try:
import django
except ImportError:
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?"
)
raise
execute_from_command_line(sys.argv)
in my settings .py I have:
WSGI_APPLICATION = 'config.wsgi.application'
I added my project path to python with following:
export PYTHONPATH=/home/ec2-user/amm_inovision_backend:$PYTHONPATH
and I am trying to run gunicorn command where my manage.py file is as: gunicorn amm_inovision_backend.config.wsgi:application
But it throws me error no module named amm_inovision_backend.config.wsgi
If I run instead gunicorn config.wsgi:application it throws no module named amm_inovision_backend.config.settings
What am I doing wrong?
Note: in the screenshot it is amm_ino_backend but actually it is amm_inovision_backend in the production
when you want to use gunicorn you must run gunicorn installed on your virtual environment.
so you don't need to export any python path, you only need to find path to gunicorn on your virtual-env where Django is installed, or you can add path to virtual environment not to project files.
also you need first to edit your wsgi.py
you have to replace this
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings") with
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
your command must be something like that:
/path/to/.virtual-envs/your-env/bin/gunicorn config.wsgi:application
Use:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
application = get_wsgi_application()
note the "CONFIG.SETTINGS"

IOError: [Errno 2] No such file or directory: '/var/www/.virtualenvs/exampleenv/bin/activate_this.py")

I have use virtualenv for deploying a django app on a vps. The following is the content of wsgi file placed in /var/www/example.wsgi
import os
import sys
import site
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('~/.virtualenvs/exampleenv/local/lib/python2.7/site-packages')
# Add the app's directory to the PYTHONPATH
sys.path.append('/var/www/example')
sys.path.append('/var/www/example/example')
os.environ['DJANGO_SETTINGS_MODULE'] = 'example.settings'
# Activate your virtual env
activate_env=os.path.expanduser("~/virtualenvs/exampleenv/bin/activate_this.py")
execfile(activate_env, dict(__file__=activate_env))
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
But the error.log file shows the following error.
IOError: [Errno 2] No such file or directory: '/var/www/.virtualenvs/exampleenv/bin/activate_this.py")
It is looking for virtualenv inside /var/www instead of ~/.virtualenv/.....
I have already checked the path of the exampleenv which exists in ~/.virtualenvs/exampleenv
The app is probably running as different user (which home dir is /var/www/), so expanduser function will use its home dir not yours.
It is not a good practice to put others users files in your home dir. Try to put virtualenv in lets say /var/www/<your_app>/.venv or /var/www/.<your_app>_venv.

Django equivalent to paster for backend processes

I use pylons in my job, but I'm new to django. I'm making an rss filtering application, and so I'd like to have two backend processes that run on a schedule: one to crawl rss feeds for each user, and another to determine relevance of individual posts relative to users' past preferences. In pylons, I'd just write paster commands to update the db with that data. Is there an equivalent in django? EG is there a way to run the equivalent of python manage.py shell in a non-interactive mode?
I think that's what Custom Management Commands are there for.
Yes, this is actually how I run my cron backup scripts. You just need to load your virtualenv if you're using virtual environments and your project settings.
I hope you can follow this, but after the line # manage.py shell you can write your code just as if you were in manage.py shell
You can import your virtualenv like so:
import site
site.addsitedir(VIRTUALENV_PATH + '/lib/python2.6/site-packages')
You can then add the django project to the path
import sys
sys.path.append(DJANGO_ROOT)
sys.path.append(PROJECT_PATH)
Next you load the django settings and chdir to the django project
import os
from django.core.management import setup_environ
from myproject import settings
setup_environ(settings)
os.chdir(PROJECT_PATH)
After this point your environment will be set just like if you started with manage.py shell
You can then run anything just as if you were in the interactive shell.
from application.models import MyModel
for element in MyModel:
element.delete()
Here is my backup file in full. I've abstracted the process out into functions. This would be named daily_backup and be put into the cron.daily folder to be run daily. You can see how to set up the environment and modify the functionality as needed.
#!/usr/bin/env python
import sys
import os
import site
import logging
from datetime import datetime
PROJECT_NAME = 'myproject'
DJANGO_ROOT = '/var/www/django'
PROJECT_PATH = DJANGO_ROOT + '/' + PROJECT_NAME
VIRTUALENV_PATH = '/var/www/envs/'+ PROJECT_NAME
BACKUP_DIR = '/var/www/backups/%s/daily' % (PROJECT_NAME)
TODAY = datetime.now().strftime('%Y%m%d-%H%M%S')
FILE_NAME = PROJECT_NAME + '_' + TODAY
site.addsitedir(VIRTUALENV_PATH + '/lib/python2.6/site-packages')
sys.path.append(DJANGO_ROOT)
sys.path.append(PROJECT_PATH)
from django.core.management import setup_environ
from myproject import settings
setup_environ(settings)
os.chdir(PROJECT_PATH)
# manage.py shell
from django.conf import settings
logging.basicConfig(level=logging.WARN)
def _setup():
if not os.path.exists(BACKUP_DIR):
logging.debug('Creating backup directory ' + BACKUP_DIR)
os.mkdir(BACKUP_DIR)
os.mkdir(BACKUP_DIR + '/databases')
else:
logging.debug('Using backup directory ' + BACKUP_DIR)
def _remove_old():
logging.debug('Cleaning out old backups')
# keep past 7 days
command = "find %s* -name '%s*' -mtime +7 -exec rm {} \\;" % (BACKUP_DIR, PROJECT_NAME)
os.system(command)
def _create_backup():
logging.debug('Backup database')
if settings.DATABASE_ENGINE == 'mysql':
command = 'mysqldump -u %s --password=%s %s > %s/databases/%s.sql' % (settings.DATABASE_USER, settings.DATABASE_PASSWORD, settings.DATABASE_NAME, BACKUP_DIR, FILE_NAME)
else:
command = '%s/bin/python %s/manage.py dumpdata --indent=4 > %s/databases/%s.json' % (VIRTUALENV_PATH, PROJECT_PATH, BACKUP_DIR, FILE_NAME)
os.system(command)
logging.debug('Backup project')
command = 'tar -czf %s/%s.tgz -C %s %s/' % (BACKUP_DIR, FILE_NAME, DJANGO_ROOT, PROJECT_NAME)
os.system(command)
if __name__ == '__main__':
_setup()
_remove_old()
_create_backup()
Sounds like you need some twod.wsgi in your life: http://packages.python.org/twod.wsgi/