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
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”
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
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”.
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
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)
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.
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
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:
Just upgraded to Python 3.10.6
This is the script with installed lxml as import:
And it is running fine directly in powershell when using Activate.ps1:
But fails in powershell when using Activate.bat:
“ModuleNotFoundError” is the same error we get in UiPath when invoking the PowerShell script with Activate.bat
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:
I tried setting the ExecutionPolicy to Bypass on every scope I was allowed to:
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
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.
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.