Python

Python Virtual Environment

How to create, activate, deactivate, and delete Python virtual environments for clean QA automation projects.

Python Virtual Environment thumbnail

What Is A Python Virtual Environment?

Most Python projects we develop have dependencies, which means code written by other people. We install and use those packages so we do not have to rewrite everything ourselves. For example, Django is a library we can use to create web apps, and there are thousands of other packages written by other developers.

When we install a package, we install a specific version. Over time, packages change as new features are developed. If we update a package months later, our code may stop working and we may need to change our code. By using a virtual environment, we can separate the dependencies of one project from another. That way, different projects can use different versions of the same library.

A Python virtual environment has two parts:

  • A Python interpreter.
  • Third-party libraries installed inside that environment.

These environments are separated from each other, which means a change in one environment does not affect another. This lets us install and use different Python and library versions for different projects.

What Is It Used For?

To create a Python virtual environment, first download and install Python on your computer. After that, create a project folder and create a virtual environment inside it.

There are different ways to create a virtual environment, but first it helps to know the common tools.

Packages For Handling Virtual Environments

There are several tools for working with Python virtual environments:

  • venv comes pre-installed with Python 3 or higher.
  • virtualenv is a superset of venv and helps create virtual environments more quickly.
  • conda offers packages and dependencies for Python and other languages.

How To Create A Python Virtual Environment With venv And Python 3

You need to have Python installed on your computer. First, use the command line to create a project folder and go to that directory. Then create a virtual environment in the current folder by running python -m venv with the environment name.

cd desktop
mkdir pythonve
cd pythonve
python -m venv venvName
python --version

Activating A Python Virtual Environment On Windows

To activate the virtual environment on Windows, run the activation script from the environment's Scripts folder.

venvName\Scripts\activate.bat

After activating the virtual environment, its name appears in parentheses at the start of the terminal prompt.

(venvName) C:\Users\love\Desktop\pythonve>

Activating A Python Virtual Environment On Mac

To activate the virtual environment on Mac, use the source command and the environment's bin/activate script.

source venvName/bin/activate

How To Deactivate

After you finish working with a virtual environment, or when you want to change to another virtual environment, deactivate it by running:

deactivate

After deactivation, the virtual environment name disappears from the prompt.

How To Install Packages On A Virtual Environment

Let's see examples of how to install third-party packages in a Python virtual environment. You can check the pre-installed packages by running pip list.

pip list

Example output:

Package    Version
---------- -------
pip        24.0
setuptools 69.0.0

We need to install other dependencies that are not pre-installed. For example, here is how to install seleniumbase. Selenium is a free, open source automated testing framework used to validate web applications across different browsers and platforms.

pip install seleniumbase

Here is another example of installing a package. Django is a popular Python framework for building web applications.

pip install Django

How To Delete A Python Virtual Environment

If you want to delete a virtual environment, you can simply delete its folder. No uninstall is required.

How To Use A Virtual Environment On Python 2

You can use the virtualenv tool to install a Python virtual environment on Python 2. It is a tool used to create an isolated Python environment. You must have Python 2 and pip installed on your system.

Use pip2 to install the virtualenv Python module. To install a virtual environment on Python 2, follow these steps:

  1. Get the full file path to the version of Python you installed.
  2. Go to the directory where you want to create your project. This is where you will create the new virtual environment.
  3. Create the virtual environment while specifying the Python version you want to use. The command will be virtualenv path venvName.
  4. To activate the virtual environment, run the activate command.

Conclusion

A Python virtual environment is a great tool for keeping a codebase working while updating Python and dependencies to new versions. It also lets you use different versions for separate projects.

References

Python Virtual Environment | SuperSQA