In my company there is a script: install.py that is written in python2.7.
The issue
I'm writing a script to run this script from my own python script which I've written in python3.11.
In the beginning I tried to do this:
subprocess.run(f"python2.7 {path}/install.py", check=True, shell=True)
But I get an error indicating that script is ran in python3.11 and not 2.7
print "Out>", serialized[:300] + (["...", ""][len(serialized) < 80])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
In python3.11 this is indeed an error but in python 2.7 this line should not throw an error
To add on that in the install.py there is this line:
#!/usr/bin/env python2.7
When checking python version I get these outputs:
me$ python2.7 --version
Python 3.11.4
me$ python3 --version
Python 3.11.4
As I found out while looking for answer, there is no way to install just python2.7 anymore using
sudo apt install python2.7
Workaround
I found a way to overcome this issue in my own machine using the asdf repository (https://asdf-vm.com/)
I just added this line:
subprocess.run(f"echo python 2.7.18 > {new_path}/.tool-versions"
Which adds a local config file that runs the local python script in that directory with python2.7.18.
But this solution is hard to maintain if I want to run this script from other machines or pass it to other team members or other employees in the company, as the asdf repository is not native to Linux (our company standard OS)
I also thought of using a docker container but it feels like an overkill.
The question
In the bottom line I have one issue and one question:
- How can I run a python code using python2.7 in context of the issue
- Is there another way to run a python script written in 2.7 while using python3.x?
Thanks!!
shell=True
;subprocess.run(["python2", f"{path}/install.py"], check=True)
. See also Actual meaning ofshell=True
insubprocess