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

**URL:** https://www.finalbuilder.com/forums/t/cant-seem-to-run-python-in-an-existing-virtual-environment/7539
**Category:** Discussion
**Created:** [September 15, 2024, 8:42pm UTC](https://www.finalbuilder.com/forums/t/cant-seem-to-run-python-in-an-existing-virtual-environment/7539 "2024-09-15T20:42:55Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![ktopaz](https://www.finalbuilder.com/forums/user_avatar/www.finalbuilder.com/ktopaz/32/2390_2.png) [@ktopaz](https://www.finalbuilder.com/forums/u/ktopaz)
#### Post date: [September 15, 2024, 8:42pm UTC](https://www.finalbuilder.com/forums/t/cant-seem-to-run-python-in-an-existing-virtual-environment/7539/1 "2024-09-15T20:42:55Z")

</div>

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:

```auto
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

---

<div class="post-metadata">

### Author: ![Sparky](https://www.finalbuilder.com/forums/user_avatar/www.finalbuilder.com/sparky/32/10_2.png) [@Sparky](https://www.finalbuilder.com/forums/u/Sparky)
#### Post date: [September 16, 2024, 7:08am UTC](https://www.finalbuilder.com/forums/t/cant-seem-to-run-python-in-an-existing-virtual-environment/7539/2 "2024-09-16T07:08:28Z")

</div>

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.

```auto
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.
