CPython Key Error importing pyRevit module

Here is a simple example of the subprocess in action:
The script that runs in Python 2.7 sends a message to the script that runs in python 3.8. The 3.8 accesses the arguments from the subprocess command via sys.argv[1, 2 ,3 etc] depending on how many arguments you pass through, and then prints a response

### PYTHON 2.7 SCRIPT ###
import subprocess

pathToScript = "path to python 3.8 script"
message = "My name is Python 2.7"
cmd = ['python', pathToScript, message]

# Create the subprocess startup info object This prevents the black terminal window from popping up when running hte subprocess
startup_info = subprocess.STARTUPINFO()
startup_info.dwFlags |= subprocess.STARTF_USESHOWWINDOW

# Run the command and capture the output
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, startupinfo=startup_info)

output, error = process.communicate()
print output
### Python 3.8 Script ###
import sys

message = sys.argv[1]

name = message.split("My name is ")[1]
response = "Hello " + name + ", My name is Python 3.8"
print(response)

The “output” variable that is printed from the 2.7 script will be whatever is printed from the 3.8 script. Hope this helps!

4 Likes