在子进程中创建 pipenv
Creating pipenv in subprocess
我正在尝试使用子进程在 python 中打开一个 pipenv virtualenv。我知道我在正确的目录中,但 pipenv 一直在父目录中打开。每次我通过 rm -r $home/.local/share/virtualenvs/....
物理删除父 virtualenv。我确认它们已被删除。这是我正在使用的代码:
import os
import subprocess
def test():
os.chdir('/home/.../example')
subprocess.run('ls')
# works correctly, in proper directory
subprocess.run('pipenv install django')
# doesn't work correctly as it installs in parent directory
我该如何解决这个问题?
subprocess.run
基于 subprocess.Popen
,并将其大部分参数传递给它。现在当 运行 help(subprocess.Popen)
:
class Popen(builtins.object)
| Popen(args, [...] cwd=None [...])
^^^^^^^^
您可以设置工作目录。
此外,您应该使用列表来传递要执行的命令。即:
subprocess.run(['pipenv', 'install', django'])
这样可以减少错误。您可以使用 shlex.split
自动完成。
Help on function run in module subprocess:
run(*popenargs, input=None, capture_output=False, timeout=None, check=False, **kwargs)
Run command with arguments and return a CompletedProcess instance.
The returned instance will have attributes args, returncode, stdout and
stderr. By default, stdout and stderr are not captured, and those attributes
will be None. Pass stdout=PIPE and/or stderr=PIPE in order to capture them.
If check is True and the exit code was non-zero, it raises a
CalledProcessError. The CalledProcessError object will have the return code
in the returncode attribute, and output & stderr attributes if those streams
were captured.
If timeout is given, and the process takes too long, a TimeoutExpired
exception will be raised.
There is an optional argument "input", allowing you to
pass bytes or a string to the subprocess's stdin. If you use this argument
you may not also use the Popen constructor's "stdin" argument, as
it will be used internally.
By default, all communication is in bytes, and therefore any "input" should
be bytes, and the stdout and stderr will be bytes. If in text mode, any
"input" should be a string, and stdout and stderr will be strings decoded
according to locale encoding, or by "encoding" if set. Text mode is
triggered by setting any of text, encoding, errors or universal_newlines.
The other arguments are the same as for the Popen constructor.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
我正在尝试使用子进程在 python 中打开一个 pipenv virtualenv。我知道我在正确的目录中,但 pipenv 一直在父目录中打开。每次我通过 rm -r $home/.local/share/virtualenvs/....
物理删除父 virtualenv。我确认它们已被删除。这是我正在使用的代码:
import os
import subprocess
def test():
os.chdir('/home/.../example')
subprocess.run('ls')
# works correctly, in proper directory
subprocess.run('pipenv install django')
# doesn't work correctly as it installs in parent directory
我该如何解决这个问题?
subprocess.run
基于 subprocess.Popen
,并将其大部分参数传递给它。现在当 运行 help(subprocess.Popen)
:
class Popen(builtins.object)
| Popen(args, [...] cwd=None [...])
^^^^^^^^
您可以设置工作目录。
此外,您应该使用列表来传递要执行的命令。即:
subprocess.run(['pipenv', 'install', django'])
这样可以减少错误。您可以使用 shlex.split
自动完成。
Help on function run in module subprocess:
run(*popenargs, input=None, capture_output=False, timeout=None, check=False, **kwargs)
Run command with arguments and return a CompletedProcess instance.
The returned instance will have attributes args, returncode, stdout and
stderr. By default, stdout and stderr are not captured, and those attributes
will be None. Pass stdout=PIPE and/or stderr=PIPE in order to capture them.
If check is True and the exit code was non-zero, it raises a
CalledProcessError. The CalledProcessError object will have the return code
in the returncode attribute, and output & stderr attributes if those streams
were captured.
If timeout is given, and the process takes too long, a TimeoutExpired
exception will be raised.
There is an optional argument "input", allowing you to
pass bytes or a string to the subprocess's stdin. If you use this argument
you may not also use the Popen constructor's "stdin" argument, as
it will be used internally.
By default, all communication is in bytes, and therefore any "input" should
be bytes, and the stdout and stderr will be bytes. If in text mode, any
"input" should be a string, and stdout and stderr will be strings decoded
according to locale encoding, or by "encoding" if set. Text mode is
triggered by setting any of text, encoding, errors or universal_newlines.
The other arguments are the same as for the Popen constructor.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^