Linux - Manually Creating a Virtual Environment using Poetry

Overview

This guide will walk you through the steps of manually creating a virtual using Poetry to work with LibreOffice.

Note

This guide assumes you have already installed LibreOffice and poetry.

See also: Configure a Poetry environment

On Linux it is simple to create a virtual environment using Poetry once you have the the path to the LibreOffice python.

See Get LibreOffice Python Path for instructions on how to get the path to the LibreOffice python.

For this guide we will assume the path is /usr/bin/python3.

Steps

Create Project Directory

Create a Directory for your project.

In this case we will use a directory called myproject in the home directory.

mkdir ~/myproject
cd ~/myproject

Init using Poetry

Use poetry to initialize a project.

poetry init

Output:

This command will guide you through creating your pyproject.toml config.

Package name [project]:
Version [0.1.0]:
Description []:  My fantastic project
Author [Secret Name <secret@name.nowhere>, n to skip]:
License []:  MIT
Compatible Python versions [^3.9]:  ^3.8

Would you like to define your main dependencies interactively? (yes/no) [yes] n
Would you like to define your development dependencies interactively? (yes/no) [yes] n

The generated pyproject.toml file will look something like:

[tool.poetry]
name = "project"
version = "0.1.0"
description = "My fantastic project"
authors = ["Secret Name <secret@name.nowhere>"]
license = "MIT"
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.8"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

Instruct Poetry to use specific python

poetry env use /usr/bin/python3

Output:

Creating virtualenv project in /home/paul/myproject/.venv
Using virtualenv: /home/paul/myproject/.venv

Activate the Virtual Environment

source .venv/bin/activate

Install OOOENV

The virtual environment has been created but it does not yet have access to uno.py and unohelper.py which are needed to use the LibreOffice API.

For this we will use the oooenv package.

Make sure you have activated the virtual environment.

oooenv is a Python package that allows you to auto configure a virtual environment to be used by LibreOffice.

Install oooenv in the virtual environment:

poetry add --group=dev oooenv

Note

The --group=dev option is used because we only need oooenv for development purposes. This option instructs Poetry to only add oooenv to the dev-dependencies section of the pyproject.toml file.

Now that the package is installed we can use it to configure the virtual environment to use uno.py and unohelper.py.

oooenv cmd-link -a

Now the virtual environment is configured to use uno.py and unohelper.py.

Install additional packages using poetry

Now that we have a virtual environment that can be used by LibreOffice, we can install additional packages using poetry such as ooo-dev-tools.

poetry add ooo-dev-tools

Now we can take advantage of OooDev.

Start python from our virtual environment.

$ python

Run a simple test to make sure everything is working.

>>> import uno
>>> from ooodev.loader import Lo
>>> from ooodev.calc import CalcDoc
>>>
>>> def say_hello(cell_name):
...     doc = CalcDoc.from_current_doc()
...     sheet = doc.sheets[0]
...     sheet[cell_name].value="Hello World!"
...
>>> _ = Lo.load_office(Lo.ConnectSocket())
>>> doc = CalcDoc.create_doc(visible=True)
>>> say_hello("A1")
>>> doc.close()
>>> Lo.close_office()