Force subprocess to use Python 3 [closed]

2024/7/8 7:30:18

So, I was trying to write a Python script that utilized subprocess to call another Python script in the same directory. This was all going well until an import statement within the second script was reached of a Python 3-only library, and since the script was opened using subprocess, which in turn uses Python 2, an ImportError occurred.

How can I force subprocess, specifically Popen(), to use Python 3 to open the script? There does not seem to be advice on this online.

Edit

While I always default to posting MWEs, for this question I believed it was unnecessary, but at any rate s soon as I proceeded to post it, it occurred to me to use 'python3' instead of just 'python',

stream = subprocess.Popen(['python3', 'app.py'])

and now the app works. What is strange is that I have only one version of Python installed by myself (3.7), and python redirects to python3, so it is strange I had to manually specify python3.

Answer

Here's a way to force a script to be run with Python3:

#! /usr/bin/python3import sys, subprocessif sys.version_info[:2] < (3, 0):# FORCE PYTHON3code = subprocess.check_call(['python3'] + sys.argv)raise SystemExit(code)print("Using Python v%d.%d" % sys.version_info[:2])

Example when run in Bash:

> python3 force_python3.py                                                                                                                         
Using Python v3.7> python2 force_python3.py                                                                                                                         
Using Python v3.7
https://en.xdnf.cn/q/120281.html

Related Q&A

Python: Im making a simple calculator for class. Whats wrong with this code? [duplicate]

This question already has answers here:How can I read inputs as numbers?(10 answers)Closed 7 months ago.My teacher requests me to make a calculator that can calculate a 15% tip on a submitted price. I…

FileNotFoundError Python Script

I am trying to run a python script, .py in the windows command prompt. I drag the script from my files into the command prompt window. I run it. Then, the script presents a prompt for me to enter the f…

Filtered product of lists without repetitions

I need an efficient solution to find all combinations without repetitions of a given lists. It has to work like this: l1 = [1, 2, 3] l2 = [3, 4, 5] combinations(l1, l2) = [(2, 4), (3, 4), (1, 5), (1, 4…

int object is not callable only appears randomly

Sometimes this code works just fine and runs through, but other times it throws the int object not callable error. I am not real sure as to why it is doing so.for ship in ships:vert_or_horz = randint(0…

scraping css values using scrapy framework

Is there a way to scrap css values while scraping using python scrapy framework or by using php scraping. any help will be appreaciated

Access dict via dict.key

I created a dict source = {livemode: False}. I thought its possible to access the livemode value via source.livemode. But it doesnt work. Is there a way to access it that way?As a not source[livemode]…

Function not returning anything

My viewdef login(request):c = {}c.update(csrf(request))return render_to_response(request, login.html, c)def auth_view(request):username = request.POST.get (username, )password = request.POST.get (passw…

My entry box always returns PY_VAR1 value!!though Im using the .get function

please take a look at my code, its really simple I need to take the value from the entry box and use it in my program and when pressing the add button I print it ,it keeps giving me this value PY_VAR1…

Arthimatic Quiz Not Accepting Correct Answers

I am attempting to make an arithmetic quiz, but have run into this issue: Even if I input the correct answer, it seems to ignore the correct answer code and go straight to the incorrect answer code. Ba…

Linux - Check if python script is running in screen and run if not [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 1…