顯示具有 Python 標籤的文章。 顯示所有文章
顯示具有 Python 標籤的文章。 顯示所有文章

2.12.2025

command not found: poe (問ChatGPT,沒試過)

 command not found: poe

ChatGPT 說:

如果你運行 poetry run poe lint 時出現 command not found: poe,可能有以下幾個原因:


1️⃣ 未安裝 Poe the Poet

poe 是 Poe the Poet 提供的命令,請先確認它是否安裝:

✅ 檢查是否已安裝:

sh
poetry show poethepoet

如果沒顯示 poethepoet,你需要安裝它。

📌 安裝 Poe the Poet:

sh
poetry add --dev poethepoet

然後嘗試再次運行 poetry run poe lint。


2️⃣ poethepoet 未設置為 Poetry 插件

即使你已經安裝了 poethepoet,Poetry 可能無法識別 poe 命令。嘗試顯式調用:

sh
poetry run python -m poethepoet lint

如果這樣可以運行,表示 poe 命令沒有自動註冊,你可以這樣解決:

sh
poetry self add "poetry-plugin"

然後重新運行 poetry run poe lint。


3️⃣ Poe 可能沒有添加到 pyproject.toml

請確保你的 pyproject.toml 文件中包含這個部分:

toml
[tool.poe]

如果沒有,你需要手動添加並配置任務,例如:

toml
[tool.poe.tasks.lint] cmd = "flake8 ."

然後再試試 poetry run poe lint。


4️⃣ Poetry 的虛擬環境未激活

如果你在系統的全局環境執行 poetry run poe lint,但 poethepoet 只安裝在 Poetry 虛擬環境內,那麼 poe 可能無法識別。

你可以手動進入 Poetry 的虛擬環境:

sh
poetry shell poe lint

如果這樣可行,說明 poetry run poe lint 可能不是必要的,你可以直接使用 poe lint。


🔍 最後檢查

如果還是不行,你可以試試這個:

sh
poetry run poe -l

看看是否有可用的 poe 任務。如果 poetry run poe 都無法執行,那麼問題很可能是 poethepoet 沒有安裝或沒被識別。

試試這些方法,看能不能解決你的問題!🚀

(open-gopro-tutorials-py3.12) pi@a5:~/Downloads/OpenGoPro-main/demos/python/tutorial$ poetry run python -m poethepoet --version
/home/pi/.cache/pypoetry/virtualenvs/open-gopro-tutorials-yv2s6YFQ-py3.12/bin/python: No module named poethepoet
 

Finally using "pip install poethepoet"

It seems works.

9.12.2024

Python3的py script呼叫其它的py script並執行

Ref: https://stackoverflow.com/questions/1186789/how-to-call-a-script-from-another-script

File test1.py:

print "test1.py"

File service.py:

import subprocess

subprocess.call("test1.py", shell=True)

The advantage to this method is that you don't have to edit an existing Python script to put all its code into a subroutine.

Documentation: Python 2, Python 3


(當呼叫的py script是跟使用者有輸入輸出的互動時,

subprocess.call可以使得跟直接執行另一個py一樣)


Ref: https://stackoverflow.com/questions/51928521/eoferror-when-starting-with-subproccess


EOFError when starting with subproccess


You are using subprocess.Popen which is a by default non blocking, piece of code, hence You program cursor moves rather than waiting for the input from the user. But what you need is a code which is blocking your program cursor. subprocess.call exactly does it. It would be waiting for the other command to execute completely.

You need to use subprocess.call to solve your problem. Just change your script2 file with this

import subprocess
p = subprocess.call(["python", "abca.py"])

You can read more about the difference between subprocess.Popen and subprocess.call in this answer. Which describes when to use which command and the difference between them



Important Note:
If your script (a.py) contains a continuous loop waiting for user input
(as shown in your code),
subprocess.run() will not be able to handle it properly
because it doesn't support interactive input.
If you need to execute a script that requires user input,
you might want to use subprocess.Popen() instead,
as it allows more control over the standard input/output.

# Run the Python script a.py
result = subprocess.run(['python3', 'a.py'], capture_output=True, text=True)

# Print the output from the script
print(result.stdout)
print(result.stderr)

# a.py:
while True: print("AAA") print("BBB") print("CCC") user_input = input("Enter something (or 'exit' to quit): ") if user_input.strip() == 'exit': break print(f"You entered: {user_input}")