Skip to content

Working With Python3 Virtual Environments

Bryce Salmi edited this page Jan 25, 2018 · 2 revisions

When developing or testing faradayio it's best to use virtual environments. Even normal installations should decouple projects by using virtual environments. This is true even if one does not plan to develop code. Python 3 presents a few steps to ensure you are actually installing and working with files inside of your virtual environment.

Installing Packages Using pip and virtualenv

Refer to the original Python documentation Installing Packages Using pip and virtualenv for the official instructions on this topic. We will walk you through specific examples using faradayio below. This example assumes the following:

  • You are using Python 3.5.2 or newer as virtualenv is included by default.
  • The virtual environment is to be created inside the faradayio git repository root folder

faradayio Virtual Environment Example

Create a virtual environment named .venv inside the git repository root:

$ python3 -m virtualenv .venv
Using base prefix '/usr'
New python executable in /home/bryce/Documents/git/faradayio/.venv/bin/python3
Also creating executable in /home/bryce/Documents/git/faradayio/.venv/bin/python
Installing setuptools, pip, wheel...done.

Now that a virtualenv has been created in our .venv folder we should activate the virtual environment.

$ source .venv/bin/activate
(.venv)$

The virtual environment contains no extra modules needed to run faradayio or perform any testing. Therefore, we want to install the dependencies from the requirements.txt file. However, we want to specify that we're using the virtual environment pip3:

(.venv)$ .venv/bin/pip3 install -r requirements.txt
Collecting attrs==17.4.0 (from -r requirements.txt (line 1))
  Using cached attrs-17.4.0-py2.py3-none-any.whl
Collecting flake8==3.5.0 (from -r requirements.txt (line 2))
  Using cached flake8-3.5.0-py2.py3-none-any.whl
Collecting mccabe==0.6.1 (from -r requirements.txt (line 3))
  Using cached mccabe-0.6.1-py2.py3-none-any.whl
Collecting pluggy==0.6.0 (from -r requirements.txt (line 4))
Collecting py==1.5.2 (from -r requirements.txt (line 5))
  Using cached py-1.5.2-py2.py3-none-any.whl
Collecting pycodestyle==2.3.1 (from -r requirements.txt (line 6))
  Using cached pycodestyle-2.3.1-py2.py3-none-any.whl
Collecting pyflakes==1.6.0 (from -r requirements.txt (line 7))
  Using cached pyflakes-1.6.0-py2.py3-none-any.whl
Collecting pyserial==3.4 (from -r requirements.txt (line 8))
  Using cached pyserial-3.4-py2.py3-none-any.whl
Collecting pytest==3.3.2 (from -r requirements.txt (line 9))
  Using cached pytest-3.3.2-py2.py3-none-any.whl
Collecting python-pytun==2.2.1 (from -r requirements.txt (line 10))
Collecting scapy-python3==0.23 (from -r requirements.txt (line 11))
Collecting six==1.11.0 (from -r requirements.txt (line 12))
  Using cached six-1.11.0-py2.py3-none-any.whl
Collecting sliplib==0.3.0 (from -r requirements.txt (line 13))
  Using cached sliplib-0.3.0-py3-none-any.whl
Collecting sphinx-rtd-theme==0.2.4 (from -r requirements.txt (line 14))
  Using cached sphinx_rtd_theme-0.2.4-py2.py3-none-any.whl
Requirement already satisfied: setuptools in ./.venv/lib/python3.5/site-packages (from pytest==3.3.2->-r requirements.txt (line 9))
Installing collected packages: attrs, pyflakes, pycodestyle, mccabe, flake8, pluggy, py, pyserial, six, pytest, python-pytun, scapy-python3, sphinx-rtd-theme, sliplib
Successfully installed attrs-17.4.0 flake8-3.5.0 mccabe-0.6.1 pluggy-0.6.0 py-1.5.2 pycodestyle-2.3.1 pyflakes-1.6.0 pyserial-3.4 pytest-3.3.2 python-pytun-2.2.1 scapy-python3-0.23 six-1.11.0 sliplib-0.3.0 sphinx-rtd-theme-0.2.4

The output above is an example of already cached modules being installed. These packages could also be outdated so please remember it's just an example.

You are now ready to start running and testing faradayio with a virtual environment using Python 3!