0

I'm trying to deploy a django app to heroku.
I have several python libraries which are not on PyPi and so I can't just declare them in requirements.txt file
In local development I've used:

import sys     
sys.path += [os.path.dirname(os.path.dirname(__file__))+"\\project-name\\lib"]

inside manage.py and it works fine there.
Obviously it doesn't work on heroku and I get import errors.

What is the recommended way to add libraries like that on heroku?

Thanks.

tomermes
  • 22,950
  • 16
  • 43
  • 67

1 Answers1

0

One way to do it is include the libraries in the repository itself, from which you can import them. That means simply moving the actual folder for each library into your main Django project folder.

- DjangoProject
    - AppFolder1
    - AppFolder2 ...
    - python-library1
    - python-library2

When the repository is pushed to Heroku your libraries will be pushed as part of the project.

From here, your imports of these libraries within a view/model etc within any app's folder would

import python-library1
from python-library2 import a_function, a_class

The reason why I suggest the directory structure above is that, most likely, you would not have go back and change any import codes.

If you have a large number of libraries and would like to keep the direcory structure simpler, simply create a folder with a name such as "importables" in the main DjangoProject folder and change the import statements to something such as...

from importables import python-library1
from importables.python-library2 import a_function, a_class

It's not exactly beautiful, but a quick way to get the job done. If you aren't sure where the libraries you'd like to include are located, there's a few ways to quickly see their location using Python (How do I find the location of Python module sources?).

Community
  • 1
  • 1
Ian Price
  • 7,416
  • 2
  • 23
  • 34