如何在Heroku上执行python脚本?

时间:2023-01-12 19:38:45

I have a python script which I only need to run once (I don't want to have to commit it to the repo to send to the Cedar instance).

我有一个python脚本,我只需要运行一次(我不想将它提交到repo发送到Cedar实例)。

The Script aggregates data over my Django models and outputs a .csv file.

脚本通过我的Django模型聚合数据并输出.csv文件。

Normally in AWS, I would scp the script to the server, manage.py shell < script.py, and scp the produced .csv back to my machine.

通常在AWS中,我会将脚本scp到服务器,manage.py shell ,并将生成的.csv>

I understand Heroku file systems are ephemeral, but is there a way to retrieve produced files on Heroku servers without uploading them to S3?

我知道Heroku文件系统是短暂的,但有没有办法在Heroku服务器上检索生成的文件而不将它们上传到S3?

Here's my best shot:

这是我最好的镜头:

cat script.py | heroku run manage.py shell --app appname

Works for a one line script, but not with line breaks.

适用于单行脚本,但不适用于换行符。

Also, the above script will only produce command line output, not return a .csv file.

此外,上面的脚本只会产生命令行输出,而不会返回.csv文件。

3 个解决方案

#1


2  

Heroku doesn't give you write permissions anywhere on your dyno (the only files it writes are the ones it checks out from your master branch when you push an update, and any dependencies). You'll have to upload your output to S3, save it to the log, or find some other delivery mechanism (email?)

Heroku不会在你的dyno上的任何地方给你写权限(它所写的唯一文件是你在推送更新时从你的主分支中检出的文件,以及任何依赖项)。您必须将输出上传到S3,将其保存到日志中,或者找到其他一些传送机制(电子邮件?)

#2


3  

I wanted to execute a python script manually in heroku one single time. That's how it worked:

我想在heroku中一次手动执行一个python脚本。这就是它的工作原理:

  1. import django in your script
  2. 在脚本中导入django

  3. add the line django.setup() in your python script (that is important to be able to use the created models in django)
  4. 在你的python脚本中添加行django.setup()(这对于能够在django中使用创建的模型很重要)

  5. shell command (has to be executed in your git repository): heroku run bash
  6. shell命令(必须在你的git仓库中执行):heroku run bash

  7. bash command: export DJANGO_SETTINGS_MODULE=projectsname.settings
  8. bash命令:export DJANGO_SETTINGS_MODULE = projectsname.settings

  9. bash command: cd projectsname
  10. bash命令:cd projectsname

  11. bash commmand: python yourscriptname.py
  12. bash commmand:python yourscriptname.py

#3


1  

You are nearly there. The issue you are having is more related to how the typical shell syntax works.

你快到了。您遇到的问题与典型shell语法的工作方式有关。

You have cat script.py | heroku run manage.py shell --app appname which pipes in the script with no line breaks.

你有cat script.py | heroku运行manage.py shell --app appname,它在脚本中没有换行符。

To get your line breaks try this.

为了获得你的换行,试试这个。

herkoku run python -c "`cat script.py`" --app appname

The key is wrapping the backticks in double quotes, that way line breaks in the script are preserved.

关键是用双引号包装反引号,这样就可以保留脚本中的换行符。

#1


2  

Heroku doesn't give you write permissions anywhere on your dyno (the only files it writes are the ones it checks out from your master branch when you push an update, and any dependencies). You'll have to upload your output to S3, save it to the log, or find some other delivery mechanism (email?)

Heroku不会在你的dyno上的任何地方给你写权限(它所写的唯一文件是你在推送更新时从你的主分支中检出的文件,以及任何依赖项)。您必须将输出上传到S3,将其保存到日志中,或者找到其他一些传送机制(电子邮件?)

#2


3  

I wanted to execute a python script manually in heroku one single time. That's how it worked:

我想在heroku中一次手动执行一个python脚本。这就是它的工作原理:

  1. import django in your script
  2. 在脚本中导入django

  3. add the line django.setup() in your python script (that is important to be able to use the created models in django)
  4. 在你的python脚本中添加行django.setup()(这对于能够在django中使用创建的模型很重要)

  5. shell command (has to be executed in your git repository): heroku run bash
  6. shell命令(必须在你的git仓库中执行):heroku run bash

  7. bash command: export DJANGO_SETTINGS_MODULE=projectsname.settings
  8. bash命令:export DJANGO_SETTINGS_MODULE = projectsname.settings

  9. bash command: cd projectsname
  10. bash命令:cd projectsname

  11. bash commmand: python yourscriptname.py
  12. bash commmand:python yourscriptname.py

#3


1  

You are nearly there. The issue you are having is more related to how the typical shell syntax works.

你快到了。您遇到的问题与典型shell语法的工作方式有关。

You have cat script.py | heroku run manage.py shell --app appname which pipes in the script with no line breaks.

你有cat script.py | heroku运行manage.py shell --app appname,它在脚本中没有换行符。

To get your line breaks try this.

为了获得你的换行,试试这个。

herkoku run python -c "`cat script.py`" --app appname

The key is wrapping the backticks in double quotes, that way line breaks in the script are preserved.

关键是用双引号包装反引号,这样就可以保留脚本中的换行符。