[behave] Add support for executing on python virtual environment
🤔 What's the problem you're trying to solve?
Most of python project use python virtual environment. It would be possible to select winch the cucumber plugin should use for running the scenario.
✨ What's your proposed solution?
A eclipse pydev project already have this feature, in fact in our project we choose to use out test virtual environment with the right version of python, pyton libraries and behave.
The virtual env is in fact a folder in with a real python environment is installed.
⛏ Have you considered any alternatives or workarounds?
No response
📚 Any additional context?
No response
@alef75 can you give some more details? e.g. how do one detects such environment (is it always a special folder?) if it is project specific or needs to be configured individually? How do one starts behave in such virtual environment and what are the steps to setup it?
Python has a standard way to install virtual environment. The virtual environment is a folder with a relevant python version in wich you can install all the python packages of some relevant version for your project. you can have multiple virtual env installed.
if you want to use a specific virtual env manually (in linux) open a terminal
source virtualenvFolder/bin/activate
# all commands here use virtualenv python
bheave .......
...
....
# for exiting virtualenv
deactivate
# from here commands use system python
In eclipse pydev you can choose which python interpreter to use in a project here you can select a virtualenv
all python interpreter in eclipse are defined in the pydev workspace settings :
Alright, so I think we should look how this project settings are stored and how to get the configured path.
If I read https://stackoverflow.com/questions/48174599/is-there-a-single-line-way-to-run-a-command-in-a-python-venv correctly, then one would not call behave but <path/to/venv/bin/>behave then to run with the configured venv.
Alright, so I think we should look how this project settings are stored and how to get the configured path.
If I read https://stackoverflow.com/questions/48174599/is-there-a-single-line-way-to-run-a-command-in-a-python-venv correctly, then one would not call
behavebut<path/to/venv/bin/>behavethen to run with the configured venv.
Yes, but it not enought, whe you activate a vitual env all the path and references point to the virtualenv path, if you don't activate the visrual env (via the command "source virtualenvFolder/bin/activate" ) and call the "<path/to/venv/bin/>command" the command will seerch it's python references in the system folder and it make it not working
Hm... I hoped there would be a one liner to activate and run the command. Maybe I need to further look into how pydev is doing it but as we execute a single (forked) process call it would mean that otherwise we need to run a script/batch file what seems a bit overcomplicated.
I've made some research and some tests and you are right, You don't need to "activate" the virtual environment — just use the venv's Python interpreter directly.
The activation script only sets environment variables for your shell session. When you call the venv's Python binary directly, it automatically uses the correct sys.prefix and finds packages in the venv's site-packages.
From a shell script:
/path/to/venv/bin/python my_tool.py --arg value
From Python (subprocess):
import subprocess
venv_python = "/path/to/venv/bin/python"
result = subprocess.run([venv_python, "my_tool.py", "--arg", "value"],
capture_output=True, text=True)
For installed console scripts (e.g., pytest, black), call them directly from the venv's bin/ folder:
/path/to/venv/bin/pytest
/path/to/venv/bin/black src/
For behave (I've tested and it works):
/path/to/venv/bin/behave .....
@alef75 many thanks I'll take a look how to enable support for virtual environments it should be possible to fetch the values even without pydev directly I need to check the source https://github.com/fabioz/Pydev what ids/preferences are used here.