Python Virtual Environment

Hi UiPath community,

I have been trying to use a python virtual environment (created in VS code) within the Python Scope Application in Studio, but I get a python engine error. Is it possible to use virtual environments within UiPath, or does Python need to be installed globally?

Adding this feature would help a lot to avoid situations where Python may not be installed on another PC (such as a client’s VM).

Hi @Donnas,

I agree that this could be really good to support out of the box. However, if you want to run the python scope from a python installed in a shared drive/VM / USB stick you can look at this workaround here :

Via a shared drive : Python integration with different vm's - #7 by jeevith

Via a USB : How to run python script in UiPath without installing python? - #4 by jeevith

The point being, UiPath’s invoke python scope does not care where python is installed but it wants to see a full installation python in the folder path provided in the activity.

Since the folder structure of a virtual env is not similar to a standard install of python, it fails. If you use the python.exe from “env/Scripts/” (as the python installation folder being parsed by UiPath does not support the virtualenv folder structure).

For people who are wondering what the error message we get:
image

In this case there is an alternative:

Another approach is to skip Python Scope activity and rather use ever versatile Invoke PowerShell activity.

  1. cd to your working folder path where you have the virtual environment
  2. Activate the virtualenv from PowerShell
  3. Run python “pythonfile.py”
  4. In the Invoke PowerShell activity, return the output as PSOutput (as the datatype required)

Resources

Param(
[Parameter(Mandatory=$true)] [string]$VirtualEnvFolder,
[Parameter(Mandatory=$true)] [string]$PythonFilePath
)
cd $VirtualEnvFolder
env/Scripts/activate.bat
python $PythonFilePath
def runInVirtualEnv():
    return "I just ran on a virtual env!"

print (runInVirtualEnv())

PythonVirtualEnv.xaml (8.8 KB)

Why not use the default activate.ps1 to activate?
I use the default .bat file to activate the virtualenv. You can use activate.ps1 if you wish, but in my experience .ps1 scripts usually require higher user access with a set execution policy Set-ExecutionPolicy Unrestricted . It is done for security, but when activating virtualenv .bat file will work without errors.

Hope this helps you and others.

2 Likes

Thanks for your reply, I will have a look at what you have suggested!

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