66public class UnrealEnginePython : ModuleRules
77{
88
9- private string pythonHome = "python35" ;
10- //private string pythonHome = "python27";
9+ // leave this string as empty for triggering auto-discovery of python installations...
10+ private string pythonHome = "" ;
11+ // otherwise specify the path of your python installation
12+ //private string pythonHome = "C:/Program Files/Python36";
1113
12- protected string PythonHome
14+
15+ private string [ ] windowsKnownPaths =
1316 {
14- get
15- {
16- return Path . GetFullPath ( Path . Combine ( ModuleDirectory , ".." , ".." , pythonHome ) ) ;
17- }
18- }
17+ "C:/Program Files/Python36" ,
18+ "C:/Program Files/Python35" ,
19+ "C:/Python27"
20+ } ;
1921
2022 public UnrealEnginePython ( TargetInfo Target )
2123 {
@@ -81,9 +83,17 @@ public UnrealEnginePython(TargetInfo Target)
8183
8284 if ( ( Target . Platform == UnrealTargetPlatform . Win64 ) || ( Target . Platform == UnrealTargetPlatform . Win32 ) )
8385 {
84- System . Console . WriteLine ( "Using Python at: " + PythonHome ) ;
85- PublicIncludePaths . Add ( PythonHome ) ;
86- PublicAdditionalLibraries . Add ( Path . Combine ( PythonHome , "libs" , string . Format ( "{0}.lib" , pythonHome ) ) ) ;
86+ if ( pythonHome == "" )
87+ {
88+ pythonHome = DiscoverPythonPath ( windowsKnownPaths ) ;
89+ if ( pythonHome == "" )
90+ {
91+ throw new System . Exception ( "Unable to find Python installation" ) ;
92+ }
93+ }
94+ System . Console . WriteLine ( "Using Python at: " + pythonHome ) ;
95+ PublicIncludePaths . Add ( pythonHome ) ;
96+ PublicAdditionalLibraries . Add ( GetWindowsPythonLibFile ( pythonHome ) ) ;
8797 }
8898 else if ( Target . Platform == UnrealTargetPlatform . Mac )
8999 {
@@ -94,7 +104,8 @@ public UnrealEnginePython(TargetInfo Target)
94104 PublicAdditionalLibraries . Add ( Path . Combine ( mac_python , "lib" , "libpython3.5m.dylib" ) ) ;
95105 Definitions . Add ( string . Format ( "UNREAL_ENGINE_PYTHON_ON_MAC=3" ) ) ;
96106 }
97- else if ( pythonHome == "python27" ) {
107+ else if ( pythonHome == "python27" )
108+ {
98109 string mac_python = "/Library/Frameworks/Python.framework/Versions/2.7/" ;
99110 PublicIncludePaths . Add ( Path . Combine ( mac_python , "include" ) ) ;
100111 PublicAdditionalLibraries . Add ( Path . Combine ( mac_python , "lib" , "libpython2.7.dylib" ) ) ;
@@ -108,12 +119,80 @@ public UnrealEnginePython(TargetInfo Target)
108119 PublicIncludePaths . Add ( "/usr/include/python3.5m" ) ;
109120 PublicAdditionalLibraries . Add ( "/usr/lib/python3.5/config-3.5m-x86_64-linux-gnu/libpython3.5.so" ) ;
110121 }
111- else if ( pythonHome == "python27" ) {
122+ else if ( pythonHome == "python27" )
123+ {
112124 PublicIncludePaths . Add ( "/usr/include/python2.7" ) ;
113125 PublicAdditionalLibraries . Add ( "/usr/lib/python2.7/config-x86_64-linux-gnu/libpython2.7.so" ) ;
114126 }
115127 Definitions . Add ( string . Format ( "UNREAL_ENGINE_PYTHON_ON_LINUX" ) ) ;
116128 }
117129
118130 }
131+
132+ private string DiscoverPythonPath ( string [ ] knownPaths )
133+ {
134+ foreach ( string path in knownPaths )
135+ {
136+ string headerFile = Path . Combine ( path , "include" , "Python.h" ) ;
137+ if ( File . Exists ( headerFile ) )
138+ {
139+ return path ;
140+ }
141+ // this is mainly useful for OSX
142+ headerFile = Path . Combine ( path , "Headers" , "Python.h" ) ;
143+ if ( File . Exists ( headerFile ) )
144+ {
145+ return path ;
146+ }
147+ }
148+ return "" ;
149+ }
150+
151+ private string GetWindowsPythonLibFile ( string basePath )
152+ {
153+ // just for usability, report if the pythonHome is not in the system path
154+ string [ ] allPaths = System . Environment . GetEnvironmentVariable ( "PATH" ) . Split ( ';' ) ;
155+ // this will transform the slashes in backslashes...
156+ string checkedPath = Path . GetFullPath ( basePath ) ;
157+ if ( checkedPath . EndsWith ( "\\ " ) )
158+ {
159+ checkedPath = checkedPath . Remove ( checkedPath . Length - 1 ) ;
160+ }
161+ bool found = false ;
162+ foreach ( string item in allPaths )
163+ {
164+ if ( item == checkedPath || item == checkedPath + "\\ " )
165+ {
166+ found = true ;
167+ break ;
168+ }
169+ }
170+ if ( ! found )
171+ {
172+ System . Console . WriteLine ( "[WARNING] Your Python installation is not in the system PATH environment variable, very probably the plugin will fail to load" ) ;
173+ }
174+ // first try with python3
175+ for ( int i = 9 ; i >= 0 ; i -- )
176+ {
177+ string fileName = string . Format ( "python3{0}.lib" , i ) ;
178+ string fullPath = Path . Combine ( basePath , "libs" , fileName ) ;
179+ if ( File . Exists ( fullPath ) )
180+ {
181+ return fullPath ;
182+ }
183+ }
184+
185+ // then python2
186+ for ( int i = 9 ; i >= 0 ; i -- )
187+ {
188+ string fileName = string . Format ( "python2{0}.lib" , i ) ;
189+ string fullPath = Path . Combine ( basePath , "libs" , fileName ) ;
190+ if ( File . Exists ( fullPath ) )
191+ {
192+ return fullPath ;
193+ }
194+ }
195+
196+ throw new System . Exception ( "Invalid Python installation, missing .lib files" ) ;
197+ }
119198}
0 commit comments