This site uses different types of cookies, including analytics and functional cookies (its own and from other sites). To change your cookie settings or find out more, click here. If you continue browsing our website, you accept these cookies.

This article is part of the Tool Mastery Series, a compilation of Knowledge Base contributions to introduce diverse working examples for Designer Tools. Here we’ll delve into uses of the Python Tool on our way to mastering the Alteryx Designer:

Python is one of the fastest growing programming languages in the world and is used for a wide variety of applications ranging from basic data manipulation to data science and software development. With the release of 2018.3 comes the long-awaited and highly anticipated Python Tool! Much like the R-Tool, the Python Tool allows you to seamlessly run code as a part of your Alteryx workflow. Also like the R-Tool, you will need to have some coding experience with the named language in order to use this tool to its maximum potential. In this Tool Mastery Article, we will introduce you to the fundamentals for using this tool.

When you first drop the Python Tool on to your Canvas you will see the following screen in the tool’s configuration window. This is a reminder to run your workflow whenever you connect your Python Tool to a new input data source. This pulls the input data into the Python Tool so that you can bring it into your Python Code.

As described in this text, to get the Jupyter Notebook interface up and running, all you need to do is wait. IT takes a couple seconds for the Jupyter Notebook interface to get served the first time you open a Python Tool in an instance of Designer. The message you first see will be replaced with a Jupyter notebook interface.

For a general introduction to Jupyter Notebooks, please review their Beginner's Guide documentation.

The first coding step in using the Python Tool is to import the Alteryx API package, which allows you to pass data between the Alteryx Engine and Python Tool. If you plan on reading in data from the Alteryx Engine or pushing data out to the Engine from the Python Tool, your code should start with:

from ayx import Alteryx

This piece of code is so fundamental it is automatically populated in the first cell of the Python Tool!

To run an individual cell in the Python Tool, you click the play button in the top toolbar, or you can use the keyboard shortcut: shift + return.

In addition to the ayx package, the Python Tool comes with a few python packages loaded by default. These packages are listed in the help documentation and primarily relate to Data Science. There is also a great article that reviews the functionality of each of these pre-installed packages. To load a package that is already installed, you can use the import command, as you would when creating a Python Script outside of Alteryx. If you would like to install a python library that is not included with the tool by default, you can use the Package.installPackages() function.

The little * asterisk where the cell number is usually displayed means that the cell is currently running.

On the success of installing a package, you will see some variation of the following messages related to dependencies and the version of the package installed.

Optional Follow Along: If you'd like to follow along with this demonstration, please download the Iris Dataset attached to this article!

If you are bringing in data through the Input Anchor in Alteryx, you will need to run the workflow to make the incoming data available to the notebook. After running the workflow, you can use the Alteryx.read() function to bring the data into Python.

The only argument to this function is the specific connection you are reading in. Like in the R Tool, this argument is a string and will need to have quotations around it.

To read in this data stream as the variable name data, the code would read:

data = Alteryx.read("#1")

If you try to read in data before running the entire workflow, you will likely see this FileNotFoundError:

The solution is to save the workflow and then run the workflow. The next time you run the code in the cell with the play button, the error should be resolved.

Everything read into the Python Tool will be read in as a pandas data frame. This enables greater flexibility for processing the data in Python. You can change the data format after reading it in, but you will need to return any outputs back to a pandas data frame.

Now that I have brought in my data, I would like to analyze it. First, I will create a new cell by clicking the plus icon next to the save/create checkpoint button, or I could use the keyboard shortcut B to add a cell below my current cell.

Other useful cell and notebook functions can also be found in this toolbar to the right of the insert cell below button. From left to right, the buttons are Save, Add a Cell , Cut Cell(s) , Copy Cell(s)) , Paste Cell(s) , Move Cell(s) Up , Move Cell(s) Down , Run , Stop , Restart the Kernel , and Restart the Kernel and Rerun the Notebook . All of these buttons have associated keyboard shortcuts. You can see a full list of Jupyter Notebook keyboard shortcuts by navigating to Help > Keyboard Shortcuts in the top toolbar.

For this demonstration I want to run cluster analysis on the infamous Iris data set, so in my new cell I will load the KMeans function from the Sci-kit learn Python module (included with the Alteryx Python Tool Installation), and write some simple code to create clusters and print the resulting cluster labels.

Now, I can visualize my clusters with the matplotlib.pyplot python library (also included with the Python Tool by default).

Finally, writing an output from the Python Tool can be done using the with Alteryx.write() function. This function is currently only supported for pandas data frames. If you attempt to write something out other than a data frame, you will get the following TypeError.

Now, all that is left to do is run the workflow, and the results will be populated in anchor 1 of the Python Tool Outputs.

With this overview, I hope you feel comfortable reading in, writing out, and processing data in the Python Tool. The only limits now are your imagination!

Things to know and Future Updates!

Starting with 2018.4, you can load externally created python scripts and Jupyter notebooks.

Metadata will not consistently populate in downstream tools for data coming out of the Python Tool.

There is an implicit type conversion from Boolean to integer on reading data into the Python Tool. Likewise, there is another implicit type conversion from Boolean to integer on writing out from the tool.

Starting with 2018.4, you now have the ability to set column data types when writing an output.

Only Pandas Data frames are currently supported for reading and writing out. You can not currently write out a plot, or read in and write out spatial objects.

Question Constants are not currently supported.

If you have any feedback for us on this tool, please post to the Product Ideas Page! Our Product Managers are very active here and would love to see any ideas for features or limitations within the Tool you encounter.

By now, you should have expert-level proficiency with the Python Tool! If you can think of a use case we left out, feel free to use the comments section below! Consider yourself a Tool Master already? Let us know at community@alteryx.com if you’d like your creative tool uses to be featured in the Tool Mastery Series.

Stay tuned with our latest posts every #ToolTuesday by following @alteryx on Twitter! If you want to master all the Designer tools, consider subscribing for email notifications.

Hey @SydneyF, this is a fantastic feature and thank you for the writeup. Quick question, if I install a package via the Python tool in Alteryx, does the package install to a separate conda environment or does it play off of my system's default conda environment and version?

Thank you for the question! The Python Tool does create a separate Python environment in your Alteryx directory under \bin\Miniconda3 called PythonTool_venv. All packages installed via the Python Tool in Alteryx will be installed in the PythonTool_venv environment, separate from your system's default Conda environment.

How would I go about performing a database connection via the python tool to execute a sql statement? I would normally use the command tool to execute the code below... can it be done via the Python tool now?

Thank you for your question, can you please post it to the Designer Forum? This will give it higher visibility on the Community, and allow you to engage with some of our Python experts. To get you started, the Python Tool creates its own virtual environment to ensure that any package installations or other modifications do not impact any Python Installations outside of Alteryx and vice versa. At this point, I would not suggest trying to update the pip version used in the Python Tool, as it may impact how Alteryx.installPackages() works.

Have you upgraded to 2018.4? Workflow constants were added to the Python tool in this release, and you should be able to access them with the function Alteryx.getWorkflowConstant(). After upgrading, try running Alteryx.help() for additional documentation.

Yesterday I created a simple pipeline that took the Master Store File - CO from the sample data and ran it into the Python tool. I just did simple normalization of data using Pandas and then outputted it to make a chart of that data.

Today I created another pipeline into the same Python tool with input #2 and followed this example successfully, however upon running the workflow, I saw that there was an error for the input #1. How does the jupyter notebook interact with Alteryx? The error I am seeing is that there is no valid metadata for pipeline #1, and it is requiring me to re-run the jupternotebook code I ran yesterday. Do I have to rererun the code within the Python tool everytime I disconnect from Alteryx?

Everything worked fine yesterday...and I have an Alteryx.write(dfs,1) statement as shown in the screenshot, but it keeps saying that there is no valid metadata for the outgoing connection 1.

To clarify, if I am in the Python tool and run all of the code, then run the workflow and don't click anywhere else, everything works. But if I click inside the python tool just to look at something and not make any changes, then click outside of the tool into canvas white space, an error is thrown saying there is no valid metadata for output number one.

When will Plots/Charts be supported to write out? I am building ML modules and would like to export the feature impact and correlation matrix plots? As mentioned I tried writing those out and got an error.

@D3100 Check whether you are running Alteryx as an administrator. By looking at the error message looks like you don't have the write access to folders where Alteryx is installed and due to which it is not able to install the packages.

Thanks I have confirmed my company's app store has installed it with Admin user level. So I can't install the new packages. I do however have my own working python 3.7 virtual environments. Is there any way I can point Alteryx to use that environment instead? I found a config file in the Python_venv Tool folder called pyvenv.cfg. It looks like it has the python distribution address. But updating this file seems to have no effect on the Alteryx tool's python SDK. Is there such a file that can be updated to point Alteryx to a different Python base?

It is not possible to change the Python instance the Python tool is associated with. The Alteryx Engine is hardcoded to point to the environment shipped with the software. This is done to ensure that the ayx library has access to all of its dependencies.

The developers of the Python tool are aware of the issue you are encountering and are working on a solution. Stay tuned.

rd916, I'm not yet sure, but I'd be glad to give it a shot 😜 This list of URLs.. are you trying to check the websites themselves and then pull the data from those sites? That is what it sounds like. Assuming that is the case, can you provide a sample site your trying to pull from and what you're expecting to get out of it?