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)
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'
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:
django-extensions
and make sure it's in your requirements.txt
pip install django-extensions
pip freeze > requirements.txt
scripts
directory as described in the documentation
heroku run
heroku run python manage.py runscript my_script
.py
to your script name in the above commandAnd off you go!
Write a custom management command for import.py and then run heroku run python manage.py import.py
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With