How can the python packages be installed in uipath?

Hello

I try to run the Python code using python activities in the UiPATH Studio.

But, following packages must be installed in advance.

from facenet_pytorch import MTCNN
from facenet_pytorch.models.utils.detect_face import extract_face
import cv2
import os
from PIL import Image
from matplotlib import pyplot as plt
import numpy as np
import math
import torch
import tensorflow as tf
import base64
import requests
from tensorflow.keras.preprocessing import image 

Iโ€™m stuck here.

How can the packages be installed in uipath?

Thank you in advance. :slight_smile:

@sssi

If the package is already installed and configured in the machine which you are running the bot then itโ€™s fine

Can you try running it through UiPath and check what errors you are facing

Check below link for your reference

Hope this may help you

Thanks

Hi @sssi,

  1. One way of managing dependencies is to make a copy of your dependencies using pip --freeze command if you are using pip. You do this first in the environment where all the dependencies of your code is installed and your code runs as it should.

    pip freeze - pip documentation v22.2.2

    pip freeze -r requirements.txt
    
  2. Later when you are running your automation, ensure that the robot PC has atleast one instance of Python installed and that the requirements.txt file can be accessed via the robot pc. You can even save the contents of the requirements.txt as a text asset in the Orchestrator and fetch it as and when you need from the robot.

  3. You can use the Invoke PowerShell activity to run the following command (ensure the you have a full path to requirements.txt generated in Step 1). Another requirement is that pip should be cmdlet which can be accessible by PowerShell.

    pip install -r requirements.txt
    

    PowerShell can now install all the modules you require.

  4. Let your python code do the rest. (Python scopeโ€“>Returnsโ€“>UiPath(.net classes) )

Alternative:
A similar process can be used if you are using conda. The commands will be slightly different.
When active in your source environment

conda list -e > requirements.txt

When you are in a given target conda enviornment you can use

conda install --file requirements.txt

Hello, @Srini84

I checked that the package was installed in the system and that the code runs as it should on other IDLE.

But, the following error occurred when I run it in UiPath.

The error message says, โ€œThe pipe is broken.โ€

RemoteException wrapping System.AggregateException: ํ•˜๋‚˜ ์ด์ƒ์˜ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.  ---> RemoteException wrapping System.InvalidOperationException: Python ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์ค‘ ์˜ค๋ฅ˜  ---> RemoteException wrapping System.IO.IOException: ํŒŒ์ดํ”„๊ฐ€ ๋Š์–ด์กŒ์Šต๋‹ˆ๋‹ค. 
   ์œ„์น˜: System.IO.Pipes.PipeStream.CheckWriteOperations()
   ์œ„์น˜: System.IO.Pipes.PipeStream.Flush()
   ์œ„์น˜: System.IO.StreamWriter.Flush(Boolean flushStream,
Boolean flushEncoder)
   ์œ„์น˜: System.IO.StreamWriter.Dispose(Boolean disposing)
   ์œ„์น˜: System.IO.TextWriter.Dispose()
   ์œ„์น˜: UiPath.Python.Service.PythonProxy.RequestAsync(PythonRequest request,
CancellationToken ct)
   ์œ„์น˜: UiPath.Python.Service.PythonProxy.InvokeMethod(Guid instance,
String method,
IEnumerable`1 args)
   ์œ„์น˜: UiPath.Python.Impl.OutOfProcessEngine.<>c__DisplayClass15_0.<InvokeMethod>b__0()
   ์œ„์น˜: System.Threading.Tasks.Task`1.InnerInvoke()
   ์œ„์น˜: System.Threading.Tasks.Task.Execute()
--- ์˜ˆ์™ธ๊ฐ€ throw๋œ ์ด์ „ ์œ„์น˜์˜ ์Šคํƒ ์ถ”์  ๋ ---
   ์œ„์น˜: System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   ์œ„์น˜: System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   ์œ„์น˜: UiPath.Python.Activities.InvokeMethod.<ExecuteAsync>d__16.MoveNext()
	--- End of inner exception stack trace ---
   ์œ„์น˜: UiPath.Python.Activities.InvokeMethod.<ExecuteAsync>d__16.MoveNext()
	--- End of inner exception stack trace ---
   ์œ„์น˜: UiPath.Shared.Activities.AsyncTaskCodeActivity.EndExecute(AsyncCodeActivityContext context,
IAsyncResult result)
   ์œ„์น˜: System.Activities.AsyncCodeActivity.System.Activities.IAsyncCodeActivity.FinishExecution(AsyncCodeActivityContext context,
IAsyncResult result)
   ์œ„์น˜: System.Activities.AsyncCodeActivity.CompleteAsyncCodeActivityData.CompleteAsyncCodeActivityWorkItem.Execute(ActivityExecutor executor,
BookmarkManager bookmarkManager)

@sssi

Check below post for your reference

Hope this may help you

Thanks

Thanks for reply, @Srini84

I checked the post you linked above but, I couldnโ€™t find answer for my case.

This is my python code.

I pass the image path as parameter to detect_face function.

And it returns boolean value depending on whether the image has a face or not using the MTCNN library.

But, the error occurs and, the error message says, โ€œThe pipe is broken.โ€ (I posted above.)

import cv2
import os
import numpy as np
from facenet_pytorch import MTCNN

def detect_face(img):

    mtcnn = MTCNN()

    if os.path.isfile(img) != True:
        raise ValueError("Confirm that ", img, " exists")

    path = np.fromfile(img, np.uint8)
    img = cv2.imdecode(path, cv2.IMREAD_UNCHANGED)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    boxes, probs, landmarks = mtcnn.detect(img, landmarks=True)

    if (boxes is None):
        raise Exception('Cannot detect face')
        return False
    else:
        return True

Hi, @jeevith
Thank you for your kind answer :slight_smile:

I solved the package dependency problems, but I am stuck with this problem now.

Can you help me?

Hi @sssi,

If you are facing any dependency issue of the packages you can upgrade/downgrade the package from manage packages to best suit your requirements.

Thanks

Hi @sssi

How did you solve this? May be others having the same problem can use what you used. Kindly pen it down in this thread.

def detect_face(img):

You are doing everything right here. But one heads up is converting python return types to .net types can be often clumbersome.

What I learnt from expierence is that it is easier to use json string as a return from python.

For example, your code could look like:

import json
import cv2
import os
import numpy as np
from facenet_pytorch import MTCNN

def detect_face(img):

    mtcnn = MTCNN()

    if os.path.isfile(img) != True:
        raise ValueError("Confirm that ", img, " exists")

    path = np.fromfile(img, np.uint8)
    img = cv2.imdecode(path, cv2.IMREAD_UNCHANGED)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    boxes, probs, landmarks = mtcnn.detect(img, landmarks=True)

    if (boxes is None):
        raise Exception('Cannot detect face')
        result = { 'result': False}  
        return json.dumps(result)
    else:
        result = { 'result': True}  
        return json.dumps(result)

In UiPath you can then use the desearlize json activity (UiPath.Web.Activities) to get the value of return value result

Here is another example of using json strings and parsing it in UiPath : How to use permuation and combination in excel uipath - Help - UiPath Community Forum


Some threads for you to read about Pipe is broken error, which I have come across and some reasons for it.

1 Like

Hi @jeevith

This was the solution.

If the package is already installed and configured in the machine which you are running the bot then itโ€™s fine. You donโ€™t need to install the package additionally.

You donโ€™t need to install the package additionally in UiPATH.


Iโ€™ll try the way you suggested. Thank you so much. :grin: :grin:

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.