# Creating a Spotfire package for Python packages from a Windows computer

The installed Spotfire client includes a Python interpreter and a set of packages to enable using Python in Spotfire.

## About this task

One of these packages, 'spotfire', provides tools for building SPKs to share Python packages with other data function authors in an organization, or to enable web client users to see Python-enabled analyses in a web browser. For more information about package workflow, see Package management for the Spotfire Service for Python. If you are using a Python interpreter other than the one provided with your Spotfire installation, then you must first run the following command:

    -m pip install spotfire

Important: You must build a package for the operating system on which your Spotfire Server node manager is installed. This topic describes building the SPK using the spotfire package supplied with your Windows installation of Spotfire. If you are installing the SPK on a Linux node, see Creating a Spotfire package for Python packages on the node manager.

The installed Spotfire client relies on pip, the Python command-line application for Python package installation. The installed Spotfire client uses a requirements.txt file to specify the packages to include in your SPK. By default, the file requirements.txt searches the PyPI package site for the specified package and version.

- To include a package from a different repository or in a local file path, in the requirements.txt file, use the option `-i` or `--index-url`, followed by the location URL.

      #example
      #
      mylib -i http://my.domain.org/lib/1.0.0/mylib/

- To include a .whl package, in the requirements.txt file, provide the relative path to the package from the current working directory.

      #simple-example
      #
      ./my_path/my_package.whl
      packaging==1.0.0

The installed Spotfire client relies on the Python packages included in the installation. Removing any of these packages causes your Spotfire installation to not work with the included Python interpreter. For a list of included packages, see Included packages.

If you are building an SPK intended for the Spotfire Service for Python, then avoid specifying packages that are included with the service installation. Installing a different version of one of these packages can cause unexpected errors. If a package you are installing depends on or requires one of the included packages, then it is filtered out when the SPK is built.

Note: The PIP packaging system might deliver a specious warning in the following form:

    WARNING: Target directory <<temporary location>>\<<package>> already exists. Specify --upgrade to force replacement.

The text of the warning can vary depending on the contents of the third-party wheel packages and operating system. This warning is from the PIP system; for our purposes, it is of no importance and can be disregarded.

For more information about creating a requirements.txt file for your package list, see its documentation at the following location.

- https://pip.readthedocs.io/en/stable/user_guide/#requirements-files
- https://pip.readthedocs.io/en/stable/reference/pip_install/#requirements-file-format

This task creates an SPK for the Spotfire Service for Python, running on a node available to Spotfire Server. If you need to build a package to distribute to installed Spotfire clients, see Python Data Functions in Spotfire®.

Perform this task from a command prompt on the Windows computer where Spotfire is installed.

## Before you begin

- You must have the appropriate Spotfire license for authoring data functions.
- You must have created the file requirements.txt containing the list of packages to include in your SPK.
  The following example specifies these packages and specified versions from PyPI.

      ####### example-requirements.txt ######
      #
      scipy == 1.11.2
      matplotlib == 3.9.2
      statsmodels == 0.14.0

## Procedure

1. From the command line, type the command to create the SPK.
   For our example, use the following command, specifying values for the installed Python Interpreter `<version#>`, the `package-name`, `name.spk`, and the path to the requirements.txt file. See the Options table for more information.

       "%SPOTFIRE_HOME/Modules/Python Interpreter_<version#>/python/python.exe"
       -m spotfire.spk packages
       [--name "<package-name>"]
       <name.spk>
       <path-to>/requirements.txt

   Note: Remember that the path to the Python interpreter has spaces in it, so you must quote the path string.

   Table 1. Options
   Option | Description
   --- | ---
   `spotfire.spk` | The package containing the Python code to download and bundle the needed packages specified in requirements.txt. Tip: From the command prompt, you can view help for the spotfire package by typing the following command: `"<path-to-python-interpreter>\python\python.exe" -m spotfire.spk packages --help`
   `packages` | The subcommand that creates the package bundle.
   `--name` | A string that sets the internal name of the generated package (for deploying multiple SPK files). Note: If you do not specify the `--name` argument, then the package builder reverts to the package name that is embedded in the spotfire.spk stamp in the requirements file (located beneath `"BuiltName"`), to match the previous version of the package. If the package name is missing entirely, it reverts to `"Python Packages Windows"` for server packages.
   `--analyst` | This option is available, but it is not used in this example. This option specifies that the Spotfire Server should distribute the packages in the SPK to other installed Spotfire clients connected to the Spotfire Server. For more information about using this option, see Python Data Functions in Spotfire®. Tip: Everyone should use the same packages and package versions as those deployed on the Spotfire Server.
   `name.spk` | The name of the SPK file that is created by this task.
   `<path-to>/requirements.txt` | The full path to the file requirements.txt.

   The following example creates an SPK named my_pkgs.spk, containing the packages specified in the example requirements.txt.

       "%SPOTFIRE_HOME%\Modules\Python Interpreter_3.12.9.xx\python\python.exe" -m spotfire.spk packages --name "example-packages" my-pkgs.spk c:\files\requirements.txt

   The packages and all of their dependencies are written to the SPK named my-pkgs.spk in the current working directory where the command was run, and the version information is recorded in the file requirements.txt. For example:

       ####### example-requirements.txt ######
       #
       scipy == 1.11.2
       matplotlib == 3.9.2
       statsmodels == 0.14.0
       ## spotfire.spk: {"BrandVersion":2,"Analyst":{"BuiltBy":"3.11.9 (tags/v3
       ## spotfire.spk: .11.3:f3909b8, Apr  4 2023, 23:49:59) [MSC v.1934 64 bi
       ## spotfire.spk: t (AMD64)]","BuiltAt":"Mon Aug 21 11:52:29 2023","Built
       ## spotfire.spk: File":"approved-packages.spk","BuiltName":"Approved Pac
       ## spotfire.spk: kages","BuiltId":"d9310771-b10a-4395-ac46-1c9f344c2c89"
       ## spotfire.spk: ,"BuiltVersion":"1.0.0.0","BuiltPackages":{"contourpy":
       ## spotfire.spk: "1.1.0","cycler":"0.11.0","fonttools":"4.42.1","kiwisol
       ## spotfire.spk: ver":"1.4.4","matplotlib":"3.9.2","packaging":"23.1","p
       ## spotfire.spk: atsy":"0.5.3","Pillow":"10.0.0","pyparsing":"3.0.9","sc
       ## spotfire.spk: ipy":"1.11.2","statsmodels":"0.14.0","tzdata":"2023.3"}
       ## spotfire.spk: },"Server":{}}

2. Locate the SPK you created in the working directory where you ran the command.
3. Add the SPK to the Spotfire Server Deployment area, and then validate and save the area.

## Results

The packages are added to the Spotfire Server node manager, where Spotfire users can access analyses that use the functions in the packages from their web browsers.

For an overview of the entire process, see Package management for the Spotfire Service for Python.