“env” Module of “os” Package (which implements basic routines for working with the OS) in Mojo Programming Language — An Overview

Shriram Sivanandhan
3 min readApr 4, 2024

--

Mojo Programming Language combines the usability of Python with the performance of C, C++ and Rust. Mojo Programming Language utilizes next-generation compiler technologies with features like caching, multithreading and cloud distribution technologies. Further, Auto-Tuning and compile-time Metaprogramming features allows to write code for several hardware. The advantages of Mojo Programming Language are Usability & Programmability, best performance, Interoperability and Extensibility.

As we saw Interoperability of Mojo Programming Language with Python Ecosystem to use Matplotlib Python Visualization Library, Pandas Python Data Analysis and Manipulation Library, NumPy (Fundamental Package for Scientific Computing in Python) Library, SciPy Python Library in Mojo Programming Language and various concepts and implementations regarding Mojo Programming Language in previous Articles/Blogs. In this Article/Blog, we are going to see an overview on env Module of os Package (which implements basic routines for working with the OS) in Mojo Programming Language.

Since the older JupyterLab-based Mojo Playground is deprecated, in this Blog, Programming in Mojo is done using the new Mojo Playground.

“env” Module of “os” Package (which implements basic routines for working with the OS) in Mojo Programming Language:

We are going to use APIs from os Package in Mojo Programming Language. In Mojo Programming Language, env Module of os Package implements basic routines for working with the OS.

In env Module of os Package we have setenv() and getenv() functions which can be imported either in two ways,

from os import setenv (or) from os.env import setenv

from os import getenv (or) from os.env import getenv

1. setenv:

Syntax: setenv(name: String, value: String, overwrite: Bool) -> Bool

setenv() function is used to change or add an Environment Variable.

Arguments:

name (String): Name of the Environment Variable.

value (String): Value of the Environment Variable.

overwrite (Bool): If already an Environment Variable exists with the given name and if overwrite is True, the Environment Variable value is changed or if overwrite value is False, the Environment Variable value is not changed.

Returns:

If the name argument is empty or contains = character, the setenv() function returns False, and in any other case, True is returned by the setenv() function.

Constraints:

This setenv() function only works on macOS or Linux. The setenv() function returns False otherwise.

2. getenv:

Syntax: getenv(name: String, default: String) -> String

getenv() function returns the value of the given Environment Variable.

Arguments:

name (String): Name of the Environment variable.

default (String): Default value to be returned when the getenv() function is called if the Environment Variable doesn’t exist.

Returns:

The value of the given Environment Variable.

Constraints:

This getenv() function only works on macOS or Linux. The getenv() function returns False otherwise.

Here in the Example below we are going to use setenv() and getenv() functions by importing it from env Module of os Package,

Example of using setenv() and getenv() functions by importing it from env Module of os Package in Mojo Programming Language
Example of using setenv() and getenv() functions by importing it from env Module of os Package in Mojo Programming Language (New Mojo Playground)

We had seen an overview on env Module of os Package (which implements basic routines for working with the OS) in Mojo Programming Language. Programming in Mojo and Mojo Programming concepts will be discussed further.

Thankyou for reading this blog on “env” Module of “os” Package (which implements basic routines for working with the OS) in Mojo Programming Language — An Overview.

Reference:

https://docs.modular.com/mojo/manual/

https://docs.modular.com/mojo/manual/basics/

https://docs.modular.com/mojo/stdlib/os/env

--

--

No responses yet