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>