Skip to main content

Lando is a development environment tool that makes it easier to set up and manage local development environments for web applications. To use Python within a Lando-based development environment, you can follow these general steps:


Create a Lando Configuration File

Make sure you have a Lando configuration file (e.g., .lando.yml) in your project's root directory. If you don't have one, you can create it. Here's a basic example:

name: py-ai
recipe: drupal10
  webroot: web
  php: '8.2'
  xdebug: false
  composer: []
  composer_version: '2.5.2'
  python: '3.11.6'
    type: phpmyadmin
      - database
    portforward: 3334
      user: dr10
      password: dr10
      database: dr10
    type: solr
    type: python:3.11.6
    port: 80
    ssl: true
    command: tail -f /dev/null
     - "apt-get update -y"
     - "apt-get install build-essential"
      - pip install -r requirements.txt

In this example, we specify a python service and use the requirements.txt file to install Python dependencies.


Create a requirements.txt File

Create a requirements.txt file in your project directory (the same directory as the .lando.yml file). This file should contain the Python packages you need for your project. You can list the packages and their versions like this:


Replace package1, package2, version1, and version2 with the actual Python packages and versions you need.


Start Lando

Open a terminal, navigate to your project directory, and start Lando by running the following command:

lando start


Access Python

You can access Python within your Lando environment using the lando command:

lando ssh -s appserver

As noted above, the service in use is python, therefore the lando command is

lando ssh -s python

This will open a shell inside the python service container. You can run Python scripts and manage your Python environment from there.

A breakdown of the command:

lando: This is the main command or executable that you are running. Lando is a development tool that helps with local development environments for web projects. It simplifies the setup and management of development environments, particularly for web applications.

ssh: This is a command used to establish a secure shell (SSH) connection to a remote server or container. SSH is a protocol for securely connecting to and managing remote systems.

-s python: This part of the command is specifying a service or container to connect to within the Lando environment. It's instructing Lando to SSH into a service named "python."

The command lando ssh -s python is telling Lando to create an SSH connection to a service or container named "python" within your Lando development environment.


Install Additional Python Tools

If you need additional Python tools or packages that are not listed in your requirements.txt, you can install them inside the Lando container using pip.

pip install package3


Use Python in Your Project

You can now use Python in your project within the Lando environment. You can run Python scripts, interact with Python packages, and integrate Python functionality into your web application.

Related articles

Andrew Fletcher17 Feb 2024
How to update installed Python packages
You can use the pip list --outdated command to view a list of installed Python packages that have newer versions available. This command will show you which packages are outdated and can be updated to the latest versions.Here's how you can use itpip list --outdatedThis command will display a list of...
Andrew Fletcher17 Feb 2024
Drupal - Solr working through tm_X3b_en_body error
Having updated Solr, re-indexing wasn't working. The error in the logs wasDrupal\search_api_solr\SearchApiSolrException while indexing item entity:node/2386:en: Solr endpoint bad request (code: 400, body: Exception writing document id...
Andrew Fletcher13 Feb 2024
Python error: in putheader if _is_illegal_header_value(values[i])
Snapshot of the errorFile "/opt/homebrew/Cellar/python@3.11/3.11.7_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/http/", line 1271, in putheader if _is_illegal_header_value(values[i]):The above error points to the putheader definition in file...