-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonLauncher.bat
More file actions
196 lines (170 loc) · 5.2 KB
/
Copy pathPythonLauncher.bat
File metadata and controls
196 lines (170 loc) · 5.2 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
@echo off
setlocal enabledelayedexpansion
:::------------------------------------Configuration------------------------------------:::
::Set the python version to use. Will be ignored if pythondir is not py or py.exe.
set pythonversion=3.11
::Used to configure the python exe to use. Useful when using portable python versions. Use %cd%\path\to\python.exe to use relative directories.
set pythondir=py
::Used to define the title of the cmd that will execute the python file.
set windowname=Python
::This script can auto detect what initial files to run edit this list to add custom initial files or change the order(if two files exist the first will be runed).
set initialfiles="run.py" "main.py" "app.py"
::Set if a venv will be created before runing the python file.
set usevenv=1
::Used to define the name of the venv that will be created and used.
set venvname=pyvenv
::Set if a requirements.txt file will be installed when creating the venv. Packages will not be installed or updated if the venv alredy exists.
set installrequirementsfile=1
::Used to define the requirements file that will be installed.
set requirementsfile=requirements.txt
::Toggle if the cmd that runs the python file should start minimized.
set minimizedcmd=0
::Toggle if the cmd window should be closed when the python file execution has ended.
set autoclosecmd=0
::Used to toggle if the arguments passed to this file will be passed to the python file. Will disable running a python file when dragging it over this file if enabled.
set passarguments=0
::Set if the user will be alerted and taken to the python download page if python is not installed. Program will abort if python is not installed when set to 0.
set alertifpynotinstalled=1
:::-----------------------------------------Code----------------------------------------:::
rem Check if python is installed.
%pythondir% -V
if %ERRORLEVEL% neq 0 (
rem Python is NOT installed.
set "ispyinstalled=0"
cls
if "!alertifpynotinstalled!"=="1" (
echo No python version was found!
echo Opening download page.
start https://www.python.org/downloads/
) else (
exit
)
pause >nul
exit
) else (
set "ispyinstalled=1"
)
cls
rem Decide what type of cli launcher is being used.
for %%A in ("%pythondir%") do (
set "fileName=%%~nxA"
)
if exist "%pythondir%" (
if /I "!fileName!"=="py" (
echo File is py
set "usepylancher=1"
) else if /I "!fileName!"=="py.exe" (
echo File is py.exe
set "usepylancher=1"
) else (
echo File name is different: %pythondir%
set "usepylancher=0"
)
) else (
if "!ispyinstalled!"=="1" (
echo File does not exist, but Python is installed.
if /I "!fileName!"=="py" (
echo File is py
set "usepylancher=1"
) else if /I "!fileName!"=="py.exe" (
echo File is py.exe
set "usepylancher=1"
) else (
echo File name is different: %pythondir%
echo !fileName!
set "usepylancher=0"
)
) else (
if "!alertifpynotinstalled!"=="1" (
echo File does not exist.
set "usepylancher=0"
) else (
exit
)
)
)
cls
rem Define what to do.
if "!minimizedcmd!"=="1" (
set "min=/min"
if "!autoclosecmd!"=="1" (
set "minexit=& exit"
) else (
set "minexit="
)
) else (
set "min="
if "!autoclosecmd!"=="1" (
set "minexit=& exit"
) else (
set "minexit="
)
)
if "!usevenv!"=="1" (
set "venvcmd=%cd%\%venvname%\Scripts\activate"
) else (
set "venvcmd=cls"
)
if "!usepylancher!"=="1" (
set "pycmd=-%pythonversion%"
) else (
set "pycmd="
)
if !passarguments!==1 (
rem Run a flie with arguments.
set foundFile=
for %%f in (%initialfiles%) do (
if exist %%~f (
set foundFile=%%~f
set "command=start %min% "%windowname%" cmd /k ^"call !venvcmd! ^& %pythondir% %pycmd% !foundFile! %* %minexit%^""
goto :runFile
)
)
set command=start %min% "%windowname%" cmd /k "!venvcmd! & %pythondir% %pycmd%"
goto :runFile
) else (
if "%~1"=="" (
rem No argument provided.
rem Check if any initial file exists.
set foundFile=
for %%f in (%initialfiles%) do (
if exist %%~f (
set foundFile=%%~f
set command=start %min% "%windowname%" cmd /k "!venvcmd! & %pythondir% %pycmd% !foundFile! %minexit%"
goto :runFile
)
)
set command=start %min% "%windowname%" cmd /k "!venvcmd! & %pythondir% %pycmd%"
goto :runFile
) else (
rem An argument has been provided. Run that file.
set command=start %min% "%windowname%" cmd /k "!venvcmd! & %pythondir% %pycmd% %1 %minexit%"
goto :runFile
)
)
:runFile
rem Run command.
call :createvenv
%command%
goto :eof
:createvenv
rem Create a virtual environment.
if "!usevenv!"=="1" (
if not exist "%venvname%" (
echo Creating Virtual environment...
%pythondir% %pycmd% -m venv "%venvname%"
if "!installrequirementsfile!"=="1" (
if exist "%requirementsfile%" (
%cd%\%venvname%\Scripts\activate
%pythondir% %pycmd% -m pip install -r %requirementsfile%
) else (
echo No requirements to install
)
) else (
echo No requirements to install
)
)
) else (
echo VENV is disabled
)
goto :eof