Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

python - Creating pipenv in subprocess

I'm trying to use subprocess to open a pipenv virtualenv in python. I know I am in the correct directory, but pipenv keeps opening in the parent directory. Each time I have physically deleted the parent virtualenv by doing, rm -r $home/.local/share/virtualenvs/..... I verify that they are deleted. Here is the code I am using:

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

How do I fix this issue?

question from:https://stackoverflow.com/questions/65854445/creating-pipenv-in-subprocess

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

subprocess.run is based on subprocess.Popen, and passes most of its argument to it. Now when running help(subprocess.Popen):

class Popen(builtins.object)
 |  Popen(args, [...] cwd=None [...])
                      ^^^^^^^^

You can set the working directory.

Also, you should use lists to pass commands to execute. IE:

subprocess.run(['pipenv', 'install', django'])

This reduces errors. You can use shlex.split to do it automatically.

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.
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...