简体   繁体   中英

Error trying to execute a Python2 binary from within a Python3 subprocess

I am trying to execute my Python3 script that calls a python binary with python2.7 lib file dependencies. The binary is pretty old, not by me and trying to bring it into Python3 is a big undertaking.

My system build is Win10 running Ubuntu WSL. I am executing my puthon3 code inside Ubuntu WSL, so essential I am executing straight off the Linux terminal.

By hand on the terminal, I can get the binary working fine by creating a virtual environment for python2 and execute the binary as such:

source ../martini_3.0.b.3.2/PythonCrisis/bin/activate
../martini_3.0.b.3.2/martinize -h
deactivate

I decided to put this in a shell (.sh) file (inc. #./bin/bash) and execute it from the Ubuntu terminal. No problem at all, However. I want to take this single step and execute it from within a Python3 script.

Unfortunately, I'm running into some problems. My Python 3 code looks like this:

import subprocess

result = subprocess.run(["/home/ubuntu/DE_NOVO_PROTEINS/SCRIPTS/martini_shell.sh"], capture_output=True, text=True)
print("stdout", result.stdout)
print("stderr", result.stderr)

I get the following:

stdout
stderr /home/ubuntu/DE_NOVO_PROTEINS/SCRIPTS/martini_shell.sh: 3: source: not found
Traceback (most recent call last):
  File "/home/ubuntu/miniconda3/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/home/ubuntu/miniconda3/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "../martini_3.0.b.3.2/martinize/__main__.py", line 3, in <module>
  File "<frozen zipimport>", line 259, in load_module
  File "../martini_3.0.b.3.2/martinize/martinize/__init__.py", line 37, in <module>
ModuleNotFoundError: No module named 'core'
DeprecationWarning: 'source deactivate' is deprecated. Use 'conda deactivate'.

Firstly, it looks like the shell script isn't being found. However, I suspect it's a little bit more complicated than that (given that the file does exist). Suggestions are very much appreciated.

EDIT Firstly, I want to stress that I am not trying to run a python2 interpreter. I am not executing python2 code. I'm executing a binary that depends on python2.7 libraries.

SOLVED I solved it. I only had to change #!/bin/sh to #!/bin/bash

As per my edits: I only had to change #!/bin/sh to #!/bin/bash

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM