Run Python code outside of virtual environment

603 Views Asked by At

I've trying to make an installer for my Linux Python program. As part of the installer, it checks for dependencies and python version. However, the Installer is obfuscated with pyarmor, and packed into a "onefile" binary using pyinstaller. As a result, the Python interpreter the installer is built with is also bundled with the app. So, when the program checks to see what version of Python is installed, it keeps returning the bundled interpreter's version, not the one installed on the user's computer. Is there anyway to run python code outside of the virtual environment to get the Python version installed on the user's computer?

I've tried various things such as generating a python file with the code that checks for the python version, and then placing a shebang at the top that points to the python binary installed on the user's computer but it always come back as permission denied. I even tried running the chmod command to give the python file executable permission, but I'm guessing my program can't make permission changes without the user doing it.

I'd really appreciate any tips or pointers. Thank you so much!

1

There are 1 best solutions below

3
jsibs On

Yes, there is a way to run Python code outside of the virtual environment and get the Python version installed on the user's computer. You can achieve this by using the subprocess module to execute a separate Python interpreter that is installed on the user's system. Here's how you can do it:

  1. Create a separate script to get the Python version: First, create a simple Python script, let's call it get_python_version.py, which will print the Python version.
# get_python_version.py

import sys

print(sys.version)
  1. Modify your main installer script: In your main installer script, after the checks for dependencies and the Python version of the bundled interpreter, you'll need to use subprocess to execute the get_python_version.py script with the system's Python interpreter and capture the output.
import subprocess

# Check for bundled interpreter version
bundled_python_version = "3.8"  # Replace this with the version of the bundled interpreter

# ... Perform checks for dependencies ...

# Now, execute the external Python script to get the system's Python version
try:
    output = subprocess.check_output(["python", "get_python_version.py"], universal_newlines=True)
    system_python_version = output.strip()
except subprocess.CalledProcessError as e:
    print("Error executing the external Python script:", e)
    sys.exit(1)

print("Bundled Python version:", bundled_python_version)
print("System Python version:", system_python_version)
  1. Packaging and distribution: When you package your installer using pyinstaller, make sure to exclude the get_python_version.py script from the package. This way, the script will be executed using the user's installed Python interpreter instead of the bundled one.

With this approach, your installer will be able to accurately determine the Python version installed on the user's system by running code outside the virtual environment and the bundled interpreter.