How to use export with Python on Linux

Use Export With Python In Linux

The ‘export’ command in Linux is typically used to set an environment variable. Environment variables are dynamic values that can affect the behavior of processes and programs running in the operating system.

The ‘export’ command helps with setting environment variables and influencing the behavior of ongoing processes. This command follows the syntax ‘export VARIABLE_NAME=”value”‘, where spaces are not allowed between the variable name and value. Additionally, the ‘env’ and ‘echo’ commands are used to view these variables. Integrating ‘export’ with Python scripts involves creating a script, setting environment variables via a shell script, making the script executable, and executing it. This process is instrumental in managing dynamic values essential for process environments in Linux

Also read: Installing Python in Alpine Linux

The basic syntax of the ‘export‘ command

export VARIABLE_NAME="value"

This is the basic syntax of the ‘export‘ command. ‘V1‘ is your variable name and ‘value‘ is your variable value. Here, you can’t add a space between the variable name and value.

Here is one example of an ‘export‘ command:

export V1="Hello, World!"

In the above example, we set a value of a variable as ‘Hello, World!‘ with variable name ‘V1.‘ Now the value Hello, World! is stored in variable V1.

Export Command

Now you can see a list of environment variables by using the command ‘env‘ or ‘echo‘ with ‘$VARIABLE_NAME‘.

echo $VARIABLE_NAME
Echo

Here we use the ‘echo $V1‘ command line to get the value of variable V1 and the output is “Hello, World!”.

So this is the basics about export command in Linux.


Also read: Downgrade From Python 3.7 to 3.6 on Windows, MacOS, and Linux

Integrating Export Command with Python Scripts in Linux

The export command with Python in Linux is typically used to refer to setting environment variables. Environment variables are key-value pairs that store information about the environment in which the process runs.
Here are steps to use export with Python:

  • Create a Python script
  • Create a shell script to set environment variables using export
  • Make the shell script executable
  • Run the shell script

Creating a Python Script for Environment Variables

Create a Python script. You can create a Python script using vi or nano text editor.

With vi editor:

vi python_script.py

With nano editor:

nano python_script

Here we create a Python script with vi editor. For example, we create a Python script for the addition of two numbers named example.py. To start typing the Python script, press’ i ‘to get into insert mode. Now you can start typing your Python script.

import os

n1 = int(os.getenv("N1", 0))
n2 = int(os.getenv("N2", 0))

sum = n1 + n2

print(f"The sum is: {sum}")

Once done with typing, press Esc to exit insert mode. To save and exit type ‘:wq!’ and press Enter.

Export Eg

In the above example, by using ‘import os‘, we import the ‘os‘ module. The ‘os‘ module in Python includes various functions for file operations, environment variables, process management, and many more.

‘os.getenv()’ is one of the function of the ‘os‘ module. This is used to retrieve the values of environment variables as N1 and N2. n1 and n2 stored the retrieved values of environment variables.

If the environment values are not set, the default value 0 is used. The ‘sum = n1 + n2’, calculates the sum of two integers obtained from the environment variables. Finally, the last line prints the result of the addition.

  • Create a shell script, here we will use ‘export‘ to set environment variables. For example, create a shell script named ‘script.sh‘ where we set environment variables with their values.

To create a shell use the following command:

vi script.sh

Press’ i ‘ to enter insert mode and then start typing your shell script.

#!/bin/bash

export N1=5
export N2=7

python3 example.py

After done typing, press Esc to exit insert mode, and to save and exit type ‘:wq!’ and press Enter.

Export Bash

The first line, ‘#!/bin/bash‘ is called a shebang. Here by using this line, it specifies that the bash shell should be used. ‘export N1=5 ‘ and ‘export N2=7‘, these line uses the export command to set the environment variables, N1 and N2 with values 5 and 7 respectively. The last line ‘python3 example.py‘, invokes the Python 3 interpreter to run the script named example.py. You can replace example.py with the name of your Python script.

  • Now make the shell script executable using the following command:
chmod +x script.sh

This command grants execute permissions to the user. The script.sh is the name of the script. Here you can replace script.sh with your actual script.

  • The final step is to run the shell script. To run the shell script use the following command:
./script.sh
Export Op

The output is 12 which is an addition of 5 and 7.


Here we use export with Python in Linux with some steps. First, we create a Python script and then a shell script using a vi text editor. You can use nano editor as well. Then we make the shell script executable and finally, we run the shell script.


References:

https://pythonhint.com/post/2676691856440983/how-to-use-export-with-python-on-linux