Answers.Body: <p><strong>The Problem was p.communicate(), which kills the subprocess. The interaction with the engine works well with the following code:</strong></p>
  
  <pre><code>def put(command, inf_list, tmp_time):
  
      engine.stdin.write(command+'\n')
      engine.stdin.flush()
      time.sleep(tmp_time)
  
      if command &lt;&gt; "quit":
  
          engine.stdin.write('isready\n')     
          engine.stdin.flush()
  
          while True:
              text = engine.stdout.readline().strip()
  
              if text == 'readyok':
                  break
  
              elif text !='':
                  inf_list.append(text)
  
  
  inf_list = []
  put('uci', inf_list, 0)
  put('setoption name Hash value 8192', inf_list, 0)
  put('ucinewgame', inf_list, 0)
  put('position fen rq3r2/2pbppk1/3p1np1/1N5p/1n1PP2P/2N2P2/PP1Q2P1/R3KB1R w KQ - 3 15', inf_list, 0)
  put('go movetime=3000', inf_list, 4)
  put('stop', inf_list, 0)
  put('quit', inf_list, 0)
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-08T13:25:58.287Z
Answers.LastActivityDate: 2020-03-08T13:25:58.287Z
Answers.UserDisplayName: Michael
Body: <p>I try to access the Stockfish engine via Python, but only get bestmove = a2a3. 
  If I enter the same commands into the MAC-terminal, the engine calculates the correct move. 
  Can anyone help me, please ? </p>
  
  <p>My Python code is: </p>
  
  <pre><code>p = subprocess.Popen('/Applications/TigerJython/LuChess/Stockfish/stockfish-11-mac/Mac/stockfish-11-64', stdin=subprocess.PIPE,
                       stdout=subprocess.PIPE,
                       stderr=subprocess.STDOUT)
  
  p.stdin.write("uci\n")
  p.stdin.write("ucinewgame\n")
  p.stdin.write("position startpos moves e2e4 e7e5\n")
  p.stdin.write("go infinite\n")
  p.stdin.flush()
  print(p.communicate())
  </code></pre>
  
  <p>The result is shown below:</p>
  
  <pre><code>('Stockfish 11 64 by T. Romstad, M. Costalba, J. Kiiski, G. Linscott
  id name Stockfish 11 64\nid author T. Romstad, M. Costalba, J. Kiiski, G. Linscott
  
  option name Debug Log File type string default
  option name Contempt type spin default 24 min -100 max 100
  ...
  uciok
  bestmove a2a3
  None
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-03-04T21:32:28.793Z
Comments.Text: The best and simplest option would be to use the [python-chess library](https://python-chess.readthedocs.io/en/latest/). For engine communication see: https://python-chess.readthedocs.io/en/latest/engine.html
Comments.UserDisplayName: Ellie
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-03-06T07:59:57.970Z
Comments.Text: Thanks, Phonon, for your hint. python-chess unfortunately only supports version 3.6+ and my application relies on Tiger-Jython, which runs under version 2.7. I could solve the problem in the meantime and have posted the code accordingly above.
Comments.UserDisplayName: Michael
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-03-06T11:30:28.653Z
Comments.Text: You're welcome and great that you solved it on your own! But generally, it might be a good idea to already consider transitioning to Python 3 anyhow as [Python 2 has become deprecated](https://www.python.org/doc/sunset-python-2/). Please feel free to post your solution as an answer (instead of an edit to the question) as it will be useful for future readers and gives the post closure. [SE even encourages answering own questions](https://meta.stackexchange.com/questions/17845/etiquette-for-answering-your-own-question).
Comments.UserDisplayName: Ellie
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-03-08T13:30:10.740Z
Comments.Text: Thanks again, Phonon, for your comments. I am so glad to have found this forum, where you can find an incredible amount of information.
Comments.UserDisplayName: Michael
Tags: <stockfish><programming><python>
Title: Communication with Stockfish engine via Python
ThreadText: # Communication with Stockfish engine via Python
  
  (Asked by: username_0 on 2020-03-04)
  
  I try to access the Stockfish engine via Python, but only get bestmove = a2a3.
  If I enter the same commands into the MAC-terminal, the engine calculates the correct move.
  Can anyone help me, please ?
  
  My Python code is:
  
  ```
  p = subprocess.Popen('/Applications/TigerJython/LuChess/Stockfish/stockfish-11-mac/Mac/stockfish-11-64', stdin=subprocess.PIPE,
                       stdout=subprocess.PIPE,
                       stderr=subprocess.STDOUT)
  
  p.stdin.write("uci\n")
  p.stdin.write("ucinewgame\n")
  p.stdin.write("position startpos moves e2e4 e7e5\n")
  p.stdin.write("go infinite\n")
  p.stdin.flush()
  print(p.communicate())
  
  ```
  
  The result is shown below:
  
  ```
  ('Stockfish 11 64 by T. Romstad, M. Costalba, J. Kiiski, G. Linscott
  id name Stockfish 11 64\nid author T. Romstad, M. Costalba, J. Kiiski, G. Linscott
  
  option name Debug Log File type string default
  option name Contempt type spin default 24 min -100 max 100
  ...
  uciok
  bestmove a2a3
  None
  
  ```
  
  **Tags:** stockfish, programming, python
  
  > **Comments:**
  > **username_1**: The best and simplest option would be to use the [python-chess library](https://python-chess.readthedocs.io/en/latest/). For engine communication see: https://python-chess.readthedocs.io/en/latest/engine.html  
  > **username_0**: Thanks, Phonon, for your hint. python-chess unfortunately only supports version 3.6+ and my application relies on Tiger-Jython, which runs under version 2.7. I could solve the problem in the meantime and have posted the code accordingly above.  
  > **username_1**: You're welcome and great that you solved it on your own! But generally, it might be a good idea to already consider transitioning to Python 3 anyhow as [Python 2 has become deprecated](https://www.python.org/doc/sunset-python-2/). Please feel free to post your solution as an answer (instead of an edit to the question) as it will be useful for future readers and gives the post closure. [SE even encourages answering own questions](https://meta.stackexchange.com/questions/17845/etiquette-for-answering-your-own-question).  
  > **username_0**: Thanks again, Phonon, for your comments. I am so glad to have found this forum, where you can find an incredible amount of information.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2020-03-08. Score: 2
  
  **The Problem was p.communicate(), which kills the subprocess. The interaction with the engine works well with the following code:**
  
  ```
  def put(command, inf_list, tmp_time):
  
      engine.stdin.write(command+'\n')
      engine.stdin.flush()
      time.sleep(tmp_time)
  
      if command <> "quit":
  
          engine.stdin.write('isready\n')     
          engine.stdin.flush()
  
          while True:
              text = engine.stdout.readline().strip()
  
              if text == 'readyok':
                  break
  
              elif text !='':
                  inf_list.append(text)
  
  inf_list = []
  put('uci', inf_list, 0)
  put('setoption name Hash value 8192', inf_list, 0)
  put('ucinewgame', inf_list, 0)
  put('position fen rq3r2/2pbppk1/3p1np1/1N5p/1n1PP2P/2N2P2/PP1Q2P1/R3KB1R w KQ - 3 15', inf_list, 0)
  put('go movetime=3000', inf_list, 4)
  put('stop', inf_list, 0)
  put('quit', inf_list, 0)
  
  ```
  
  
  ---
  
  