Mini-tut: getting started with Python

Hello all,
After a long hiatus I’ve been able to start to work with UiPath a little once again. I was very pleased to find Python activities available, but I ran into a snag here and there. I’m sharing a simple workflow to get you started. I’ll update this mini-tut as I complete more workflows.

To start, you’ll need to make sure you have Python activities available, even if you’ve loaded them for other projects, and might expect to find them already available in your new project. In 2019.6, that wasn’t the case for me. From “Manage Packages”, make sure you have this installed:
python%20activities

We’re going to keep this as simple as possible. All there is is a Python Scope activity, and one Load Python Script Activity. Here’s an overview of what your sequence will look like:

The%20sequence%20overview

The Python Scope entries are crucial. I was NOT able to work with Python 3.7 64-bit, my system default. So I installed 64-bit Python 3.6 in ‘C:\Python36’. I used the generic download from python.org. Here’s what the scope properties panel looks like for me:

python%20scope%20properties

That’s a variable in the Input → Path field (the only one we’ll use). The entry looks like this:

In the Expression Editor for the variable, it looks like this:

The%20variable%20expression%20for%20prog

Here is the Load Python Script activity, seen as when double-clicking on it.

load%20activity%20shot

Now we want to load that Python script as shown. For our purposes, we won’t add a path. I’m assuming the EVERYTHING is in the project directory - .xaml, python script, and output text file. Take a look at the properties:

loading%20the%20script%20properties

And here it is in the Expression Editor (remember, no path needed for this simple exercise):

load%20-%20expression%20editor

Okay, we’re ready to run the Python. Here’s the contents of the script:

f = open("C:\\Users\\YourUserName\\Documents\\UiPath\\InvokePython\\helloworld.txt", "w")
f.write("Hello World from UiPath running Python. This time the script is loaded, not code.")   
f.close()

As you can see, you WILL have to change the path here, IN the file ‘Test.py’, to let UiPath know where to put the output text file.

WARNING: If you don’t add this absolute path, UiPath will drop ‘helloworld.txt’ in the folder where the NuGet package is, and you in all probability don’t want that. There are a number of posts about this elsewhere on the forum, but first things first.

About the attached files: I’m including ‘Py5.xaml’, and ‘Test2.py’. Be sure to rename the Python file to ‘Test.py’ once you’ve edited it to add the path as above, or else change the fields in the script. Your choice.

By all means let me know if this doesn’t work for you. I’ll be adding at least one more version (using code in the ‘Load Python Code’ activity instead of loading a script to run.

Once again - change that file path! :slight_smile:

Regards,
burque505

SimplePython.zip (2.0 KB)

3 Likes

Second installment: Providing code in the UiPath Expression Editor, and not from a script.

(First, see @ronanpeter’s solution for plenty more information.)

The only difference between this workflow and the previous one is where we put the python code. In this version, everything is basically the same, but the code goes here:

loadcod-code-properties

This by itself doesn’t tell you much, but take a look at what’s in the expression editor:

So you can paste the python code in if you like, here it is:

"
f = open('C:\\Users\\Winter\\Documents\\UiPath\\InvokePython\\helloworld.txt', 'w')
f.write('Hello World from UiPath running Python. Using Code input instead of File.')
f.close()
"

Notice that you can use a multiline string now. In the past you might have had to write something like this:

"f = open('C:\\Users\\Winter\\Documents\\UiPath\\InvokePython\\helloworld.txt', 'w')" + Environment.NewLine + "f.write('Hello World from UiPath running Python. Using Code input instead of File.')" + Environment.NewLine + "f.close()" + Environment.NewLine + ""

What a pain to code! Oh well, you don’t have to do it any more.

I’ve quite wrongly assumed here that you’d be using a VB.Net based workflow. You may prefer C# and by using that for your processes. If you are using C#, this next screenshot may well be superfluous for you, but in case you’re just getting started with C# too, here it is:
LoadCode-Code-CSharp

As you can see, the only real difference is the “@” preceding the multiline string. Here’s the code in case you want to paste it in:

@"
f = open('C:\\Users\\Winter\\Documents\\UiPath\\InvokePython\\helloworld.txt', 'w')
f.write('Hello World from UiPath running Python. This is a C-sharp process.')
f.close()
"

I hope this is of some use to you.
Best regards,
burque505

1 Like

Fantastic
Currently i m working on python activities to get to a URL and get the output from that URL (like how we do in web services), is that possible with this python activity buddy
Cheers @burque505

@Palaniyappan, if you haven’t already tried it, take a look at Beautiful Soup. The home page has links to a number of projects you may be able to adapt to your purposes. The most up-to-date version can be installed with pip, and you can find info on it here.
Good luck with it!
Best regards,
burque505

Great
Let me try that once and will let you know
Cheers @burque505

Third installment:
This is a really simple way to get a Python 3.7.3 script running and pull in all the required packages.
All it takes is a sequence with one “Start Process” activity to call your version of Python with arguments.

NOTE: The attached .xaml won’t work for you as-is. You’ll need to match the Python program path and the argument path to suit your system. The attached script, “widgets_example_pyside.pyw” WILL require that you install pyside on your system (“pip install PySide2”, probably), but other than that should work.

Start Process properties:
startpythonprocess

The sequence:
startypyside

The output:
pyside_samples2

Regards,
burque505

InvokePythonSomeWay.zip (3.9 KB)

Below tutorialon python in UiPath

I have followed same process but my code getting stucked at python scope even I have provided all details