![](/img/trans.png)
[英]with open FileNotFoundError: [Errno 2] No such file or directory:
[英]subprocess FileNotFoundError: [Errno 2] No such file or directory when open external program in PATH
我正在使用 Mac 並嘗試使用 Python 中的子進程模塊打開一個外部程序(Visual Studio Code)。 code
二進制文件位於usr/local/bin
中,它位於PATH
變量中。 當我使用python3.8 main.py
在 shell 上運行它時,我的代碼運行良好。 但是,當我使用 IDLE 運行程序時,Python 不斷給我
Traceback (most recent call last):
File "/Users/mike/Desktop/main.py", line 33, in <module>
subprocess.Popen(['code'])
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/subprocess.py", line 1702, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'code'
我的代碼盡可能簡單:
import subprocess
subprocess.Popen(['code'])
有誰知道是什么導致了問題以及我該如何解決?
感謝大家。
默認情況下,如文檔中所述, Popen
不使用 shell (因此$PATH
變量不可用於子進程)。
所以你需要以太:
subprocess.Popen(['/usr/local/bin/code'])
或者
subprocess.Popen('code', shell=True)
如果有任何擴展名,您可以在打開文件時嘗試使用它。 我們也可以嘗試提供一次絕對路徑。
嘗試使用 Visual Studio 代碼的完整路徑,而不僅僅是“代碼”這個詞
subprocess.Popen(['usr/local/code'])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.