0
votes

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

1
The shell script is working fine. But the error is saying that the traceback is coming from miniconda3, not Python2 - OneCricketeer
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
@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
Or as shown here, execute the Python2 binary directly, not via a virtualenv stackoverflow.com/questions/27863832/… - OneCricketeer

1 Answers

0
votes

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