variable referenced before assignment
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.
Line 27 should be removed, since that one makes sys local within python_exec()
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
does this help?
if isWindows():
import sys
return os.path.join(sys.prefix, 'bin', 'python.exe')
elif isMacOS():
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")
this is an update that will work with Windows too, I hope that helps.
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")
you need to fix the space, did you try that?