Using Python with imports in UiPath

Hi. Ive learned to use python scripts in UiPath with Python Scope->Load Python Script->Invoke Python Method->Get Python Object, and using input paramteres and getting output from return statement etc. It works very well :slight_smile:

The problem is when the python script have external imports like openpyxl and lxml etc. The execution stops at “Invoke Python Method” with the error “One or more error occured”. This happen since UiPath doesnt recognize the python dependencies.

I have spent several days searching for a solution on Google but with no luck.
What I have tried so far:

  • Moving the “name”.py file and the venv folder inside the UiPath project folder.
  • Starting a new python project inside the UiPath project folder.
  • Using an unofficial UiPath package which creates a venv from a requirements.txt file generated from the command “python -m pip freeze >> requirements.txt”
    image

The only solution I found so far is to install the python packages (lxml/openpyxl) systemwide on the machine, but that can be problematic when the machine is shared.

I am using Studio 2021.10.4 and Python 3.9

And also, I think the official UiPath documentation regarding Python Activities should contain information about this topic

2 Likes

As I mentioned in the beginning of my post, I have already learned the basics and got results with simple scripts which your tutorial explain.

The tutorial you linked does not explain what to do when your Python scripts is using imports (dependencies installed in Python like openpyxl, pandas, lxml, numpy etc)
And when using those imports, UiPath fails at “Invoke Python Method: One or more errors occurred”.

Can you show us your XAML file screen shot @Hawkman

When debugging the workflow I get “One or more errors occured” at Invoke Python Method.
When running the workflow I get “Pipe is broken” at Invoke Python Method

Here is the PY script. It works when commenting out all external imports. But fails when trying to load one of the istalled one like numpy etc:
image

Share the error message from here

Look into below screen @Hawkman

RemoteException wrapping System.AggregateException: One or more errors occurred. —> RemoteException wrapping System.InvalidOperationException: Error invoking Python method —> RemoteException wrapping System.IO.IOException: Pipe is broken.
at System.IO.Pipes.PipeStream.CheckWriteOperations()
at System.IO.Pipes.PipeStream.Flush()
at System.IO.StreamWriter.Flush(Boolean flushStream,
Boolean flushEncoder)
at System.IO.StreamWriter.Dispose(Boolean disposing)
at System.IO.TextWriter.Dispose()
at UiPath.Python.Service.PythonProxy.RequestAsync(PythonRequest request,
CancellationToken ct)
at UiPath.Python.Service.PythonProxy.InvokeMethod(Guid instance,
String method,
IEnumerable1 args) at UiPath.Python.Impl.OutOfProcessEngine.<>c__DisplayClass15_0.<InvokeMethod>b__0() at System.Threading.Tasks.Task1.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
— End of stack trace from previous location where exception was thrown —
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at UiPath.Python.Activities.InvokeMethod.d__16.MoveNext()
— End of inner exception stack trace —
at UiPath.Python.Activities.InvokeMethod.d__16.MoveNext()
— End of inner exception stack trace —
at UiPath.Shared.Activities.AsyncTaskCodeActivity.EndExecute(AsyncCodeActivityContext context,
IAsyncResult result)
at System.Activities.AsyncCodeActivity.System.Activities.IAsyncCodeActivity.FinishExecution(AsyncCodeActivityContext context,
IAsyncResult result)
at System.Activities.AsyncCodeActivity.CompleteAsyncCodeActivityData.CompleteAsyncCodeActivityWorkItem.Execute(ActivityExecutor executor,
BookmarkManager bookmarkManager)

Have you pass the python path in the Environment variable @Hawkman

Regards
Gokul

Hi @Hawkman @Gokul001,

There is an alternative way, but this may required additional handling of messages from the terminal. It wont be a off-the-shelf approach.

I have a working approach with an example file in this link.

Python Virtual Environment - Help / Studio - UiPath Community Forum

Also read the threads mentioned in the above thread. You could use a ShareDrive to save your python installation to avoid systemwide module installs.

Yes I tried with different approaches, passing the Python installation Path, and with .exe and with passing the script project folder. Still the same exception message.

I will look into it :slight_smile:

Hello again. I tried your solution. First of all I had to change the command from: env/Scripts/activate.bat, to:
.\venv\Scripts\activate.bat in order to make it work, and it did :slight_smile:
But…again, when using Python imports it does not work.

Here is the Python script (with import Numpy as an example), running successfully in PyCharm:

And here is UiPath running the script with import:

Dont know what more to do… :frowning:

Hi @Hawkman,

Ok lets recap.

We now know that the approach works when using the terminal/powershell in PyCharm (I am unsure whay PyCharm uses as its terminal)

From what you are informing me, I am suspecting you are also using the Windows version of a UiPath project and not a Windows-Legacy version.

The windows version of Invoke PowerShell activity behaves differently: See this thread discussion Invoke powershell unable to recognized aws keyword - Help / Studio - UiPath Community Forum

So I am suspecting that you are getting this error because how the Invoke PowerShell activity uses the user scope of PowerShell and not system scope.

Can you tell us if you are using Windows or Windows-Legacy to build your project?

I am suspecting this because your virtual env should have numpy installed yet the error shown by Invoke PowerShell is “No module found”

I am using windows legacy:
image

Just upgraded to Python 3.10.6
This is the script with installed lxml as import:
image

And it is running fine directly in powershell when using Activate.ps1:
image

But fails in powershell when using Activate.bat:
image

“ModuleNotFoundError” is the same error we get in UiPath when invoking the PowerShell script with Activate.bat
image

I have read that Activate.bat is ment for command prompt, and Activate.ps1 is for PowerShell
If we change the UiPath code in Invoke PowerShell from .bat to .ps1 accordingly, we get another errormessage regarding execution policies:
image

I tried setting the ExecutionPolicy to Bypass on every scope I was allowed to:
image

But still the same execution policy error in UiPath. Maybe It is the MachinePolicy or UserPolicy that need to be set to Bypass, but I am not allowed to change that on the machine. I guess it is a company strict policy.

Update:
My usage for Python in UiPath is not running the entire script anyways through Invoke PowerShell. I need to use the UiPath activities with Python Scope and Invoke Python Method so I can run specific function with sending arguments in from UiPath to the Python script.

I guess the only solution for me is installing the Python packages systemwide on the machine (I have verified that Invoke Python Method work properly then with Python imports) so I can use the UiPath Python activities as intended :slight_smile:

Yes, I’m fairly sure the Python activities don’t support virtual environments, so installing the packages globally is the only option if you want to use them.

That said, you could set up a full separate python installation, keep it out of your env variables, and use the Library Path argument to specify it.

which version of python activities package are you using?

@Hawkman and @Stefan_Reutter,

Using Powershell as I noted in my earlier post, it touch an go and it is possible to integrate with Virtual Environments.

Python Scope alternatives

I still belive it should be possible using Python Scope. UiPath Python Scope does not mandate that (does not say only one installation can be used). You can have another instance of Python standalone in a USB see tests here :
How to run python script in UiPath without installing python? - Help - UiPath Community Forum

Or install/copy in/to a Shared Drive

Python integration with different vm’s - Help / Activities - UiPath Community Forum

In both these above cases, the Python Scope will only look for invoked libraries in respective scopes. i.e., we need not install python modules globally but under respective scopes.

What do you mean by separate python installation and keep it out my env variables?

I got Python 3.10 installed “separatly” as normal. And I use PyCharm for writing python code.
When I start a new project, PyCharm needs a base interpreter (the python installation) for the virtuel env, so the python.exe etc is located in the project folder /venv/Scripts.

How to avoid that as you are saying ?