Python command line tool not working --- python3-lxc

I have got a weird problem and I wonder whether others have the same…

I am trying to use the Python package python3-lxc but it does not appear to connect to the LXC API.

I start a container and verify that it is running.

$ lxc list -c s mycontainer
+---------+
|  STATE  |
+---------+
| RUNNING |
+---------+

or

$ lxc info mycontainer
Name: mycontainer
Remote: unix://
Architecture: x86_64
Created: 2018/01/21 13:01 UTC
Status: Running
Type: persistent
Profiles: default
.
... more stuff

Then I use below sample code

# file name: lxctest.py

#!/usr/bin/python3
import lxc
import sys

c = lxc.Container("mycontainer")
if c.defined:
    print("Container already exists", file=sys.stderr)
    sys.exit(1)
else:
    print("The container is NOT defined")

When I run it, I get The container is NOT defined whereas I should get Container already exists as response.

$ ./lxctest.py 
The container is NOT defined

System details

$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 17.10
Release:	17.10
Codename:	artful
$ uname -a
Linux machinename 4.13.0-25-generic #29-Ubuntu SMP Mon Jan 8 21:14:41 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

Used Snap to install LXD

sudo apt remove --purge lxd lxd-client
sudo groupadd --system lxd
sudo usermod -G lxd -a $USER
sudo snap install lxd
newgrp lxd
lxd init

Installed python3-lxc

$ apt-cache show python3-lxc
Package: python3-lxc
Architecture: amd64
Version: 2.1.0-0ubuntu1
Priority: optional
Section: python
Source: lxc
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 137
Depends: liblxc1 (= 2.1.0-0ubuntu1), python3 (<< 3.7), python3 (>= 3.6~), python3:any (>= 3.3.2-2~), libc6 (>= 2.4)
Recommends: lxc-templates (= 2.1.0-0ubuntu1)
Filename: pool/main/l/lxc/python3-lxc_2.1.0-0ubuntu1_amd64.deb
Size: 20666
MD5sum: 38fdb4fd7391c4011abd6a70b917afea
SHA1: 6e4f79cf0a0ad3c645f0489e9dc7782d69a6a934
SHA256: c5382a62b2f95cb9d2c3d658bad83c76c456fe59c47bd8db36e14d218d184564
Homepage: https://linuxcontainers.org
Description-en: Linux Containers userspace tools (Python 3.x bindings)
 Containers are insulated areas inside a system, which have their own namespace
 for filesystem, network, PID, IPC, CPU and memory allocation and which can be
 created using the Control Group and Namespace features included in the Linux
 kernel.
 .
 This package contains the Python 3.x bindings.
Description-md5: ee623a31db200104a7207d9de42b8f04
Supported: 9m

I think that python3-lxc is for LXC 1.0, not LXD.

If you want to access LXD in Python, use python3-pylxd instead (https://github.com/lxc/pylxd).

1 Like

@simos Thank you for your suggestion! I will look into python3-pylxd now.

Yes, python3-pylxd (https://github.com/lxc/pylxd) is the way to go…

However, at present, when pylxd is installed via pip, it can neither connect to the LXD socket. As of today, doing a pip install pylxd will get you version 2.2.4, which cannot find the LXD socket when LXD runs as a Snap.

Luckily there is already an enhancement with commit 8b08c41df07d7eefd072a2ecb5144bdb2413b271. See
https://github.com/lxc/pylxd/blob/8b08c41df07d7eefd072a2ecb5144bdb2413b271/pylxd/client.py#L250 but it is currently only part of the master branch.

I hope they are going to cut a new version soon and publish it at https://pypi.python.org/pypi, but in the meantime the below commands will make it work.

python3 -m venv myvenv --system-site-packages
source myvenv/bin/activate
pip install wheel
pip install -ve git://github.com/lxc/pylxd.git@master#egg=pylxd

Note, the install takes some time since the git clone takes quite a long time (i.e. couple of minutes) .

1 Like