diff --git a/samples/core/lightweight_component/Lightweight Python components - basics.ipynb b/samples/core/lightweight_component/Lightweight Python components - basics.ipynb index 7b834ce3520..a4d8572dad0 100644 --- a/samples/core/lightweight_component/Lightweight Python components - basics.ipynb +++ b/samples/core/lightweight_component/Lightweight Python components - basics.ipynb @@ -14,7 +14,7 @@ "\n", "There are several requirements for the function:\n", "* The function should be stand-alone. It should not use any code declared outside of the function definition. Any imports should be added inside the main function. Any helper functions should also be defined inside the main function.\n", - "* The function can only import packages that are available in the base image. If you need to import a package that's not available you can try to find a container image that already includes the required packages. (As a workaround you can use the module subprocess to run pip install for the required package.)\n", + "* The function can only import packages that are available in the base image. If you need to import a package that's not available you can try to find a container image that already includes the required packages. (As a workaround you can use the module subprocess to run pip install for the required package. There is an example below in my_divmod function.)\n", "* If the function operates on numbers, the parameters need to have type hints. Supported types are ```[int, float, bool]```. Everything else is passed as string.\n", "* To build a component with multiple output values, use the typing.NamedTuple type hint syntax: ```NamedTuple('MyFunctionOutputs', [('output_name_1', type), ('output_name_2', float)])```" ] @@ -38,9 +38,8 @@ "metadata": {}, "outputs": [], "source": [ - "# Install the SDK and tensorflow\n", - "!pip3 install kfp --upgrade\n", - "!pip3 install tensorflow==1.8.0" + "# Install the SDK\n", + "!pip3 install kfp --upgrade" ] }, { @@ -105,6 +104,12 @@ "from typing import NamedTuple\n", "def my_divmod(dividend: float, divisor:float, output_dir:str = './') -> NamedTuple('MyDivmodOutput', [('quotient', float), ('remainder', float)]):\n", " '''Divides two numbers and calculate the quotient and remainder'''\n", + " #Pip installs inside a component function.\n", + " #NOTE: installs should be placed right at the beginning to avoid upgrading a package\n", + " # after it has already been imported and cached by python\n", + " import sys, subprocess;\n", + " subprocess.run([sys.executable, '-m', 'pip', 'install', 'tensorflow==1.8.0'])\n", + " \n", " #Imports inside a component function:\n", " import numpy as np\n", "\n", @@ -154,7 +159,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -163,7 +168,7 @@ "MyDivmodOutput(quotient=14, remainder=2)" ] }, - "execution_count": 9, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -289,7 +294,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.5" + "version": "3.6.7" } }, "nbformat": 4,