Can't seem to run Python in an existing virtual environment

Hi Guys,

I’ve created a virtual environment in all our windows server agents:
cd C:\FinalBuilder\Utils & python -m venv LimitedPolicyVenv
I then activated the environment and installed the lxml package (pip install lxml)
I do see it when running pip freeze while the venv is activated

I then try to run a Continua CI 1.9.2.1388 “Python” action to run a script
I check “Use command line executable in virtual environment scripts folder if it exists”
and set the working folder to C:\FinalBuilder\Utils\LimitedPolicyVenv
Which is my virtual environment as noted above (it has a “Scripts” folder in it)

I then run the Continua CI configuration / stage / action -
And it fails due to a missing lxml module:

Python [services_lister.py] Failure
Parameters
Working Directory: C:\FinalBuilder\Utils\LimitedPolicyVenv
Executable: C:\Program Files\Python312\Python.exe
Arguments: -q services_lister.py Deployment\AutoMarketingDeployment-FB7\AutoMarketingDeployment.config.xml QA --ci_var ServicesList_AutoMarketing
Environment Variables (user- or action-defined)
No environment variables specified
Output
Traceback (most recent call last):
  File "C:\FinalBuilder\Utils\LimitedPolicyVenv\services_lister.py", line 5, in <module>
    import lxml.etree as ET
ModuleNotFoundError: No module named 'lxml'

Am I doing something wrong?
Can you kindly explain how can one run a python script inside a pre-existing virtual environment folder, making sure the virtual environment is properly activated first?

Thanks!
Arik

Hi Arik,

The “Use command line executable in virtual environment scripts folder if it exists” option checks for the VIRTUAL_ENV environment variable. If the virtual environment has been activated then VIRTUAL_ENV should contain the path to the virtual environment folder. If %VIRTUAL_ENV%\Scripts\\python.exe exists, then it executes that file rather than the one in the path folder.

The python activate scripts basically do nothing more than set the PROMPT, PATH, PYTHONHOME and VIRTUAL_ENV environment variables. The Python VirtualEnv action sets these variables automatically for all child action processes (except for the PROMPT variable which is not really needed in a CI environment).

If you don’t want to use the Python VirtualEnv action, then you can set the above environment variables in the Environment tab of the Python action. e.g.

VIRTUAL_ENV=C:\FinalBuilder\Utils\LimitedPolicyVenv
PYTHONHOME=
PATH=C:\FinalBuilder\Utils\LimitedPolicyVenv\Scripts;%PATH%

Running any of the activate scripts before running the Python action won’t work, because the environment variables are set at process level, and the new values are therefore lost after the process completes.