# Import Tensorflow failed in sw

**URL:** <https://forums.swift.org/t/import-tensorflow-failed-in-sw/30811>\
**Category:** Compiler\
**Tags:** tensorflow\
**Created:** [November 15, 2019, 8:08am UTC](https://forums.swift.org/t/import-tensorflow-failed-in-sw/30811 "2019-11-15T08:08:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tkclimb](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tkclimb/32/9615_2.png) [@tkclimb](https://forums.swift.org/u/tkclimb)\
**Post date:** [November 15, 2019, 8:08am UTC](https://forums.swift.org/t/import-tensorflow-failed-in-sw/30811/1 "2019-11-15T08:08:54Z")

</div>

Hi, I'm a compiler developer who recently have interested in swift for tensorflow (S4TF).  
This is amazing project, thanks for the development team.

Anyway, currently I've setup the development environment including swift-compiler itself with Tensorflow module and debug flag by following the instruction in [GitHub - apple/swift at tensorflow](https://github.com/apple/swift/tree/tensorflow#building-swift-for-tensorflow)

I ran

`swift/utils/build-script --enable-tensorflow --release-debuginfo`

Then I called swift REPL and tried to import Tensorflow but it falied with the following error.

```swift
➜ cd {swift-source directory}
➜ ./build/Ninja-RelWithDebInfoAssert/swift-macosx-x86_64/bin/swift
(swift) import Tensorflow

<REPL Input>:1:8: error: cannot load module 'TensorFlow' as 'Tensorflow'
import Tensorflow

```

I don't even know how to use swift-compiler itself but I'd like to use it as pure-compiler from shell with VSCode not Xcode if I could because I'm intended to use it for normal machine learning and compiler development, not application dev for apple products.  
Here, I have questions

- do S4TF developers mandatorily use Xcode or not?
- how to debug swift-compiler itself or any document about it?
- how to import Tensorflow module or to build the compiler can do?

---

<div class="post-metadata">

**Author:** ![Victor\_Guerra](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/victor_guerra/32/782_2.png) [@Victor\_Guerra](https://forums.swift.org/u/Victor_Guerra)\
**Post date:** [November 15, 2019, 12:50pm UTC](https://forums.swift.org/t/import-tensorflow-failed-in-sw/30811/2 "2019-11-15T12:50:47Z")

</div>

Hello @tkclimb,

Welcome to the Swift forums 🙂

> [@tkclimb](#):
>
> I ran
> 
> `swift/utils/build-script --enable-tensorflow --release-debuginfo`

Since this PR [#27596](https://github.com/apple/swift/pull/27596) was merged, you need to pass the flag `--enable-experimental-differentiable-programming` to the `build-script` so that you enable AutoDiff capabilities in the compiler + stdlib. Could you please try to run the `build-script` with this flag?

> [@tkclimb](#):
>
> do S4TF developers mandatorily use Xcode or not?

I use mainly Xcode but I've read that people use as well vim or VS Code. You can find [here](https://github.com/apple/swift/tree/master/utils/vim) integration scripts for vim. For developing with swift using VS Code check out this [extention](https://github.com/vknabel/vscode-swift-development-environment).

> [@tkclimb](#):
>
> how to debug swift-compiler itself or any document about it?

You can use `lldb` to debug the compiler. There is a a wrapper to launch `lldb` including some helpers relevant for the swift compiler that you can find within your build directory : `swift-macosx-x86_64/utils/lldb/lldb-with-tools` and you can use it like this:

```bash
./lldb-with-tools -- ./swift /tmp/test.swift

```

Another option for you to play around with S4TF is to download a Development Snapshot, for instance the one from [Nov. 11th, 2019](https://storage.googleapis.com/swift-tensorflow/mac/swift-tensorflow-DEVELOPMENT-2019-11-11-a-osx.pkg). Install it and from the shell you can do:

```bash
$> export TOOLCHAINS=com.google.swift.20191111
$> swift
Welcome to Swift version 5.1.1-dev (Swift e242a8825f).
Type :help for assistance.
  1> import TensorFlow
PlatformPOSIX::DoLoadImage
path: libswiftTensorFlow.dylib
PlatformPOSIX::DoLoadImage
path: @rpath/libswiftTensorFlow.dylib
PlatformPOSIX::DoLoadImage
path: libswiftDarwin.dylib

```

---

<div class="post-metadata">

**Author:** ![tkclimb](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tkclimb/32/9615_2.png) [@tkclimb](https://forums.swift.org/u/tkclimb)\
**Post date:** [November 16, 2019, 1:34am UTC](https://forums.swift.org/t/import-tensorflow-failed-in-sw/30811/3 "2019-11-16T01:34:22Z")

</div>

@Victor_Guerra Thank you, it seems great information.  
I'll try your suggestion. Hopefully I want to do some contribution to this amazing compiler stack.

---

<div class="post-metadata">

**Author:** ![dan-zheng](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dan-zheng/32/2534_2.png) [@dan-zheng](https://forums.swift.org/u/dan-zheng)\
**Post date:** [November 16, 2019, 2:50am UTC](https://forums.swift.org/t/import-tensorflow-failed-in-sw/30811/4 "2019-11-16T02:50:44Z")

</div>

> [@tkclimb](#):
>
> Then I called swift REPL and tried to import Tensorflow but it falied with the following error.
> 
> ```swift
> ➜ cd {swift-source directory}
> ➜ ./build/Ninja-RelWithDebInfoAssert/swift-macosx-x86_64/bin/swift
> (swift) import Tensorflow
> 
> <REPL Input>:1:8: error: cannot load module 'TensorFlow' as 'Tensorflow'
> import Tensorflow
> 
> ```

`error: cannot load module 'TensorFlow' as 'Tensorflow'` occurs because `TensorFlow` has been misspelled as `Tensorflow`.

Could you please try `import TensorFlow` instead?

---

<div class="post-metadata">

**Author:** ![dan-zheng](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dan-zheng/32/2534_2.png) [@dan-zheng](https://forums.swift.org/u/dan-zheng)\
**Post date:** [November 16, 2019, 2:51am UTC](https://forums.swift.org/t/import-tensorflow-failed-in-sw/30811/5 "2019-11-16T02:51:13Z")

</div>

> - do S4TF developers mandatorily use Xcode or not?

Swift for TensorFlow has [macOS and Ubuntu toolchains](https://github.com/tensorflow/swift/blob/master/Installation.md) that work just like toolchains from [Swift.org - Download Swift](http://swift.org/download). Xcode is not mandatory!

In fact, there are some known issues regarding Xcode and Swift for TensorFlow toolchains ([mailing list discussion](https://groups.google.com/a/tensorflow.org/d/msg/swift/plU_x_7WOCk/kq1Da8YWAgAJ)). It's not clear whether these issues can be fixed via changes to Xcode's open source dependencies.

If you'd like to start a project, SwiftPM works more reliably:

```bash
$ mkdir Example
$ cd Example
$ swift package init
# Edit sources to import and use TensorFlow.
$ vim Sources/Example/Example.swift
$ swift test

# macOS: generate .xcodeproj to open project in Xcode.
$ swift package generate-xcodeproj
$ open Example.xcodeproj

```

> - how to debug swift-compiler itself or any document about it?

There's a great [Debugging the Swift Compiler](https://github.com/apple/swift/blob/tensorflow/docs/DebuggingTheCompiler.rst) document with useful information.

> - how to import Tensorflow module or to build the compiler can do?

It seems like you followed the right steps! Try `import TensorFlow` instead.

---

<div class="post-metadata">

**Author:** ![tkclimb](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tkclimb/32/9615_2.png) [@tkclimb](https://forums.swift.org/u/tkclimb)\
**Post date:** [November 16, 2019, 3:09am UTC](https://forums.swift.org/t/import-tensorflow-failed-in-sw/30811/6 "2019-11-16T03:09:31Z")

</div>

@dan-zheng  
Oh, I see... This is a silly mistake...  
Now, I confirmed importing TF correctly has done. Many Thanks!

How about what is the difference of putting "--enable-experimental-differentiable-programming" that previous answer mentioned?

---

<div class="post-metadata">

**Author:** ![dan-zheng](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dan-zheng/32/2534_2.png) [@dan-zheng](https://forums.swift.org/u/dan-zheng)\
**Post date:** [November 16, 2019, 4:30pm UTC](https://forums.swift.org/t/import-tensorflow-failed-in-sw/30811/7 "2019-11-16T16:30:34Z")

</div>

> [@tkclimb](#):
>
> How about what is the difference of putting "--enable-experimental-differentiable-programming" that previous answer mentioned?

The `build-script` flag `--enable-experimental-differentiable-programming` enables standard library additions for differentiable programming, including the [`Differentiable` protocol](https://github.com/apple/swift/blob/master/docs/DifferentiableProgramming.md#the-differentiable-protocol-1).

However, passing it explicitly isn't necessary, as the default value is true on both [`tensorflow` branch](https://github.com/apple/swift/blob/14d290a0c565ecc01f8ba1c5d727fb7750bb6938/utils/build_swift/driver_arguments.py#L1058) and [`master` branch](https://github.com/apple/swift/blob/9498287043e21ee1e0e5c715220e83d1d3610695/utils/build_swift/driver_arguments.py#L1047) (to ensure testing by CI).

---

<div class="post-metadata">

**Author:** ![Victor\_Guerra](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/victor_guerra/32/782_2.png) [@Victor\_Guerra](https://forums.swift.org/u/Victor_Guerra)\
**Post date:** [November 17, 2019, 9:36am UTC](https://forums.swift.org/t/import-tensorflow-failed-in-sw/30811/8 "2019-11-17T09:36:39Z")

</div>

> [@dan-zheng](#):
>
> However, passing it explicitly isn't necessary, as the default value is true on both [`tensorflow` branch](https://github.com/apple/swift/blob/14d290a0c565ecc01f8ba1c5d727fb7750bb6938/utils/build_swift/driver_arguments.py#L1058) and [`master` branch](https://github.com/apple/swift/blob/9498287043e21ee1e0e5c715220e83d1d3610695/utils/build_swift/driver_arguments.py#L1047) (to ensure testing by CI).

I've been using this flag unnecessarily in my local builds then :( ... thanks @dan-zheng for the clarification and sorry @tkclimb for the misleading info.
