Step-by-Step Guide to Creating Python Virtual Environments

Allan Kouidri
-
8/18/2023
python venv

Virtual environments in Python provide an isolated and controlled environment for developing and running Python projects. They allow you to install project-specific dependencies without conflicting with other projects or the system-wide Python installation.

In this guide, we will explore how to create virtual environments in Python, step by step.

Step 1: verify python installation

Before we begin, ensure that Python is installed on your system. Open a terminal or command prompt and type `python –version` to check if Python is installed and to view the version number. If Python is not installed, visit python.org and download the latest stable version compatible with Ikomia.

On the date of publication of this post, we recommend installing Python 3.10. Please check the latest updates on the Ikomia API to see the list of compatibility.

Step 2: install virtualenv

Though not mandatory, installing the virtualenv package can simplify the process of creating and managing virtual environments. To install virtualenv, open a terminal or command prompt and type `pip install virtualenv`.


pip install virtualenv

Compared to the built-in ‘venv’, ‘virtualenv’ offers advanced features such as the ability to clone existing environments and support for more customization options like using different versions of Python. 

Step 3: create a new virtual environment

Open a terminal or command prompt, navigate to the directory where you want to create your virtual environment, and execute the following command:


python -m virtualenv my_ikomia_env --python=python3.10

Here, ‘my_ikomia_env’ is the name of the virtual environment. You can choose any name you prefer. This command creates a new directory called ‘my_ikomia_env’ in your current location and sets up the virtual environment structure within it.

If you have several versions of Python installed, virtualenv allows you to create virtual environments with custom Python interpreters using `-python=python3.x`.

Step 4: activate the virtual environment

To start using the virtual environment, you need to activate it. The activation process varies depending on your operating system:

  • For Windows (Command Prompt or PowerShell):

my_ikomia_env\Scripts\activate

  • For Unix/Linux (Bash/Zsh):

source my_ikomia_env/bin/activate

Once activated, you should see the virtual environment name (`my_ikomia_env` in this example) prefixed to your command prompt, indicating that you are working within the virtual environment.

A black background with white textDescription automatically generated with low confidence

Step 5: installing packages

With the virtual environment active, you can now install packages and dependencies specific to your project without affecting the global Python installation or other virtual environments. Use the pip package manager to install packages as usual, for example to install ikomia:


pip install ikomia

To exit the virtual environment, simply run the following command:

deactivate

This command will restore your command prompt to its original state, outside the virtual environment.

Embracing virtual environments for Python development

Virtual environments are valuable tools for Python developers as they provide a controlled and isolated environment for project-specific dependencies. By following the steps outlined in this guide, you can easily create and manage virtual environments for your Python projects.

Using virtual environments helps ensure the reproducibility and reliability of your projects by keeping their dependencies separate from each other and from the system-wide Python installation. 

Exploring the features of Ikomia's ecosystem

To learn about the Ikomia API, refer to the documentation. You may also check out the list of state-of-the-art algorithms on Ikomia HUB and try out Ikomia STUDIO, which offers a friendly UI with the same features as the API.

Arrow
Arrow
No items found.
#API

Build with Python API

#STUDIO

Create with STUDIO app

#SCALE

Deploy with SCALE