How to run a one-time python script on Heroku

I have a Django application and it works on Heroku. I want to run a simple script called import.py that imports a CSV file into my models. It works fine on my local computer. When I try to run a script on Heroku using this command:

heroku run python manage.py < import.py

Everything he does is reading the script back to me, but not executing any content. What am I doing wrong?

Edit:

This is the beginning of the result that I get at startup: 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)
+4
source share
3 answers

, . app/management/commands . , . script - , import.py (bad name...). , , __init__.py "management" "commands", Python. :

app
β”œβ”€β”€ admin.py
β”œβ”€β”€ __init__.py
β”œβ”€β”€ management
β”‚   β”œβ”€β”€ commands
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   └── import.py
β”‚   └── __init__.py
β”œβ”€β”€ models.py
... other files

- : python manage.py import ( ...)

heroku run python manage.py import .

P.S. , "import"

+2

import.py, heroku run python manage.py import.py.

+1

django-extensions . python Django manage.py:

python manage.py runscript my_script

Heroku:

  • django-extensions , requirements.txt
    • pip install django-extensions
    • pip freeze > requirements.txt
  • Create a directory scriptsas described in the documentation
  • Expand in Heroku
  • Launch it on Heroku with heroku run
    • heroku run python manage.py runscript my_script
    • Note. Do not add a .pyname to your script in the above command.

And you leave!

+1
source

All Articles