Skip to content

Commit bb3ae80

Browse files
committed
Fix Python path detection: initialize variable before loop
1 parent 3d9a724 commit bb3ae80

2 files changed

Lines changed: 18 additions & 14 deletions

File tree

src/py.erl

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -799,27 +799,29 @@ get_python_executable() ->
799799
import sys, os
800800
# When embedded, sys.executable points to the embedding app
801801
# Use sys.prefix to find the actual Python installation
802+
_py_exe = 'python3' # default fallback
802803
if sys.platform == 'win32':
803-
python = os.path.join(sys.prefix, 'python.exe')
804+
path = os.path.join(sys.prefix, 'python.exe')
805+
if os.path.isfile(path):
806+
_py_exe = path
804807
else:
805808
ver = f'python{sys.version_info.major}.{sys.version_info.minor}'
806809
# Try common locations
807810
for path in [
808811
os.path.join(sys.prefix, 'bin', ver),
809812
os.path.join(sys.prefix, 'bin', 'python3'),
810813
os.path.join(sys.prefix, 'bin', 'python'),
811-
sys.executable # fallback
812814
]:
813-
if os.path.isfile(path) and os.access(path, os.X_OK):
814-
python = path
815-
break
816-
else:
817-
python = 'python3'
818-
python
815+
try:
816+
if os.path.isfile(path) and os.access(path, os.X_OK):
817+
_py_exe = path
818+
break
819+
except:
820+
pass
819821
">>,
820822
case exec(Code) of
821823
ok ->
822-
case eval(<<"python">>) of
824+
case eval(<<"_py_exe">>) of
823825
{ok, Path} when is_binary(Path) -> binary_to_list(Path);
824826
_ -> "python3"
825827
end;

test/py_venv_SUITE.erl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,19 @@ init_per_group(_Group, Config) ->
6464
%% Note: sys.executable returns beam.smp when embedded, so we find the actual Python
6565
Code = <<"
6666
import sys, os
67+
_python_path = 'python3' # default
6768
ver = f'python{sys.version_info.major}.{sys.version_info.minor}'
6869
for path in [
6970
os.path.join(sys.prefix, 'bin', ver),
7071
os.path.join(sys.prefix, 'bin', 'python3'),
7172
os.path.join(sys.prefix, 'bin', 'python'),
7273
]:
73-
if os.path.isfile(path) and os.access(path, os.X_OK):
74-
_python_path = path
75-
break
76-
else:
77-
_python_path = 'python3'
74+
try:
75+
if os.path.isfile(path) and os.access(path, os.X_OK):
76+
_python_path = path
77+
break
78+
except:
79+
pass
7880
">>,
7981
ok = py:exec(Code),
8082
{ok, PythonPath} = py:eval(<<"_python_path">>),

0 commit comments

Comments
 (0)