Skip to main content

To begin, note that Lando is often used for web development and it may not be optimised for running long-running Python scripts or applications.  It's primarily designed to host web services, so if you have specific Python development requirements, you might want to consider using a dedicated Python development environment or container.  However, Lando can be useful for running Python scripts in the context of web development projects.

To run Python within a Lando environment, you can follow these steps:

Set Up a Lando Environment

To begin, make sure you have Lando installed and have set up a Lando environment for your project.  For more details about installing Lando, have a look at 'Lando setup running Drupal 10'.

You can create a Lando configuration file (.lando.yml) in your project's root directory.  Ensure that you specify the desired services and configurations in your Lando configuration file.  For Python development, you might want to include services like web and database as needed.

An example of a .lando.yml file

name: py-ai
recipe: drupal10
config:
  webroot: web
  php: '8.1'
  python: 3.9.2
services:
  phpmyadmin:
    type: phpmyadmin
    hosts:
      - database
  database:
    portforward: 3334
    creds:
      user: py10
      password: py10
      database: py10
  solr:
    type: solr

 

Access the Lando Shell

You can access the Lando shell by running the following command in your project's directory

lando ssh

While the above command works, I found I wasn't in a working Python environment.  To check I ran

python --version

To which I had the following response

bash: python: command not found

To get Python running, exit out shell by entering the command exit.

exit

Now to enter a Python environment in Lando run

lando ssh -s python

Now the response will be similar to the following when you run the command python --version

Python 3.11.6

This will open a shell session within your Lando environment.  The lando ssh command is used with Lando, a local development environment and DevOps tool, to open a Secure Shell (SSH) session into your Lando environment.

Here's what lando ssh does:

Access the Lando Environment: When you run lando ssh, it establishes a secure SSH connection to the Lando environment for your project. This environment typically includes web servers, databases, and other services needed for your web development project.

Command Line Access: After running lando ssh, you get access to a command line terminal within the Lando environment. This allows you to run commands and interact with the services, files, and configurations of your development environment.

Manage Your Project: With the Lando environment's command line access, you can perform tasks like running web development commands, managing services, checking logs, updating configurations, and more. It's a convenient way to perform development-related tasks within the environment.

Isolation: Lando environments are isolated from your host system, which means they have their own file system and dependencies. When you run lando ssh, you're essentially stepping into this isolated environment.

For web development, Lando is commonly used to create and manage local development environments that match the production environment as closely as possible. This makes it easier to develop, test, and debug web applications. The lando ssh command is valuable for managing and interacting with these environments.

Keep in mind that the specific services and configurations available within your Lando environment will depend on how you've configured your .lando.yml file and the needs of your project. The lando ssh command is a powerful tool for working with your Lando-based web development projects.

 

Check Python Version

To check the Python version installed in your Lando environment, you can run:

python --version

This command will display the version of Python installed.  I had the following response

Python 3.9.2

Updating the Python version in a Lando environment involves creating a new Lando configuration with the desired Python version and rebuilding the environment.

Update Lando Configuration

Update your Lando configuration file (usually .lando.yml) to specify the desired Python version.  Using the example noted earlier, update the python line

name: py-ai
recipe: python
config:
 php: '8.1'
 python: 3.11.6  # Update this to your desired Python version
/...

Rebuild the Lando Environment

After updating the configuration, you need to rebuild the Lando environment with the new Python version. Run the following commands in your project directory

lando rebuild

This command will recreate the Lando environment using the updated configuration, which includes the new Python version.  Once rebuilt, check Python version and confirm it has updated.  To action this step without a prompt response add -y to the command

lando rebuild -y

 

Run Python Scripts

You can execute Python scripts directly within the Lando shell. For example, if you have a Python script named myscript.py, you can run it with the following command

python myscript.py

Make sure your Python script is located in a directory accessible from the Lando environment.

 

Install Python Packages

If you need to install Python packages or dependencies, you can use pip, the Python package manager. For example, to install a package like requests, run

pip install requests

This will install the package into your Lando environment.

 

Python Virtual Environments

It's a good practice to create and activate Python virtual environments within your Lando environment to isolate Python dependencies. You can use venv or virtualenv to create virtual environments. Here's an example of creating and activating a virtual environment:

python -m venv myenv
source myenv/bin/activate

This isolates your Python environment and packages.

 

Exit out of ssh

To exit out of a Lando SSH session, simply type the exit command and then press Enter. This will exit the SSH session and return you to your local command prompt.

exit

After running the exit command, you should see a message indicating that you have logged out of the SSH session, and you will be back in your local terminal.
Exiting the SSH session will not stop or affect your Lando environment, so you can continue to use Lando for your local development tasks.

Related articles