install_blender_python_modules icon indicating copy to clipboard operation
install_blender_python_modules copied to clipboard

variable referenced before assignment

Open pbaileyire opened this issue 3 years ago • 7 comments

On windows, running the script causes a "sys referenced before assignment" bug because a local variable sys is being referenced rather than the imported module sys.

Adding a 'global sys' statement to the beginning of the 'IsWindows()' branch appears to fix the issue.

pbaileyire avatar Dec 15 '22 21:12 pbaileyire

Line 27 should be removed, since that one makes sys local within python_exec()

Steven684 avatar Feb 07 '23 20:02 Steven684

same here bro, blender 3.6, 4.0, OS Windows, I comment on line 27 Python: Traceback (most recent call last): File "\Text", line 40, in installModule UnboundLocalError: local variable 'python_exe' referenced before assignment

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "\Text", line 49, in File "\Text", line 42, in installModule File "\Text", line 19, in python_exec UnboundLocalError: local variable 'sys' referenced before assignment

OmidGhotbi avatar Mar 27 '24 14:03 OmidGhotbi

does this help?

if isWindows():
        import sys
        return os.path.join(sys.prefix, 'bin', 'python.exe')
elif isMacOS():

luckychris avatar Mar 27 '24 17:03 luckychris

import sys import subprocess import os import platform import bpy

def isWindows(): return os.name == 'nt'

def isMacOS(): return os.name == 'posix' and platform.system() == "Darwin"

def isLinux(): return os.name == 'posix' and platform.system() == "Linux"

def python_exec(): if isWindows(): return os.path.join(sys.prefix, 'bin', 'python.exe') elif isMacOS(): try: # 2.92 and older path = bpy.app.binary_path_python except AttributeError: # 2.93 and later path = sys.executable return os.path.abspath(path) elif isLinux(): return os.path.join(sys.prefix, 'bin', 'python') else: print("sorry, still not implemented for ", os.name, " - ", platform.system)

def installModule(packageName): python_exe = python_exec() try: subprocess.check_call([python_exe, "-c", f"import {packageName}"]) except subprocess.CalledProcessError: # upgrade pip subprocess.call([python_exe, "-m", "ensurepip"]) subprocess.call([python_exe, "-m", "pip", "install", "--upgrade", "pip"]) # install required packages subprocess.call([python_exe, "-m", "pip", "install", packageName])

installModule("Cython")

OmidGhotbi avatar Mar 28 '24 00:03 OmidGhotbi

this is an update that will work with Windows too, I hope that helps.

OmidGhotbi avatar Mar 28 '24 00:03 OmidGhotbi

Tried the code suggested above like so, and did not work.

import sys
import subprocess
import os
import platform
import bpy

def isWindows():
    return os.name == 'nt'

def isMacOS():
    return os.name == 'posix' and platform.system() == "Darwin"

def isLinux():
    return os.name == 'posix' and platform.system() == "Linux"

def python_exec():
    if isWindows():
        return os.path.join(sys.prefix, 'bin', 'python.exe')
    elif isMacOS():
        try:
            # 2.92 and older
            path = bpy.app.binary_path_python
        except AttributeError:
            # 2.93 and later
            path = sys.executable
        return os.path.abspath(path)
    elif isLinux():
        return os.path.join(sys.prefix, 'bin', 'python')
    else:
        print("sorry, still not implemented for ", os.name, " - ", platform.system)

def installModule(packageName):
    python_exe = python_exec()
    try:
        subprocess.check_call([python_exe, "-c", f"import {packageName}"])
    except subprocess.CalledProcessError:
        # upgrade pip
        subprocess.call([python_exe, "-m", "ensurepip"])
        subprocess.call([python_exe, "-m", "pip", "install", "--upgrade", "pip"])
        # install required packages
        subprocess.call([python_exe, "-m", "pip", "install", packageName])

installModule("watchdog")

Obliveater95 avatar Apr 26 '24 07:04 Obliveater95

you need to fix the space, did you try that?

OmidGhotbi avatar Apr 26 '24 15:04 OmidGhotbi