Skip to main content
infervour.com

Back to all posts

How to Execute the Erlang Command Using Python?

Published on
7 min read
How to Execute the Erlang Command Using Python? image

Best Tools for Executing Erlang Commands Using Python to Buy in October 2025

1 Programming Erlang: Software for a Concurrent World

Programming Erlang: Software for a Concurrent World

BUY & SAVE
$35.99
Programming Erlang: Software for a Concurrent World
2 Erlang Programming: A Concurrent Approach to Software Development

Erlang Programming: A Concurrent Approach to Software Development

BUY & SAVE
$39.99
Erlang Programming: A Concurrent Approach to Software Development
3 Elixir in Action

Elixir in Action

BUY & SAVE
$49.99
Elixir in Action
4 Metaprogramming Elixir: Write Less Code, Get More Done (and Have Fun!)

Metaprogramming Elixir: Write Less Code, Get More Done (and Have Fun!)

BUY & SAVE
$17.00
Metaprogramming Elixir: Write Less Code, Get More Done (and Have Fun!)
5 Learn You Some Erlang for Great Good!: A Beginner's Guide

Learn You Some Erlang for Great Good!: A Beginner's Guide

BUY & SAVE
$41.99
Learn You Some Erlang for Great Good!: A Beginner's Guide
6 Functional Web Development with Elixir, OTP, and Phoenix: Rethink the Modern Web App

Functional Web Development with Elixir, OTP, and Phoenix: Rethink the Modern Web App

BUY & SAVE
$28.00 $45.95
Save 39%
Functional Web Development with Elixir, OTP, and Phoenix: Rethink the Modern Web App
7 Testing Elixir: Effective and Robust Testing for Elixir and its Ecosystem

Testing Elixir: Effective and Robust Testing for Elixir and its Ecosystem

BUY & SAVE
$29.83 $45.95
Save 35%
Testing Elixir: Effective and Robust Testing for Elixir and its Ecosystem
8 Introducing Erlang: Getting Started in Functional Programming

Introducing Erlang: Getting Started in Functional Programming

BUY & SAVE
$33.99
Introducing Erlang: Getting Started in Functional Programming
+
ONE MORE?

To execute the Erlang command using Python, you can make use of the subprocess module in Python. The subprocess module allows you to create new processes, connect to their input/output/error pipes, and obtain their return codes.

Here is an example of how to execute the Erlang command using Python:

  1. Import the subprocess module:

import subprocess

  1. Define the Erlang command as a string:

erlang_command = "erl -eval 'io:format(\"Hello, World!\")' -noshell"

  1. Execute the command using subprocess.run():

result = subprocess.run(erlang_command, shell=True, capture_output=True, text=True)

In this example, we use the subprocess.run() function to execute the Erlang command. The shell=True argument instructs the function to use the system shell to execute the command. The capture_output=True argument captures the output of the command, and text=True ensures that the output is returned as a string.

Finally, you can access the output of the Erlang command as follows:

output = result.stdout print(output)

This will print the output generated by the Erlang command.

Note that you may need to modify the Erlang command according to your specific requirements. The example presented here executes a simple Erlang command that prints "Hello, World!" using the io:format/1 function.

What is the best way to capture the output of an erlang command executed with Python?

To capture the output of an Erlang command executed with Python, you can make use of the subprocess module in Python. Here's an example of how you can do it:

import subprocess

def capture_erlang_output(command): process = subprocess.Popen(["erl", "-noshell", "-eval", command, "-s", "init", "stop"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = process.communicate() return stdout.decode().strip()

erlang_command = "" output = capture_erlang_output(erlang_command) print(output)

In the above code, the Popen function from the subprocess module is used to execute the Erlang command in a separate process. The stdout parameter is set to subprocess.PIPE to capture the output of the command.

The output of the Erlang command is obtained using process.communicate(), which returns a tuple containing the standard output and standard error output. Since we are only interested in the standard output, we store it in the stdout variable.

Finally, we decode the output from bytes to a string using stdout.decode(), remove any leading/trailing whitespace using strip(), and then print the output.

Note: In the example above, it assumes the Erlang runtime is available in the system's PATH and can be executed simply by calling erl. If the Erlang binary is located elsewhere, you might need to specify the full path to the erl command.

How to pass command-line arguments to an erlang command using Python?

To pass command-line arguments to an Erlang command using Python, you can use the subprocess module. Here's an example:

import subprocess

def run_erlang_command(args): command = ['erl'] + args result = subprocess.run(command, capture_output=True, text=True) return result

Example usage

arguments = ['-eval', 'io:format("Hello, World!~n")'] output = run_erlang_command(arguments) print(output.stdout)

In this example, the run_erlang_command function takes a list of command-line arguments as input. It constructs a command list by appending the args to the erl command. Then, it uses subprocess.run to execute the command, capturing the output and returning it. The capture_output=True argument captures both the stdout and stderr, and text=True ensures the result is returned as a string.

You can modify the arguments list to pass any desired command-line arguments to the Erlang command. The captured output will be stored in output.stdout and can be processed further as needed.

When managing and supervising Erlang processes executed through Python, the recommended way is to use the ErlPort library. ErlPort provides a communication channel between Erlang and Python, allowing you to spawn and control Erlang processes from Python.

Here are the steps to manage and supervise Erlang processes executed through Python using ErlPort:

  1. Install ErlPort: First, install the ErlPort library in your Python environment. You can install it using pip:

pip install ErlPort

  1. Start the Erlang node: Before interacting with Erlang processes, you need to start an Erlang node. You can start an Erlang node using the erlport.erlang.call function:

import erlport.erlang

erlport.erlang.setup()

  1. Spawn Erlang processes: Use the erlport.erlang.spawn function to spawn new Erlang processes. This function takes an Erlang module name and a function name to execute:

import erlport.erlang

pid = erlport.erlang.spawn(None, 'my_module', 'my_function', [])

  1. Interact with Erlang processes: Use the erlport.erlang.cast or erlport.erlang.call functions to send messages to Erlang processes and receive responses:

import erlport.erlang

Sending a message to the Erlang process

erlport.erlang.cast(pid, {'message': 'Hello'})

Sending a synchronous call and receiving the response

response = erlport.erlang.call(pid, {'command': 'get_data'})

  1. Supervision: ErlPort provides a basic supervision feature, allowing you to monitor and restart Erlang processes automatically when they crash. You can define a supervisor module in Erlang and start it from Python:

import erlport.erlang

Start the supervisor

supervisor_pid = erlport.erlang.spawn(None, 'my_supervisor', 'start', [])

These are the basic steps to manage and supervise Erlang processes executed through Python using the ErlPort library. The library also provides more advanced features, such as registering processes, linking processes, and handling message patterns. You can refer to the ErlPort documentation for more details and examples.

The recommended way to execute Erlang commands on multiple nodes with Python is by using the "erlport" library. "erlport" provides a Python interface to interact with Erlang nodes and allows you to execute Erlang commands remotely.

Here's an example of how you can use "erlport" to execute Erlang commands on multiple nodes:

  1. Install erlport using pip:

$ pip install erlport

  1. Start an Erlang node(s) using the erl command:

$ erl -sname node1

Erlang/OTP 24 [erts-12.1.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit] [dtrace]

Eshell V12.1.3 (abort with ^G) (node1@your_hostname)1>

  1. Create a Python script to execute Erlang commands on multiple nodes:

import erlport.erlterms as erlterms import erlport.erlang as erlang

define the list of Erlang nodes

nodes = ["node1@your_hostname", "node2@your_hostname"]

def execute_erlang_command(command): # iterate over the list of nodes for node in nodes: # establish a connection to each node connection = erlang.Terminal(protocol=erlang.OTPProtocol, node=node)

    # execute the Erlang command on the node
    result = connection.execute(command)
    
    # print the result (from each node)
    print(f"Node {node}: {erlterms.erlterm\_to\_python(result)}")
    
    # close the connection
    connection.close()

execute an Erlang command on the nodes

command = "really_cool_module:awesome_function()." execute_erlang_command(command)

In this example, the execute_erlang_command function takes an Erlang command and executes it on each node. The result is printed, and the connection is closed for each node.

You can add as many nodes as required to the nodes list. Replace "node1@your_hostname" and "node2@your_hostname" with the actual names of your Erlang nodes.

Make sure your Erlang nodes are running and accessible before executing the Python script.

What are the prerequisites for running erlang commands with Python?

To run Erlang commands with Python, you need to install the following prerequisites:

  1. Erlang: Install the Erlang programming language on your system. You can download it from the official Erlang website (https://www.erlang.org/downloads).
  2. ErlPort: ErlPort is a library that allows communication between Erlang and Python. Install ErlPort by running the command pip install erlport in your terminal or command prompt.
  3. Python: Install Python on your system. You can download it from the official Python website (https://www.python.org/downloads).
  4. Python ErlPort module: Import the ErlPort module in your Python code using the command import erlport. This module provides the necessary functionality to interact with Erlang.

Once you have these prerequisites installed, you can run Erlang commands using Python and communicate seamlessly between both languages.