Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing Python code between Heroku apps

Tags:

git

python

heroku

I have 2 Heroku apps, both Pyhon, and some python modules that are shared between them (i.e.: they are the same in both apps). What is the best way to set up my git / requirements.txt to achieve the following?

  1. The shared code is versioned (that is: it too sits in Git, I don't care where -- one of the projects? Another project?)
  2. In development, I have a single copy of that shared code on my disk
  3. In development, I can change that single copy of the shared code and have the change reflected in both apps, ideally without doing anything other than changing files on disk.
  4. When pushing to Heroku, all I do is a simple "push"

My gut feeling tells me I need to have the shared code as a Python package, on disk, and in development add it to each app's PYTHONPATH. Further, I would add it (how? using a private server? from s3? as a vendor dir?) to the requriments.txt on each app.

... but I can't quite nail it.

Any thoughts?

like image 722
Nitzan Shaked Avatar asked May 31 '26 07:05

Nitzan Shaked


1 Answers

You can use pip requirements files to do that. You may have one for local development and one for your apps. Please check out the requirements file format for more info.

Local development requirements file

file:///path/to/your/lib/project#egg=MyProject

This should give you readonly access from your app, which is useful for shared code (e.g. git clone of a project). Please note that it will point to HEAD. This should fit requirements 1, 2, 3.

For the project layout, you can check out the setuptools documentation; e.g. create a setup.py and a package with your code in it. You can look at the requests library which is a good example.

App requirements file

git://git.myproject.org/MyProject.git#egg=MyProject

This should pick up whatever code you pushed to your repo. I don't have experience with Heroku, but if they support requirements file that should just work. This should fit requirement 1, and I hope 4.

If you want to use private git repo with heroku, please refer to heroku documentation (please note that it is username:password basic auth and does not have email address). If you don't want to use a password you can use a revocable OAuth token.

like image 80
dnozay Avatar answered Jun 01 '26 21:06

dnozay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!