iT邦幫忙

0

Python subprocess 的問題

  • 分享至 

  • xImage

請教問題,我已經使用 uv 安裝了 Python 3.13.1,如下:

> uv python find 3.13.1
C:\Users\meebo\AppData\Roaming\uv\python\cpython-3.13.1-windows-x86_64-none\python.exe

並且在 test_anyio 資料夾下建立倚賴剛剛裝版本的虛擬環境:

> cd test_anyio
uv python find 3.13.1
> C:\Users\meebo\code\python\test_anyio\.venv\Scripts\python.exe

在這個資料夾下有以下程式檔:

> cat parent.py
import subprocess
import sys

print(sys.prefix)
print(sys.base_prefix)
print('Parent:', sys.executable)

subprocess.run(
    ["python", "child.py"],
)

其中透過 subprocess 執行的 child.py 檔如下:

> cat child.py
import sys

print('Child:', sys.executable)

我的環境中並沒有任何全域可執行的 python:

> python
python: The term 'python' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

但是當我執行 parent.py 時,看到的輸入如下:

> uv run parent.py
C:\Users\meebo\code\python\test_anyio\.venv
C:\Users\meebo\AppData\Roaming\uv\python\cpython-3.13.1-windows-x86_64-none
Parent: C:\Users\meebo\code\python\test_anyio\.venv\Scripts\python.exe
Child: C:\Users\meebo\AppData\Roaming\uv\python\cpython-3.13.1-windows-x86_64-none\python.exe

其中 child.py 使用的並不是虛擬環境中的 python.exe,而是虛會得到這樣的原始安裝的 python.exe。

請問為什麼會這樣?以及 subprocess 又是如何找到原始安裝的 python.exe?

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答