├── requirements.txt ├── runtime.txt ├── Procfile ├── .gitignore ├── script.py ├── LICENCE └── README.md /requirements.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /runtime.txt: -------------------------------------------------------------------------------- 1 | python-3.10.8 2 | -------------------------------------------------------------------------------- /Procfile: -------------------------------------------------------------------------------- 1 | worker: python script.py 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Cache 2 | __pycache__/ 3 | -------------------------------------------------------------------------------- /script.py: -------------------------------------------------------------------------------- 1 | import time 2 | 3 | while True: 4 | print("I am working!") 5 | time.sleep(3) 6 | -------------------------------------------------------------------------------- /LICENCE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Michael Krukov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Template for hosting python scripts and applications on Heroku 2 | 3 | This is a small example of running your script with 4 | [Heroku](https://www.heroku.com/). You can run almost any python 5 | application with any dependencies. 6 | 7 | ## Getting Started 8 | 9 | 1. Download or clone this repository 10 | 2. Register on [Heroku](https://www.heroku.com/) 11 | 3. Download and install [Heroku CLI](https://devcenter.heroku.com/articles/getting-started-with-python#set-up) 12 | 4. Download and install [git](https://git-scm.com/downloads) 13 | 5. Copy your script or project to this repository's folder 14 | 6. Replace "script.py" with the path to your main executable file in 15 | `Procfile`. For details about `Procfile` refer to the [docs](https://devcenter.heroku.com/articles/procfile). 16 | If you need to run web application, you have to use `web` instead of `worker`. 17 | 7. You may select your python version and runtime using `runtime.txt`. Read 18 | how on [official heroku page](https://devcenter.heroku.com/articles/python-runtimes#selecting-a-runtime). 19 | 8. If you are using any not built-in modules, you must add them to your 20 | `requirements.txt`. To check which version of the module you have, run 21 | `pip freeze` in the terminal. You will get lines with information about 22 | installed modules and their versions in the format like 23 | `MODULE_NAME==MODULE_VERSION`. Add lines with required modules and their 24 | versions to your `requirements.txt`. Heroku will install modules from this 25 | file automatically. 26 | 27 | > If you are using some kind of virtual environment, you can generate 28 | > ready-to-use `requirements.txt` with `pip freeze > requirements.txt`. 29 | 9. Open terminal (or do it another way, but I will explain how to do it in 30 | the terminal on Ubuntu) and create a git repository. 31 | 1. Initiate git repository 32 | 33 | ```bash 34 | git init 35 | ``` 36 | 37 | 2. Create heroku application 38 | 39 | ```bash 40 | heroku create 41 | ``` 42 | 43 | 3. Add, commit and push your code into branch `master` of the 44 | remote `heroku`. 45 | 46 | ```bash 47 | git add . 48 | git commit -m "initial commit" 49 | git push heroku master 50 | ``` 51 | 52 | 10. Specify the amount of worker that will run your application 53 | 54 | ```bash 55 | heroku ps:scale worker=1 56 | ``` 57 | 58 | 11. Now everything should be working. You can check your logs with this command 59 | 60 | ```bash 61 | heroku logs --tail 62 | ``` 63 | 64 | 12. You can open the URL where the script is deployed using the below 65 | command (if you are deploying web application) 66 | 67 | ```bash 68 | heroku open 69 | ``` 70 | 71 | 13. From now on you can use usual git commands (push, add, commit, etc.) 72 | to update your app. Every time you `push heroku master` your 73 | app gets redeployed with updated source code 74 | 75 | 14. To stop your application scale down the amount of workers with like this 76 | 77 | ```bash 78 | heroku ps:scale worker=0 79 | ``` 80 | 81 | ### Prerequisites 82 | 83 | * [Heroku CLI](https://devcenter.heroku.com/articles/getting-started-with-python#set-up) 84 | * [git](https://git-scm.com/downloads) 85 | 86 | ## Authors 87 | 88 | * @michaelkrukov - https://michaelkrukov.ru/ 89 | 90 | ## Acknowledgments 91 | 92 | * [Official guide to deploy app](https://devcenter.heroku.com/articles/getting-started-with-python#introduction) 93 | * [Official guide about worker](https://devcenter.heroku.com/articles/background-jobs-queueing) 94 | * [Guided "Simple twitter-bot with Python, Tweepy and Heroku"](http://briancaffey.github.io/2016/04/05/twitter-bot-tutorial.html) 95 | --------------------------------------------------------------------------------