Python virtualenv setup¶
Python "virtual environments" allow us to create isolated spaces, ensuring that each Python project can have its own set of dependencies without affecting other projects.
In other words, a virtual environment is a directory containing a few scripts that enable us to create a project-specific Python runtime environment, where using the pip install command affects only the project itself and not other projects or the system as a whole.
Note
Using Virtualenv is not strictly necessary for developing bots and integrations with the Teamwire SDK, but we believe it can be useful to avoid dependency conflicts across different projects.
The following sections will guide you through the steps involved in setting up Virtualenv for your Python environment.
Installing virtualenv¶
In order to install Virtualenv, we need to use the pip install command. Depending on your system and your Python setup, you might need to invoke pip3 instead of pip. On Unix systems (e.g. Linux, macOS), you might also need to use sudo to run commands as an administrator. Similarly, on Windows you might need to run the command prompt shell as an administrator.
If the installation succeeds, you can verify that virtualenv is working with the following command:
Creating a virtualenv¶
As mentioned, Virtualenv works on a "per project" basis, so let's suppose we have a project named "my_teamwire_bot" and we want to use Virtualenv with it.
Assuming the my_teamwire_bot directory already exists, we should first create a Python virtual environment for it with the following commands:
In particular, <virtualenv_name> is usually env, so we simply issue the command as follows:
This will create an env directory under my_teamwire_bot.
Activating the virtualenv¶
Once the virtual environment has been created, we should activate it, to ensure that we use isolated dependencies for our project.
Assuming we are still in the my_teamwire_bot directory, we activate the virtual environment with:
On Unix-like systems
On Windows
The shell prompt will change, showing the environment name, as follows:
On Unix-like systems
On Windows
From now on, every time you issue a pip install command, it will only affect the activated project.
Example
To install the Teamwire SDK in an isolated virtual environment for your project, simply run:
Deactivating the virtualenv¶
Once you have finished working on a specific project, you can deactivate its virtual environment.
This can be done with the deactivate command, which is available only if a Virtualenv is currently activated: