1

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:

  1. How can I run a python code using python2.7 in context of the issue
  2. Is there another way to run a python script written in 2.7 while using python3.x?

Thanks!!

2
  • 2
    Note: subprocess.run call a new program, so your question is: how to run a python2.7 code. (the calling program is irrelevant, you have the same problem also with other languages: you are calling python2.7 which it is not more installed) -- conda may help (and install locally, just for your user: you do not want to install a very obsolete and with a lot of security issues at system level. -- But I think the code will be never upgraded to python3, so docker will be necessary if not today, tomorrow. Commented Jul 3, 2024 at 11:58
  • As an aside, you want to avoid the shell=True; subprocess.run(["python2", f"{path}/install.py"], check=True). See also Actual meaning of shell=True in subprocess
    – tripleee
    Commented Jul 3, 2024 at 14:35

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.