community
cancel
Showing results for 
Search instead for 
Did you mean: 

Alteryx Designer Knowledge Base

Definitive answers from Designer experts.

How To: Use Alteryx.installPackages() in Python tool

Moderator
Moderator
Created on

How To: Use Alteryx.installPackages() in Python tool

 

Installing a package from the Python tool is an important task. In this article, we will review all the possible functionality included with the Python method Alteryx.installPackages().

 

Prerequisites

 

  • Product - Alteryx Designer
    • Version  ≥ 2018.3

 

Background Information

 

First of all, don't get confused: you can use either Alteryx.installPackage(), Alteryx.installPackages(), or Package.installPackages() to achieve same result.

 

By default, packages are installed under %ALTERYX%\bin\Miniconda3\PythonTool_venv\Lib\site-packages (example: C:\Program Files\Alteryx\bin\Miniconda3\PythonTool_venv),which means you may need to start Designer with admin rights if the installation folder does not allow write access to a standard account.

 

Typically, people use installPackage() with a single argument (the package name(s)). But, looking at the method itself, there are in fact 3 parameters:

 

Parameter Comment
package A string or list of strings of package name(s)
install_type Optional: default value: "install". Pip command to use.
debug

Optional: default value: False

Add some details to the output

 

Function signature:

Signature: Alteryx.installPackages(package, install_type=None, debug=None, **kwargs)

 

In reality, Alteryx.installPackages() is nothing more than a wrapper for the pip (Python Package Manager) command. 

 

 

This means that following command:

Alteryx.installPackage(package="logger")

 

is interpreted as:

%ALTERYXDIR%\bin\miniconda3\pythontool_venv\scripts\python.exe -m pip install logger

 

This can be seen using debug parameter:

 

Spoiler
Alteryx.installPackage(package="logger",debug=True)

 

 Output (duplicates due to debug mode):

Executing subprocess: install logger
[Executing subprocess: c:\users\xxxx\appdata\local\alteryx\bin\miniconda3\pythontool_venv\scripts\python.exe -m pip install logger ]
[Subprocess success!]
Collecting logger
Installing collected packages: logger
Successfully installed logger-1.4
Collecting logger
Installing collected packages: logger
Successfully installed logger-1.4

 

Procedure: Standard Installation

 

In this case, only the package argument is specified. 

 

Examples:

# With one package
Alteryx.installPackage("logger")
Alteryx.installPackage(package="logger")

# With multiple packages
Alteryx.installPackage(["logger","wordcloud"])
Alteryx.installPackage(package=["logger","wordcloud"])

# With a specific version
Alteryx.installPackage("logger==1.3")

# Without cache enable to ensure that package is downloaded before installation
Alteryx.installPackage(package="logger",install_type="install --no-cache-dir")

 

 

Procedure: Installation from GitHub

 

Git must be installed on the machine to use this method. It could be downloaded from https://git-scm.com/downloads.

Instead of the package name, specify git URL prefixed with git+.

 

Example:

Alteryx.installPackages(package="git+https://github.com/alteryx/promote-python.git")

 

 

Procedure: Installation of a Module in a Different Folder

 

This method uses parameter --target to specify the destination and creates it if needed.

 

Example:

Alteryx.installPackages(package="logger",
        install_type="install --target=C:\\ProgramData\\PythonLibs")

Now, in order to use it, the package needs to be imported using Alteryx.importPythonModule(%MODULE_PATH%) [2018.4 and above]

 

Example:

logger = Alteryx.importPythonModule("C:\\ProgramData\\PythonLibs\\logger")
logger.logging.info("Logger installed successfully")

 

Remark: With this method, a module does not appear as installed in PythonTool environment

 

Procedure: Installation from Wheels

 

In this case, we use --no-index and --find-links with the local repository to ensure that package is not going to be downloaded.

 

Example:

 

Here, C:\ProgramData\PythonWheels contains following files (numpy and Pillow are dependencies of wordcloud):

 

numpy-1.16.3-cp36-cp36m-win_amd64.whl
Pillow-6.0.0-cp36-cp36m-win_amd64.whl
wordcloud-1.5.0-cp36-cp36m-win_amd64.whl

 

Alteryx.installPackages(package="wordcloud",
                       install_type="install --no-index --find-links=C:\\ProgramData\\PythonWheels)

 

Procedure: Uninstall Package(s)

 

Specify uninstall as the install_type parameter and either a string with the package name or a list of strings with package names.

 

Examples:

# For a single package
Alteryx.installPackages(package="wordcloud", install_type="uninstall")

# For multiple packages
Alteryx.installPackages(package=["wordcloud","pillow"], install_type="uninstall")

 

Procedure: Download Wheels or Archive Files

 

In this case, the command to use is download instead of the default install. One may also specify a destination folder with parameter --dest

 

Example:

Alteryx.installPackages(package="wordcloud",install_type="download --dest=C:\\ProgramData\\PythonWheels")

 

 

Procedure: List the Currently Installed Modules

 

The following procedure provides a basic way to list the module names and versions installed along with Python tool.

 

Note that it will not work with Production mode as it uses a feature specific to Jupyter.

 

from ayx import Alteryx
import re
import pandas as pd

 

The following uses the magic command capture to store the output of pip command into variable out

 

Here we use freeze to obtain a list of names and versions:

%%capture --no-display out
Alteryx.installPackage(package="",install_type="freeze")

 

# Split on carriage return (\r if present and \n)
# Filter on empty items (if item and it out_row)
packages = ( (item for item in out_row.split("=") if item) 
     for out_row in re.split(string=out.stdout,pattern=r"\r*\n") if out_row)

# Alteryx.write() requires a dataframe
output_df = pd.DataFrame(packages ,columns=["package","version"])
Alteryx.write(output_df,1)

 

Example of result:

 

python_packages.png

 

  

Additional Resources