0

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

Anthony Nash
  • 834
  • 1
  • 9
  • 26
  • The shell script is working fine. But the error is saying that the traceback is coming from miniconda3, not Python2 – OneCricketeer May 02 '21 at 11:44
  • Maybe overkill, but are you able to use Docker to create a Python2 container to run the script? I don't think using `source` within a Python3 subprocess is going to work – OneCricketeer May 02 '21 at 11:51
  • @OneCricketeer - thanks for the comment. In answer to your question, I have no idea, I've never heard of Docker before. Does it work in WSL Ubuntu? – Anthony Nash May 02 '21 at 11:54
  • Yes it does https://docs.docker.com/docker-for-windows/wsl/ – OneCricketeer May 02 '21 at 11:54
  • Or as shown here, execute the Python2 binary directly, not via a virtualenv https://stackoverflow.com/questions/27863832/calling-python-2-script-from-python-3 – OneCricketeer May 02 '21 at 11:57
  • Forgive my cynicism, but as you said yourself it seems very overkill when this just concerns executing a binary file (with Py2.7 lib file dependencies) from within Python3. Nonetheless, thanks, I'll look into it. – Anthony Nash May 02 '21 at 11:59
  • @OneCricketeer just seen your other comment with the link to python2, I'll look through it, thanks. However, before anyone gets over existed and brands this question a duplicate, I want to see whether suggestions here factor in that the file I am trying to execute is a binary and not a script. – Anthony Nash May 02 '21 at 12:03
  • @OneCricketeer sadly, the comments from that particular thread didn't fix my problem; I did learn some new Python-related technicalities though. Swings and roundabouts. – Anthony Nash May 02 '21 at 12:36

1 Answers1

0

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

Anthony Nash
  • 834
  • 1
  • 9
  • 26