詳細
subprocessでgitコマンドを実行した際に表題のようなエラーがある為、困っています。
環境としまして、
デスクトップのWindowsPCと、VPSのubuntuを使用しております。
いずれもgitはインストール済みです。
まずWinPCで次のようなコードを作成しました。
■ディレクトリ構造
Project L Tool1 L main.py L sources L api L git_api.py L controll.py L Tool2 L Tool3
■今回実行する際の関連コード
・git_api.py
python
1def git(cmd): 2 proc = subprocess.Popen(cmd, stdin=-1, stdout=-1, stderr=-1) 3 out, err = proc.communicate() 4 5 if len(err) == 0: 6 print(out) 7 return out.strip() 8 else: 9 print(err.strip()) 10 11def pull(_brunch_name): 12 cmd = "git pull origin " + _brunch_name 13 git(cmd)
・controll.py
python
1git_api.pull("master")
以上のようなコードをgitのプライベートリポジトリにpushし、
VPSのubuntu上で「Project」をgitクローン後、「Tool1」ディレクトリの「python3 main.py」を実行すると、subprocessで「git pull origin master」を実行する際にエラーが発生します。
File "/home/ubuntu/Project/Tool1/sources/api/git_api.py", line 9, in git proc = subprocess.Popen(cmd, stdin=-1, stdout=-1, stderr=-1) File "/usr/local/lib/python3.8/subprocess.py", line 858, in __init__ self._execute_child(args, executable, preexec_fn, close_fds, File "/usr/local/lib/python3.8/subprocess.py", line 1704, in _execute_child raise child_exception_type(errno_num, err_msg, err_filename) FileNotFoundError: [Errno 2] No such file or directory: 'git pull origin master'
※一部省略
WinPC上で実行する分には問題ありません。
ubuntu上でgit pullを行う分にも問題がなく、pythonコードでsubprocessを通してgitコマンドが出来ないようです。
(コミットも同様のエラー)
エラー内容からgitファイルを見ることができないと思い、「Project」フォルダなどをPATHに追加してみても変化はなく、困っています。
どうか解決方法をご教示頂けませんでしょうか。
実現したいこと
エラーを修正してgitコマンドをpythonコードから実行したい
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/08/17 10:43