-
Notifications
You must be signed in to change notification settings - Fork 14
/
requirements.py
37 lines (35 loc) · 1.25 KB
/
requirements.py
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
"""
this part checks external packages and install if not exist
"""
requirements = ['pygame', 'numpy']
import sys
# find and install any missing external packages
def check():
# find missing packages
from importlib.util import find_spec
missing = [requirement for requirement in requirements if not(find_spec(requirement))]
if not missing:
return
# install missing packages
sys.stdout.write("Installing" + ','.join(missing) + ".\n")
# redirect out to nothing so no installing messages will be seen.
sys_stdout = sys.stdout
sys_stderr = sys.stderr
sys.stdout = None
sys.stderr = None
from pip.commands.install import InstallCommand
from pip.status_codes import SUCCESS
cmd = InstallCommand()
for requirement in requirements:
try:
if cmd.main([requirement]) is not SUCCESS:
sys_stderr.write("Can not install " + requirement + ", program aborts.\n")
sys.exit()
# this might occur because of redirection of stdout and stderr
except AttributeError:
pass
# direct out back to normal
sys.stdout = sys_stdout
sys.stderr = sys_stderr
sys.stdout.write("All packages are installed, starting game...")
sys.stdout.flush()