Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit daa24ba

Browse files
authored
Merge pull request #1477 from stonebig/master
improve utils.py code with grok
2 parents 795170c + 2462ea4 commit daa24ba

File tree

1 file changed

+22
-27
lines changed

1 file changed

+22
-27
lines changed

winpython/utils.py

+22-27
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,36 @@
1111
"""
1212

1313
import os
14-
from pathlib import Path
14+
import sys
15+
import stat
16+
import shutil
17+
import locale
18+
import tempfile
1519
import subprocess
20+
import configparser as cp
21+
from pathlib import Path
1622
import re
1723
import tarfile
1824
import zipfile
19-
import tempfile
20-
import shutil
2125
import atexit
22-
import sys
23-
import stat
24-
import locale
2526
import io
26-
import configparser as cp
27-
28-
# Local imports
2927
import winreg
3028

31-
def get_python_executable(path = None):
32-
"""return the python executable"""
33-
my_path = sys.executable if path == None else path # default = current one
34-
my_path = my_path if Path(my_path).is_dir() else str(Path(my_path).parent)
35-
exec_py = str(Path(my_path) / 'python.exe')
36-
exec_pypy = str(Path(my_path) / 'pypy3.exe') # PyPy !
37-
# PyPy >=7.3.6 3.8 aligns to python.exe and Lib\site-packages
38-
python_executable = exec_py if Path(exec_py).is_file() else exec_pypy
39-
return python_executable
40-
41-
def get_site_packages_path(path = None):
42-
"""return the python site-packages"""
43-
my_path = sys.executable if path == None else path # default = current one
44-
my_path = my_path if Path(my_path).is_dir() else str(Path(my_path).parent)
45-
site_py = str(Path(my_path) / 'Lib' / 'site-packages')
46-
site_pypy = str(Path(my_path) / 'site-packages') # PyPy !!
47-
site_packages_path = site_pypy if Path(site_pypy).is_dir() else site_py
48-
return site_packages_path
29+
def get_python_executable(path=None):
30+
"""Return the path to the Python executable."""
31+
python_path = sys.executable if path is None else path
32+
base_dir = Path(python_path).parent if not Path(python_path).is_dir() else Path(python_path)
33+
python_exe = base_dir / 'python.exe'
34+
pypy_exe = base_dir / 'pypy3.exe' # For PyPy
35+
return str(python_exe if python_exe.is_file() else pypy_exe)
36+
37+
def get_site_packages_path(path=None):
38+
"""Return the path to the Python site-packages directory."""
39+
python_path = sys.executable if path is None else path
40+
base_dir = Path(python_path).parent if not Path(python_path).is_dir() else Path(python_path)
41+
site_packages = base_dir / 'Lib' / 'site-packages'
42+
pypy_site_packages = base_dir / 'site-packages' # For PyPy
43+
return str(pypy_site_packages if pypy_site_packages.is_dir() else site_packages)
4944

5045
def onerror(function, path, excinfo):
5146
"""Error handler for `shutil.rmtree`.

0 commit comments

Comments
 (0)