0

I have defined an environment variable by adding export TEST1=VAL1 line into /home/username/.bashrc file.

This variable is listed when printenv command is used on terminal on my user account. But it is not listed when the following python code is used:

variables = subprocess.check_output("printenv", shell=True, executable='/bin/bash').decode()

What is the solution for listing this variable by using Python.

OS: Debian-like Linux, Python: 3.9

Example code for running terminal commands in python:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

builder = Gtk.Builder()
builder.add_from_file('test.ui')
window1 = builder.get_object('window1')

class Signals:
    def on_window1_destroy(self, widget):
        Gtk.main_quit()

builder.connect_signals(Signals())

import subprocess
variables = subprocess.check_output("/bin/bash -l -c printenv", shell=True).decode()
f = open("/tmp/env.txt","w")
f.write("%s" % (variables))
f.close()

window1.show_all()
Gtk.main()

GUI file:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <property name="default_width">300</property>
    <property name="default_height">300</property>
    <child>
      <placeholder/>
    </child>
    <child>
      <object class="GtkTreeView" id="treeview1">
        <property name="visible">True</property>
        <property name="can_focus">True</property>
        <child internal-child="selection">
          <object class="GtkTreeSelection"/>
        </child>
      </object>
    </child>
  </object>
</interface>
1

1 Answer 1

1

With shells, there is a difference between login and non login execution (see man bash).

executable attribute does not accept parameters, just an executable.

So try:

#! /usr/bin/python
import subprocess
variables = subprocess.check_output("/bin/bash -c printenv", shell=True).decode()
f = open("/tmp/env.txt","w")
f.write("%s" % (variables))
f.close()

Output do not contains TEST1 variable.

With bash -l option:

#! /usr/bin/python
import subprocess
variables = subprocess.check_output("/bin/bash -l -c printenv", shell=True).decode()
f = open("/tmp/env.txt","w")
f.write("%s" % (variables))
f.close()

Output contains TEST1 variable.

Sign up to request clarification or add additional context in comments.

5 Comments

Both of the codes you wrote list the variable (the variable in the .bashrc file) if Python code is run from terminal. But none of them list the variable if they are run in a Python application/IDE. Other variables are list.
I have updated my code with an output to a file. It's work fine. First code, without -l bash option, TEST1 is not visible. Second code, with -l bash option, TEST1 is visible. I have created a launcher on my desktop for the Python script outside a terminal
I have added an example Python code and a simple GUI file for trying. The code you wrote did not work in my code. What is the reason for this? It lists the variable only if this code is called with terminal.
Ok. I see. In my ~/.bash_profile, I have three lines for loading ~/.bashrc like this : if [ -f ~/.bashrc ]; then . ~/.bashrc; fi. Do you have those lines ? If not, put your export ... command in your ~/.bash_profile file
Shell login explanation is too long for a comment. See man bash, "Invocation" section (man page : man7.org/linux/man-pages/man1/bash.1.html#INVOCATION). Also, about the creation of default files during creation of new user, generally with useradd command (man page : man7.org/linux/man-pages/man8/useradd.8.html). This command is also known as adduser. It's depends on your system, your distribution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.