0

I'm working with Fn Project as part of Oracle Cloud Functions and I'm looking to include some helper functions that I wrote myself that are currently used in a few other programs. I'd like to include these functions without having to copy and paste them into my source code, and I feel like there's a way to do this that I haven't managed to find.

Here is what my directory structure looks like. Names have been changed to make it easier to understand.

- helper
  - foo.py
- other
  - example.py
- functions
  - myfunc
    - func.py
    - func.yaml
    - requirements.txt

Currently, in example.py I have the following:

from helper import foo
...
foo.bar() 

When I write func.py I write the same thing but when I run fn deploy -app test_app and then fn invoke test_app myfunc it errors out on the import statement (I ran the basic hello world function with from helper import foo at the top:

Error invoking function. status: 502 message: function failed

My first instinct was to put helper in the requirements.txt file but that only works with pip packages. How do I ensure that my own code is included?

Any help would be much appreciated!


Update

So far my only solution has been to literally copy and paste my code into the source file, which is not very maintainable.

Ash Oldershaw
  • 302
  • 2
  • 13

1 Answers1

0

Once you do from helper import foo inside func.py, the entire code including the library import will be done in func.py. Using the command pip freeze > requirements.txt in your main directory, you can list all the dependent libraries of your project inside the requirements.txt. Hence when you create the docker image it will install everything from requirements.txt.

Aparna
  • 422
  • 2
  • 5
  • Thanks for this, perhaps my question wasn't clear enough - this is actually about the `foo` file I've written. Would it be included in the generated docker image - i.e. will `foo.bar()` work? – Ash Oldershaw Jul 13 '20 at 12:52
  • You have written `foo.bar()` inside `example.py` and this `bar()` function is present inside `helper/foo.py` right? You will be generating a docker image of your entire project, which consists of all the files and hence all the .py files, functions inside that, libraries as in requirements. txt will be there. – Aparna Jul 13 '20 at 12:57
  • Hi Aparna, I've updated the question. It doesn't include the function as it seems to be outside the scope of the Fn function. – Ash Oldershaw Jul 13 '20 at 14:16
  • 1
    I believe you are creating the docker image of functions folder (in case your project), but the functions you have used inside that is outside that and hence it won't be there in your docker image as per my understanding. If you want `foo.py` and `example.py` in that image the it must be in scope, ie. create a folder and then keep all these `helper, others and functions` inside that and create a docker image of . – Aparna Jul 13 '20 at 15:03
  • That's exactly right - is there any way you know of whereby I can include the helper functions - because I want to be able to use these functions in multiple docker images – Ash Oldershaw Jul 13 '20 at 18:18
  • 1
    Other than including these required .py files in your project, I am not sure how to make use of this for multiple docker images. If yo want to use the same code in multiple projects, then only way I can think of is creating an image of that common code. So that every project's docker container can interact with this common container. This is not at all a good solution, but I am not getting any other solution in my mind. – Aparna Jul 15 '20 at 07:22
  • Thanks for that Aparna :) – Ash Oldershaw Jul 17 '20 at 11:04