[Python] How to replace sh? Error: No module named 'fcntl'

Discussão em 'Python' iniciado por Stack, Maio 21, 2025.

  1. Stack

    Stack Membro Participativo

    I am running code on Windows and I have this line

    pip = sh.Command(os.path.join(sys.exec_prefix,"bin","pip"))


    got

    No module named 'fcntl'


    I tried to replace it this way

    def pip(*args):
    result = subprocess.run(
    [sys.executable, "-m", "pip"] + list(args),
    capture_output=True,
    text=True
    )
    if result.returncode != 0:
    raise Exception(f"Pip command failed: {result.stderr}")
    return result.stdout.strip()


    I got error

    File "C:\Applics\Python 3.9\lib\subprocess.py", line 505, in run
    with Popen(*popenargs, **kwargs) as process:
    File "C:\Applics\Python 3.9\lib\subprocess.py", line 951, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
    File "C:\Applics\Python 3.9\lib\subprocess.py", line 1360, in _execute_child
    args = list2cmdline(args)
    File "C:\Applics\Python 3.9\lib\subprocess.py", line 565, in list2cmdline
    for arg in map(os.fsdecode, seq):
    File "C:\Applics\Python 3.9\lib\os.py", line 822, in fsdecode
    filename = fspath(filename)


    when I run

    pip("install",tuple("--upgrade"))


    I tried this on Ubuntu and it works fine.

    def run_pip(*args):
    """Run pip with the given arguments"""
    cmd = [sys.executable, "-m", "pip"] + list(args)
    return subprocess.run(cmd, capture_output=True, text=True)


    result = run_pip("list")
    print(result.stdout)


    it looks like that I do not understand Windows dir structure.

    What else could work?

    Continue reading...

Compartilhe esta Página