Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run one-off python script on Heroku

I have a Django app up and running on Heroku. I want to run a simple script called import.py, which imports a CSV file into my models. It works great on my local computer. When I try to run the script on Heroku using this commmand:

heroku run python manage.py < import.py

All it does is read the script back to me, but not execute any of the content. What am I doing wrong?

Edit:

This is the start of the result I get when I run: heroku run python manage.py < import.py

Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import csv
>>> from bah_api.models import withDependents, withOutDependents, ZipMHA
>>> 
>>> # Populate CSV file into model
>>> def LoadCSV(file_location, my_model, delim):
... f = open(file_location)
  File "<console>", line 2
    f = open(file_location)
    ^
IndentationError: expected an indented block
>>> csv_f = csv.reader(f, delimiter=delim)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'f' is not defined
>>> for row in csv_f:
... 
Display all 182 possibilities? (y or n)
like image 787
Casey Avatar asked Apr 19 '15 21:04

Casey


3 Answers

I think you should create command for this. Commands should be placed in app/management/commands directory in your project. If this directory doesn't exist, create it. The name of the script is the name of your command, so you should name it import.py (bad name...). Another thing that has to be done is creating __init__.py files in both the 'management' and 'commands' directories, because these have to be Python packages. Tree should be like this :

app
├── admin.py
├── __init__.py
├── management
│   ├── commands
│   │   ├── __init__.py
│   │   └── import.py
│   └── __init__.py
├── models.py
... other files

Now you should be able to make something like this: python manage.py import (really bad name...)

Orheroku run python manage.py import will work with heroku.

P.S. I don't know if it works with name 'import'

like image 86
pythad Avatar answered Oct 17 '22 01:10

pythad


You can use the django-extensions runscript command. This command allows you to run python scripts in the Django context via manage.py:

python manage.py runscript my_script

To do this with Heroku:

  1. Install django-extensions and make sure it's in your requirements.txt
    • pip install django-extensions
    • pip freeze > requirements.txt
  2. Create a scripts directory as described in the documentation
  3. Deploy to Heroku
  4. Run it on Heroku with heroku run
    • heroku run python manage.py runscript my_script
    • Note: don't append a .py to your script name in the above command

And off you go!

like image 45
Peter Huitsing Avatar answered Oct 17 '22 02:10

Peter Huitsing


Write a custom management command for import.py and then run heroku run python manage.py import.py.

like image 34
mcastle Avatar answered Oct 17 '22 02:10

mcastle