I'm trying to extend the HelloWorld example here:
git://github.com/compnerd/swift-build-examples /SourceCache/swift-build-examples
by simply adding this line at the top of HelloWorldCore.swift:
import Python
building gives this error:
\SourceCache\swift-build-examples\HelloWorld-CMake\Source\HelloWorldCore\HelloWorldCore.swift:2:8: error: no such module 'Python'
import Python
^
ninja: build stopped: subcommand failed.
Without the "import Python" HelloWorld builds fine:
[2/5] Linking C shared library bin\CCore.dll
Creating library lib\CCore.lib and object lib\CCore.exp
[3/5] Linking Swift shared library bin\HelloWorldCore.dll
Creating library lib\HelloWorldCore.lib and object lib\HelloWorldCore.exp
[4/5] Linking Swift executable bin\TestHelloWorldCore.exe
Creating library bin\TestHelloWorldCore.lib and object bin\TestHelloWorldCore.exp
[5/5] Linking Swift executable bin\HelloWorld.exe
Creating library bin\HelloWorld.lib and object bin\HelloWorld.exp
So the general setup and configuration seems to be all there. Not sure what to do to make Python module visible to the compiler/linker. Python is definitely installed as I'm able to type "python" in the same command prompt from which I'm attempting to build, and that works fine by starting the python interpreter.
Wondered what was a good way to make it possible to import Python into Swift 5.3 running on windows. Is Swift “VS2019(Google)” a good choice, or would it be easier to start off by using a different swift installer which might have python more readily integrated? Or perhaps some other suggestions - thanks!
I tried to write up all the steps, which I then followed myself when setting up a new VM (window 10) from scratch. It looks like the steps below are complete and should pretty much just work. There are 30 steps in total. I had to break into several replies as I'm only allowed up to 5 links per post being as I'm new on this forum.
Swift – Hello World! Installing Swift
Install Visual Studio 2019 using MSDN subscription:
Include C++ so that “x64 Native Tools” command prompt is available.
Also include Python which is needed for integration with Front Arena.
Install CMake (Download | CMake) and choose option to add PATH to all users on machine
Get the “hello world!” sample source code by running the following from command prompt:
git clone git://github.com/compnerd/swift-build-examples /SourceCache/swift-build-examples
Close the command prompt and then reopen the “x64 Native Tools” command prompt again
Run the following in this new command prompt:
set SDKROOT=%SystemDrive%\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk
copy "%SDKROOT%\usr\share\ucrt.modulemap" "%UniversalCRTSdkDir%\Include%UCRTVersion%\ucrt\module.modulemap"
copy "%SDKROOT%\usr\share\visualc.modulemap" "%VCToolsInstallDir%\include\module.modulemap"
copy "%SDKROOT%\usr\share\visualc.apinotes" "%VCToolsInstallDir%\include\visualc.apinotes"
copy "%SDKROOT%\usr\share\winsdk.modulemap" "%UniversalCRTSdkDir%\Include%UCRTVersion%\um\module.modulemap"
Setup build parameters:
set SDKROOT=%SystemDrive%/Library/Developer/Platforms/Windows.platform/Developer/SDKs/Windows.sdk
set SWIFTFLAGS=-sdk %SDKROOT% -I %SDKROOT%/usr/lib/swift -L %SDKROOT%/usr/lib/swift/windows
Run the following to configure cmake:
"%ProgramFiles%/CMake/bin/cmake.exe" ^
-B /BinaryCache/HelloWorld ^
-D BUILD_SHARED_LIBS=YES ^
-D CMAKE_BUILD_TYPE=Release ^
-D CMAKE_Swift_FLAGS="%SWIFTFLAGS%" ^
-G Ninja ^
-S /SourceCache/swift-build-examples/HelloWorld-CMake
The output of the above configuration commands should look like this:
-- The Swift compiler identification is Apple 5.3
-- Check for working Swift compiler: C:/Library/Developer/Toolchains/unknown-Asserts-development.xctoolchain/usr/bin/swiftc.exe
-- Check for working Swift compiler: C:/Library/Developer/Toolchains/unknown-Asserts-development.xctoolchain/usr/bin/swiftc.exe - works
-- Configuring done
-- Generating done
-- Build files have been written to: /BinaryCache/HelloWorld
Now build the hello world exe file:
cmake --build /BinaryCache/HelloWorld
The output of the build command should look like this:
[2/5] Linking C shared library bin\CCore.dll
Creating library lib\CCore.lib and object lib\CCore.exp
[3/5] Linking Swift shared library bin\HelloWorldCore.dll
Creating library lib\HelloWorldCore.lib and object lib\HelloWorldCore.exp
[4/5] Linking Swift executable bin\TestHelloWorldCore.exe
Creating library bin\TestHelloWorldCore.lib and object bin\TestHelloWorldCore.exp
[5/5] Linking Swift executable bin\HelloWorld.exe
Creating library bin\HelloWorld.lib and object bin\HelloWorld.exp
To verify, navigate to the newly built exe location:
cd C:\BinaryCache\HelloWorld\bin
Typing HelloWorld.exe should give this output:
Hello World!
Integrating Python
21. For convenience, you may wish to install Notepad++ (Downloads | Notepad++) which does have support for Swift syntax highlighting.
22. Edit this file using Notepad++ or similar:
C:\SourceCache\swift-build-examples\HelloWorld-CMake\Source\HelloWorld\HelloWorld.swift
23. The new content of the file is changed near the top so that the whole file looks like this:
import HelloWorldCore
Save the file, and then delete or rename this folder to force a clean rebuild
C:\BinaryCache\HelloWorld
PATH environment variable will need to be updated to include the path where Python was installed by Visual Studio 2019. If python was installed through other means (e.g. by installing Anaconda) you might be able to skip the next step and go straight to step 27
In Windows start menu, search for “control panel” and launch
In control panel, search for “environment” and click on “Edit system environment variables”
In “System Properties” the “Advanced” tab is selected, click on “Environment Variables…” button
On the lower pane “System variables” select “Path” and then click on the Edit button
Click on “New” and paste the path where Visual Studio 2019 installed Python to, which is normally:
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64
Click OK on all open dialog boxes
To verify, reopen a new “x64 Native Tools” command prompt
Type “path” in this new command prompt then hit return to confirm that Python is included in the search paths
Or, alternatively type “python” to confirm that the interpreter starts (ctrl+z then enter to exit)
Repeat steps 13 to 18 above to rebuild and link etc
Typing HelloWorld.exe should give this output:
3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)]
Hello World!
The first line shows the version of Python which has been integrated – Congratulations!
Thanks for writing these up! I wonder if it makes sense to do a few things here. First of all, I think it's valuable to split these up into two separate categories:
For the Swift for TensorFlow specific instructions (21-30), perhaps you would be willing to help improve the documentation in the TensorFlow Swift APIs repository?
Is there any kind of roadmap for simplifying the steps and reducing their amount? I have no relevant experience with the Windows ecosystem, but maybe Swift for Windows could be packaged and distributed with something like the recently announced Microsoft's winget package manager?
This is where I think that it would be possible to improve the installer to automate this, but no one has stepped up to do this work, and my Windows knowledge is based on what I've figured out while doing this work :). If someone knows how to handle the required elevation to automate the SDK modifications, that would be a welcome enhancement to the installer.
Steps 11-20: build project
About half the steps here are this is expected output and the other half splits up the configure into 3 steps, build into 1 step, and execution into 1 step.
Steps 21-30: specifics to editing the code, setting up a particular dependency which has additional specifics to system setup (e.g. python).
This is probably the area that really needs improvements. However, some of this is specific to development on Windows and the realities of using Python (such as editing the PATH).
Sure, I'd be happy to help however I can. This might be a basic question, bearing in mind that I'm a total Swift beginner - would one expect "import Python" to work from plain Swift, as opposed to Swift TensorFlow? If so, then it might make sense to address that aspect in "GettingStartedWindows.md" as you mention.
I say this because the notes in GettingStartedWindows do mention including Python support when installing Visual Studio say that this is "needed for Python integration". But by following all the steps in GettingStartedWindows it is not clear how to work with Python with the resulting setup.
To me, making use of Swift TensorFlow was in way of workaround to get Python integration working with Swift, but there might be a more direct way of doing so. If that's the case, that would basically be the real answer to the original question I'd posted here, and if someone is able to provide that real answer I'd be more than happy to update GettingStartedWindows.md accordingly if that helps.
No, import Python would not be expected to work in Swift by default. The python interoperability requires PythonKit which is a separate project.
The python integration it is referring to is specific to LLDB. The debugger depends on the python interpreter. That should be rewritten as I originally wrote the instructions bearing Swift for TensorFlow in mind. Things have progressed since that point. The Windows port tends to move faster than the documentation, reminders like this are often the catalyst for improving the documentation.
I’m glad that you found Swift for TensorFlow useful :). There are many other really interesting features in Swift for TensorFlow. For the use of Python interoperability, that is not specific to Swift for TensorFlow, it is just is that the dependency is included in the build. Building the module yourself would allow you to have Python interoperability with Swift.
I think that a general section in the document for adding dependencies seems reasonable in the examples repository. Unfortunately, SwiftPM isn’t yet ready on Windows and so the instructions are slightly different (normally, you would just add the dependency to the manifest). Here you need to build the dependency and add the paths to the build invocation.