Setup Python Virtual Environment and install Selenium and Behave BDD

Tutorial 3​​ ​​ Setup​​ Python​​ Virtual Environment​​ and install Selenium and​​ Behave​​ BDD

Welcome to the 3rd​​ article in Behave BDD series!

What you will Learn :

  • About Python Virtual Environments

  • Install​​ Python​​ Virtual Environment

  • Create Python Virtual Environment

  • Install Selenium

  • Install Behave

  • Deactivate virtual env

About​​ Python​​ Virtual Environments

Python virtual environments are used to isolate different versions of Python projects, different versions of libraries (example, selenium​​ lib) running at the same time in your machine.​​ So, let’s suppose, you want to run your tests using 2 different versions of selenium (selenium_Y, selenium_Z). If we don’t have virtual environment, you would be first running your tests with selenium_Y. Once you are done, you have to override selenium_Y with selenium_Z. However with virtual environments, you don’t have to override. You can simply have 2 separate environments, one having selenium_Y, another having selenium_Z.​​ Similarly, one virtual environment can have Python2, another can have Python3 and so on.​​ So in each environment, we can install whatever versions or dependencies we want.

Virtual environments are​​ especially​​ useful​​ for a QA, different libraries​​ can be installed​​ in different environments to​​ test them out. Usage of virtual environment is optional but highly recommended.​​ 

Install​​ Python Virtual Environment

We will now​​ install​​ a virtual environment for Python 3.​​ 

To get started, open the command prompt and execute below commands. These​​ remind​​ us​​ about​​ the​​ installation​​ paths of​​ python and pip​​ 

You can use either of the below 2 commands to install virtual environment:

pip install virtualenv

OR

python -m pip install virtualenv

So let us execute-- ​​ python -m pip install virtualenv

The below screen shows that virtual env is already installed on my machine​​ 

However, in your case, output similar to below might be seen. This indicates that virtual env is successfully installed (see the last line)​​ 

Create Python Virtual Environment

We will now create a virtual environment for Python 3.

Run the ‘where python’ command to find out the location of python exe, see below.​​ 

We than execute below command to create a virtual env for python 3. Note that venv_py3 is the name of our virtual env. You can name it anything you wish to.

virtualenv --python="C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\python.exe" venv_py3​​ 

So below is the syntax to create virtual env:

virtualenv --python="<location of python exe>" <name of virtual env>

Next, run the dir command, you will see venv_py3 directory being created

Next​​ we will cd to this dir and then cd to Scripts dir

Next​​ run the dir command, you would see activate.bat file

Next run the activate.bat command. Once you execute this command, you will enter inside the virtual environment, shown as rounded brackets (venv_py3)

Now​​ when you run ‘where python’, the first one would be your virtual env

So ‘python –V’ command shows that the python ver is Python 3.7.5 as expected

Install Selenium

Execute ‘pip install selenium’ command

So selenium package/library is installed in our virtual env.

Install behave

Execute ‘pip install behave’ command



So behave package/library​​ 
is installed in our virtual env, see location below

Deactivate virtual env

To​​ deactivate our virtual env, notice below that there is a deactivate.bat file inside the Scripts directory

So just run this command, you will exit from virtual environment. You will no longer see (venv_py3)

So this is how we setup a python virtual environment. If you have another python version on your machine, you can follow the above steps to create another virtual environment!​​ 

We will continue with our setup in our next tutorial. ​​ 

Thank you​​ for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 10th Nov 2024