• <i id='YEH5j'><tr id='YEH5j'><dt id='YEH5j'><q id='YEH5j'><span id='YEH5j'><b id='YEH5j'><form id='YEH5j'><ins id='YEH5j'></ins><ul id='YEH5j'></ul><sub id='YEH5j'></sub></form><legend id='YEH5j'></legend><bdo id='YEH5j'><pre id='YEH5j'><center id='YEH5j'></center></pre></bdo></b><th id='YEH5j'></th></span></q></dt></tr></i><div id='YEH5j'><tfoot id='YEH5j'></tfoot><dl id='YEH5j'><fieldset id='YEH5j'></fieldset></dl></div>

    <tfoot id='YEH5j'></tfoot>

    1. <small id='YEH5j'></small><noframes id='YEH5j'>

      <legend id='YEH5j'><style id='YEH5j'><dir id='YEH5j'><q id='YEH5j'></q></dir></style></legend>
        • <bdo id='YEH5j'></bdo><ul id='YEH5j'></ul>
      1. 在 Python 中执行 BASH 命令——在同一进程中

        时间:2023-09-10
        • <bdo id='mS9kN'></bdo><ul id='mS9kN'></ul>

          <small id='mS9kN'></small><noframes id='mS9kN'>

            <legend id='mS9kN'><style id='mS9kN'><dir id='mS9kN'><q id='mS9kN'></q></dir></style></legend>

            1. <tfoot id='mS9kN'></tfoot>
                  <tbody id='mS9kN'></tbody>
                <i id='mS9kN'><tr id='mS9kN'><dt id='mS9kN'><q id='mS9kN'><span id='mS9kN'><b id='mS9kN'><form id='mS9kN'><ins id='mS9kN'></ins><ul id='mS9kN'></ul><sub id='mS9kN'></sub></form><legend id='mS9kN'></legend><bdo id='mS9kN'><pre id='mS9kN'><center id='mS9kN'></center></pre></bdo></b><th id='mS9kN'></th></span></q></dt></tr></i><div id='mS9kN'><tfoot id='mS9kN'></tfoot><dl id='mS9kN'><fieldset id='mS9kN'></fieldset></dl></div>

                  本文介绍了在 Python 中执行 BASH 命令——在同一进程中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我需要执行命令./home/db2v95/sqllib/db2profile 在 Python 2.6 中 import ibm_db_dbi 之前.

                  I need to execute the command . /home/db2v95/sqllib/db2profile before I can import ibm_db_dbi in Python 2.6.

                  在我进入 Python 之前执行它是可行的:

                  Executing it before I enter Python works:

                  baldurb@gigur:~$ . /home/db2v95/sqllib/db2profile
                  baldurb@gigur:~$ python
                  Python 2.6.4 (r264:75706, Dec  7 2009, 18:45:15) 
                  [GCC 4.4.1] on linux2
                  Type "help", "copyright", "credits" or "license" for more information.
                  >>> import ibm_db_dbi
                  >>> 
                  

                  但在 Python 中使用 os.system("./home/db2v95/sqllib/db2profile")subprocess.Popen(["./home/db2v95/sqllib/db2profile"]) 导致错误.我做错了什么?

                  but executing it in Python using os.system(". /home/db2v95/sqllib/db2profile") or subprocess.Popen([". /home/db2v95/sqllib/db2profile"]) results in an error. What am I doing wrong?

                  这是我收到的错误:

                  > Traceback (most recent call last):  
                  > File "<file>.py", line 8, in
                  > <module>
                  >     subprocess.Popen([". /home/db2v95/sqllib/db2profile"])  
                  > File
                  > "/usr/lib/python2.6/subprocess.py",
                  > line 621, in __init__
                  >     errread, errwrite)   File "/usr/lib/python2.6/subprocess.py",
                  > line 1126, in _execute_child
                  >     raise child_exception OSError: [Errno 2] No such file or directory
                  

                  推荐答案

                  你调用的是'.'外壳命令.该命令的意思是在当前进程中执行这个 shell 文件".您不能在 Python 进程中执行 shell 文件,因为 Python 不是 shell 脚本解释器.

                  You are calling a '.' shell command. This command means 'execute this shell file in current process'. You cannot execute shell file in Python process as Python is not a shell script interpreter.

                  /home/b2v95/sqllib/db2profile 可能设置了一些 shell 环境变量.如果您使用 system() 函数读取它,那么变量将仅在执行的 shell 中更改,并且在调用该 shell 的进程(您的脚本)中不可见.

                  The /home/b2v95/sqllib/db2profile probably sets some shell environment variables. If you read it using system() function, then the variables will be only changed in the shell executed and will not be visible in the process calling that shell (your script).

                  你只能在启动你的python脚本之前加载这个文件——你可以制作一个shell包装脚本来执行./home/b2v95/sqllib/db2profile并执行你的python脚本.

                  You can only load this file before starting your python script – you could make a shell wrapper script which would do . /home/b2v95/sqllib/db2profile and execute your python script.

                  另一种方法是查看 db2profile 包含的内容.如果这只是 NAME=value 行,您可以在您的 python 脚本中解析它并使用获得的数据更新 os.environ.如果脚本做了更多的事情(比如调用其他东西来获取值),你可以在 Python 中重新实现整个脚本.

                  Other way would be to see what the db2profile contains. If that are only NAME=value lines, you could parse it in your python script and update os.environ with the data obtained. If script does something more (like calling something else to obtain the values) you can reimplement whole script in Python.

                  更新一个想法:将脚本读入python,将它(使用Popen)管道到shell,脚本后将env命令写入同一个shell并读取输出.这样,您将获得 shell 中定义的所有变量.现在您可以读取变量了.

                  Update An idea: read the script into python, pipe it (using Popen) to the shell, after the script write env command to the same shell and read the output. This way you will get all the variables defined in the shell. Now you can read the variables.

                  类似这样的:

                  shell = subprocess.Popen(["sh"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
                  script = open("/home/db2v95/sqllib/db2profile", "r").read()
                  shell.stdin.write(script + "
                  ")
                  shell.stdin.write("env
                  ")
                  shell.stdin.close()
                  for line in shell.stdout:
                      name, value = line.strip().split("=", 1)
                      os.environ[name] = value
                  

                  这篇关于在 Python 中执行 BASH 命令——在同一进程中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:SQLAlchemy Columns 结果处理 下一篇:在 Python 中将 SQL 与 IBM_DB 连接器一起使用

                  相关文章

                  最新文章

                • <small id='caYA3'></small><noframes id='caYA3'>

                • <tfoot id='caYA3'></tfoot>

                        <bdo id='caYA3'></bdo><ul id='caYA3'></ul>
                      <i id='caYA3'><tr id='caYA3'><dt id='caYA3'><q id='caYA3'><span id='caYA3'><b id='caYA3'><form id='caYA3'><ins id='caYA3'></ins><ul id='caYA3'></ul><sub id='caYA3'></sub></form><legend id='caYA3'></legend><bdo id='caYA3'><pre id='caYA3'><center id='caYA3'></center></pre></bdo></b><th id='caYA3'></th></span></q></dt></tr></i><div id='caYA3'><tfoot id='caYA3'></tfoot><dl id='caYA3'><fieldset id='caYA3'></fieldset></dl></div>
                    1. <legend id='caYA3'><style id='caYA3'><dir id='caYA3'><q id='caYA3'></q></dir></style></legend>