# Development setup pycyphal (Ubuntu)

**URL:** <https://forum.opencyphal.org/t/development-setup-pycyphal-ubuntu/1844>\
**Category:** Development & Maintenance\
**Created:** [March 11, 2023, 4:54pm UTC](https://forum.opencyphal.org/t/development-setup-pycyphal-ubuntu/1844 "2023-03-11T16:54:05Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![maksim.drachov](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/maksim.drachov/32/1577_2.png) [@maksim.drachov](https://forum.opencyphal.org/u/maksim.drachov)\
**Post date:** [March 11, 2023, 4:54pm UTC](https://forum.opencyphal.org/t/development-setup-pycyphal-ubuntu/1844/1 "2023-03-11T16:54:05Z")

</div>

This guide assumes you’re using Ubuntu (in my case a VM running on Amazon EC2 as I’m on a Macbook).

## 1. Setup VM

If you already have a Ubuntu setup, you can skip this section.

1. Go to the [EC2 Management Console](https://eu-central-1.console.aws.amazon.com/ec2/home)
2. Launch Instance, with the following settings:
  - Name: `pycyphal-vm`
  - OS: `Ubuntu`
  - Instance type: `t2.large` (default `t2.micro` doesn’t have enough RAM)
  - Key pair: Create new key pair and save.
  - Other settings: default

3. Connect to instance using SSH client (using key pair)  
 ![4-ssh-connect](https://forum.opencyphal.org/uploads/default/original/2X/d/d75759589add9d3b18aed5458702587f31595efb.png)  
 ![1-connect-ssh](https://forum.opencyphal.org/uploads/default/original/2X/8/81aeba605adf422799c58bf28ab9ccc298d8d4ef.png)

Setup GitHub

1. Generate SSH key

```bash
$ ssh-keygen -t ed25519 -C "your_email@example.com"

```

2. Add to agent

```bash
$ eval "$(ssh-agent -s)"
$ ssh-add ~/.ssh/id_ed25519

```

3. Add public key to [GitHub settings](https://github.com/settings/keys):

```bash
cat ~/.ssh/id_ed25519.pub

```

## 2. Setup Ubuntu

1. Install ncat, pip, nox

```bash
sudo apt-get update
sudo apt-get -y install ncat
sudo apt install python3-pip
sudo apt install nox

```

2. On EC2 VMs, the following errors might appear:

```auto
subprocess`.CalledProcessError: Command '('sudo', 'modprobe', 'vcan')' returned non-zero exit status 1.

```

To prevent this type of error:

```bash
sudo apt-get install -y linux-modules-extra-$(uname -r)

```

([source](https://github.com/linux-can/can-utils/issues/46#issuecomment-792857631))

## 3. Setup pycyphal

1. Clone repo

```bash
git clone git@github.com:OpenCyphal/pycyphal.git
cd ~/pycyphal
git submodule update --init --recursive

```

2. Run nox (this will setup a virtual environment that can then be used for `pytest`)

```auto
nox --list # Lists all available sessions
nox --session test-3.10 # Runs the tests in Python 3.10
# nox --session check_style # Runs black code style check

```

3. Setup (necessary for `pytest`, recommended to save these commands in setup.sh to save some time!)

```bash
export CYPHAL_PATH="/home/ubuntu/pycyphal/demo/custom_data_types:/home/ubuntu/pycyphal/demo/public_regulated_data_types" # EC2 default username is ubuntu, replace if relevant ($HOME causes issues with Vscode debug)
export PYTHONASYNCIODEBUG=1
source .nox/test-3-10/bin/activate
# source setup.sh # to save time

```

4. In another terminal window (leave open/running):

```bash
ncat --broker --listen -p 50905

```

5. Pytest

```bash
pytest pycyphal/ # Runs all unittest/doctests in pycyphal folder (code)
pytest tests/ # Runs all unittests in tests folder
pytest tests/transport/udp -k _output_session # Runs only unittest from tests/transport/udp, whose unittest name matches the given substring (-k option)
pytest tests/ --pdb # the --pdb option will stop execution upon failing a unittest and open a debug console

```

 ![pytest]()

## 4. Setup Vscode

### 4.1 Remote Session

If you want to use Vscode, you’ll need to do the following steps:

1. Install the [Remote SSH](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-ssh) extension
2. Open Command Palette:
  1. `Remote SSH: Connect to Host`
  2. `Configure SSH Hosts...`
  3. `/users/maksimdrachov/.ssh/config`
  4. Add the new host: (use ssh command copied from EC2)  
 ![config]()

3. Open Command Palette:
  1. `Remote SSH: Connect to Host`
  2. Select added host

4. Open pycyphal folder  
 ![5-vscode](https://forum.opencyphal.org/uploads/default/original/2X/3/3bc4a18a9ca46feafb313ea14e16b90b24d816b5.jpeg)

### 4.2 Debug

1. Install [Python](https://marketplace.visualstudio.com/items?itemName=ms-python.python) extension
2. Open Command Palette:
  1. `Python: Configure Tests...`
  2. `pytest`
  3. `Use existing config file setup.cfg`

3. For pytest to work correctly
  - Create file `.env-variables` with the following text:

```auto
CYPHAL_PATH="/home/ubuntu/pycyphal/demo/custom_data_types:/home/ubuntu/pycyphal/demo/public_regulated_data_types"
PYTHONASYNCIODEBUG=1

```

(In my case username is `ubuntu`, this might be different for you)
  - Edit `.vscode/settings.json`:

```auto
{
 "python.testing.pytestArgs": [], # This should already be in place after step 2
 "python.testing.unittestEnabled": false, # and this
 "python.testing.pytestEnabled": true, # and this
 "python.envFile": "${workspaceFolder}/.env-variables", # but not this one
}

```

4. Restart Vscode window  
 ![6-vscode-testing](https://forum.opencyphal.org/uploads/default/original/2X/2/26c7522f393b47293aea0a87f428b1cbeb28282b.jpeg)

Now you should be able to run unittests in Debug mode, place breakpoints,…

PS: In my case it is was put automatically, however make sure the Python interpreter from the nox session is used. If this isn’t the case:

- Open Command Palette
  - `Python: Select Interpreter`
  - `Enter interpreter path...`
  - Enter path: `./.nox/test-3-10/bin/python`

## 5. Finishing touches

To leave the code as clean as you found it, the following tools are part of CI and so should be consulted before wrapping up a MR.

### 5.1 Black: code formatting

Install:

```bash
sudo apt install black

```

To run:

```bash
cd ~/pycyphal
nox --session check_style

```

Let’s say you did something wrong, now you can fix it:

 ![7-black-style](https://forum.opencyphal.org/uploads/default/original/2X/d/d0eb694024aec6bff6663fd77ac59faf027560dc.png)

(or you could just install the [Black Formatter](https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter) extension)

### 5.2 Pylint: code analysis

Install:

```bash
sudo apt install pylint

```

For example, you’re working on the udp part, run this:

```bash
pylint --disable=import-error,line-too-long pycyphal/transport/udp

```

(`import-error` and `line-too-long` are covered by MyPy and Black)

### 5.3 MyPy: static typing

Install:

```bash
sudo apt install mypy

```

For example, you’re working on the udp part, run this:

```bash
mypy pycyphal/transport/udp | grep pycyphal/transport/udp

```

(There’s probably a more conscise command? @pavel.kirienko)
