【问题标题】:Getting Environment Variables Of An User By Using Python使用 Python 获取用户的环境变量
【发布时间】:2021-11-07 12:51:10
【问题描述】:

我通过将export TEST1=VAL1 行添加到/home/username/.bashrc 文件中定义了一个环境变量。

在我的用户帐户的终端上使用printenv 命令时会列出此变量。但是使用以下python代码时没有列出:

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

使用 Python 列出这个变量的解决方案是什么。

OS: Debian-like Linux, Python: 3.9

在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()

图形界面文件:

<?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>

【问题讨论】:

标签: python python-3.x linux environment-variables subprocess


【解决方案1】:

使用 shell,登录和非登录执行是有区别的(参见man bash)。

executable 属性不接受参数,只是一个可执行文件。

那就试试吧:

#! /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()

输出不包含TEST1 变量。

使用 bash -l 选项:

#! /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()

输出包含TEST1 变量。

【讨论】:

  • 如果从终端运行 Python 代码,您编写的两个代码都列出了变量(.bashrc 文件中的变量)。但是,如果它们在 Python 应用程序/IDE 中运行,它们都不会列出变量。其他变量是列表。
  • 我已将代码更新为文件的输出。它工作正常。第一个代码,没有-l bash 选项,TEST1 不可见。第二个代码,带有-l bash 选项,TEST1 是可见的。我在桌面上为终端外的 Python 脚本创建了一个启动器
  • 我添加了一个示例 Python 代码和一个简单的 GUI 文件以供尝试。您编写的代码在我的代码中不起作用。这是什么原因?仅当使用终端调用此代码时,它才会列出变量。
  • 好的。我知道了。在我的~/.bash_profile 中,我有三行用于加载~/.bashrc,如下所示:if [ -f ~/.bashrc ]; then . ~/.bashrc; fi。你有那些台词吗?如果没有,请将您的 export ... 命令放入您的 ~/.bash_profile 文件中
  • Shell 登录说明太长,无法评论。请参阅man bash,“调用”部分(手册页:man7.org/linux/man-pages/man1/bash.1.html#INVOCATION)。另外,关于在创建新用户时创建默认文件,一般使用useradd 命令(手册页:man7.org/linux/man-pages/man8/useradd.8.html)。此命令也称为adduser。这取决于您的系统和发行版。
猜你喜欢
  • 2013-07-01
  • 2014-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 2017-04-11
  • 2018-09-03
相关资源
最近更新 更多