-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwo_numpy.py
More file actions
72 lines (63 loc) · 2.59 KB
/
two_numpy.py
File metadata and controls
72 lines (63 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
This script checks if the numpy package is installed and if its version matches the one specified
in the environment variable NUMPY_VERSION.
In python 3.13, there are two python interpreters: python.exe and python3.13t.exe.
Different interpreters have different numpy versions to installed and import.
This script is used to ensure that the correct numpy version is used in the correct interpreter.
If set the environment variable NUMPY_VERSION, it will check the version of numpy.
If the numpy hasn't been installed, it will install the numpy package.
If the numpy is not suitable for the current interpreter, it will reinstall the numpy package.
Note: please set the official wersion as the environment variable NUMPY_VERSION.
For example: 2.2.0, 2.2.1, etc.
Don't set the version like 2.2.0rc1, 2.2.0dev0, 2.2.0beta, etc.
"""
import os
import sys
import subprocess
from importlib.metadata import version
from packaging.version import parse
def is_valid_version(version_str):
try:
parsed = parse(version_str)
return not parsed.is_prerelease and not parsed.is_devrelease
except:
return False
if 'NUMPY_VERSION' not in os.environ:
version_information = ''
else:
if not is_valid_version(os.environ['NUMPY_VERSION']):
raise ValueError('''Wrong version information: 'NUMPY_VERSION' must be the official version.
For example: 2.2.0, 2.2.1, etc.
Don't set the version like 2.2.0rc1, 2.2.0dev0, 2.2.0beta, etc.''')
version_information = '=='+os.environ['NUMPY_VERSION']
def get_version():
try:
return version("numpy")
except Exception:
return ''
def setup_np():
setup_numpy = subprocess.Popen([
sys.executable,
'-m',
'pip',
'install',
'--force-reinstall',
f'numpy{version_information}'
], stderr=subprocess.PIPE, stdout=subprocess.PIPE, text=True)
_, stderr = setup_numpy.communicate()
return (setup_numpy.returncode, stderr)
if not get_version():
if (result := setup_np())[0] != 0:
raise ImportError(f"cannot setup numpy: \n{result[1]}")
import numpy as np
elif version_information and version_information != get_version():
if (result := setup_np())[0] != 0:
raise ImportError(f"cannot setup numpy: \n{result[1]}")
import numpy as np
else:
try:
import numpy as np
except Exception:
if (result := setup_np())[0] != 0:
raise ImportError(f"cannot setup numpy: \n{result[1]}") from None
import numpy as np