Alteryx Designer Knowledge Base

Definitive answers from Designer experts.
Check out our powerful new search update! You can read more here. Please let us know if you have any feedback by creating a topic here.

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

Moderator
Moderator
Created

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 until 2019.2

%ALTERYX%\bin\Miniconda3\envs\JupyterTool_vEnv\Lib\site-packages for 2019.3+

 

Example: 

C:\Program Files\Alteryx\bin\Miniconda3\PythonTool_venv

 

As a result, you may need to start Designer with administrator rights if the installation folder does not allow write access to a standard account, like for an admin version for example.

 

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:

 

 

 

 

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:

 

up to 2019.2:

 

 

 

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

 

 

 

 

for 2019.3+:

 

 

 

%ALTERYXDIR%\bin\miniconda3\envs\jupytertool_venv\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 the User Folder

 

This method uses parameter --user to specify that package must be installed in user folder (%APPDATA%/Python/Python36 as per https://www.python.org/dev/peps/pep-0370/#windows-notes).

 

Example:

 

 

 

Alteryx.installPackages(package="logger",
        install_type="install --user")

 

 

 

 

 

Now, in order to use it, package location must be added to default path: %APPDATA%\Python\Python36\site-packages

 

 

Example:

 

 

 

import os,sys
sys.path.insert(1,os.path.join(os.environ['APPDATA'],"Python",f"Python{sys.version_info.major}{sys.version_info.minor}","site-packages"))

import logger

 

 

 

 

 

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 local directory or tar.gz

 

The package must exist in a place accessible by the machine (such as C:\Users\<YOUR USERNAME>\Documents\Personal\PythonPckg).

 

Example:

 

 

 

Alteryx.installPackages(package="file: <your directory\filetoinstall.tar.gz>")

Alteryx.installPackages(package="file:C:\tsmithers\downloads\nlptoolkit-dictionary-1.0.4.tar.gz")

 

 

 

 

 

Procedure: Installation with a proxy in place

 

This allows the option of adding a proxy and proxy credentials to the installation argument. Credentials can be left off or included depending on the environment.

 

Example:

 

 

 

#No password and username
Alteryx.installPackages(package="<mypackage>",install_type="install --proxy proxy.server:port") 

#With password and username
Alteryx.installPackages(package="<mypackage>",install_type="install --proxy [user:passwd@]proxy.server:port") 

 

 

 

 

 

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.

 

Example:

 

 

 

# 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.

 

 

 

from ayx import Alteryx
import re
from pandas import DataFrame
import io
from contextlib import redirect_stdout

with io.StringIO() as current_output, redirect_stdout(current_output):
    
    Alteryx.installPackages(package='',install_type='freeze')
    
    packages = ( (item for item in out_row.split("=") if item) 
    for out_row in re.split(string=current_output.getvalue(),pattern=r"\r*\n") if out_row)
    
    output_df = DataFrame(packages ,columns=["package","version"])

Alteryx.write(output_df,1)

 

 

 

 

Example of result:

 

python_packages.png

 

 

  

Additional Resources

 

Comments

This article is awesome! Thanks for all the help @PaulN

Asteroid

@PaulN 

 

How would one adapt this

 

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

 

to a network drive that has spaces in some of the folder names...

 

So far I have 

Alteryx.installPackages(package="hyperapi",
install_type='install --no-index --find-links="\\xyx.org.com\\d\\w\\Space Here\\Alteryx\\Systems\\For System Use\\Hyper API"')

 

But I get the error

Command '['c:\\users\\name\\appdata\\local\\alteryx\\bin\\miniconda3\\pythontool_venv\\scripts\\python.exe', '-m', 'pip', 'install', '--no-index', '--find-links="\\xyz.org.com\\dfs\\wtops\\Space', 'Here\\Alteryx\\Systems\\For', 'System', 'Use\\Hyper', 'API"', 'hyperapi']' returned non-zero exit status 1.

 

I tried multiple ways with quotes. Is using network drive even possible? I wish whoever made this didn't put spaces in file path names.

Asteroid

Thanks for the article. The machine I use doesn't have admin rights to intall any s/w. 

In that case am not able to run my codes that were running in IDE to adapt to alteyx due to package unavailability. 

Its practically not possible for us to freely develop codes using the Alteryx python module when we have user restrictions on installing the packages. Having admin rights is the only way to install the packages to miniconda in Alteryx? 

 

Can anything be done on this?  

If this is not doable, I would suggest to have all the basic packages for data analysis pre installed in the Alteryx bundle without which this feature is not gonna be of great value add to the tool. 

 

errors

 

Installing collected packages: soupsieve, beautifulsoup4
ERROR: Could not install packages due to an EnvironmentError: [WinError 5] Access is denied: 'c:\\program files\\alteryx\\bin\\miniconda3\\envs\\jupytertool_venv\\Lib\\site-packages\\soupsieve'
Consider using the `--user` option or check the permissions.

 

CalledProcessError: Command '['c:\\program files\\alteryx\\bin\\miniconda3\\envs\\jupytertool_venv\\python.exe', '-m', 'pip', 'install', 'beautifulsoup4']' returned non-zero exit status 1.

  

Asteroid

@Raghu_s 

After going through the same things, yes you absolutely need t o have an admin install this for you. I tried every work around I could think of, and at the end of the day had to wait for the admin to get back from vacation so I could get some tools installed.