Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python subprocess is running a different version of Python

I'm trying to create a Python script to execute other Python scripts, and it works on most scripts, but will fail when it encounters print('anything', end=''). This is because the subprocess is running 2.7, rather than 3.4. I cannot for the life of me figure out how to have the subprocess run 3.4.

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> import subprocess
>>> sys.version
'3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)]'
>>> results = subprocess.check_output('testprint.py', shell=True)
>>> results
b'2.7 (r27:82525, Jul  4 2010, 07:43:08) [MSC v.1500 64 bit (AMD64)]\r\n'

testprint.py is just this:

import sys
print(sys.version)

Edit: And of course I would realize a solution after posting the question. I modified the code to the following:

path = r'C:\Python34\python.exe'
results = subprocess.check_output([path, 'testprint.py'], shell=True)

Now I'm passing in the executable path the script will be run through. Still, I would love a more elegant and permanent solution.

like image 655
Sage Avatar asked Apr 08 '15 19:04

Sage


People also ask

Is subprocess built in Python?

The subprocess module present in Python(both 2. x and 3. x) is used to run new applications or programs through Python code by creating new processes.

How do I run a subprocess in Python?

To start a new process, or in other words, a new subprocess in Python, you need to use the Popen function call. It is possible to pass two parameters in the function call. The first parameter is the program you want to start, and the second is the file argument.

Are Subprocesses parallel in Python?

All sub processes are run in parallel. (To avoid this one has to wait explicitly for their completion.) They even can write into the log file at the same time, thus garbling the output. To avoid this you should let each process write into a different logfile and collect all outputs when all processes are finished.

What is subprocess in Python?

What is Subprocess in Python? Subprocess is the task of executing or running other programs in Python by creating a new process. We can use subprocess when running a code from Github or running a file storing code in any other programming language like C, C++, etc. We can also run those programs that we can run on the command line.

How to run external commands in Python subprocess?

Python Subprocess: Run External Commands 1 Processes and sub-processes. A program that is executed on a computer is also called a process. ... 2 Create a Python subprocess with subprocess.run. ... 3 Capture output of a Python subprocess. ... 4 Feeding data from standard input. ... 5 Running shell commands. ... 6 Caveats to look out for. ...

What is the difference between OS system and subprocess run?

os.system vs subprocess.run You might see code examples where os.system () is used to execute a command. The subprocess module is more powerful, though, and the official Python docs recommend using it over os.system (). Another issue with os.system is that it is more prone to command injection.

How do I Capture the output of a Python subprocess?

Capture output of a Python subprocess. If you run an external command, you’ll likely want to capture the output of that command. We can achieve this with the capture_output=True option: >>> import subprocess. >>> result = subprocess.run( ['python3', '--version'], capture_output=True, encoding='UTF-8') >>> result.


1 Answers

One solution is to do:

import sys
import subprocess

subprocess.call([sys.executable, 'testprint.py'])

sys.executable is the location of the python binary running the code executing. Note that for some reason shell=True launches the python binary without any arguments on sys.argv, so either omit that option, use a string or shlex.quote. This is effectively the same as your solution, but the executable position is not hard-coded.

like image 100
matsjoyce Avatar answered Oct 28 '22 20:10

matsjoyce