Swift Playground Error: Module 'Python' Has No Member Named 'Import'

swift playground error: module 'Python' has no member named 'import'

A few things to check.

  1. Presumably you're using the swift / tensorflow toolchain from latest download link here (and you've configured Xcode to use it.
    View Xcode’s (Preferences, Components > Toolchains, and select the installed Swift for TensorFlow toolchain.)
    https://github.com/tensorflow/swift/blob/master/Installation.md

  2. Make sure you change the Xcode build system to legacy (File > Project Settings > Build System).

  3. Check the runtime search path
    /Library/Developer/Toolchains/swift-latest/usr/lib/swift/macosx

UPDATE:
Have a look at XcodeGen which can automagically create an xcode project with relevant settings for swift for tensorflow.
I crafted a project.yml file here

DO NOT include libppython2.7.tbd in the linked frameworks. tensorflow will automagically find relevant python version 3 or 2.

I can't use Python module in Swift

A year later, I got a solution.
The name of module was wrong.

import PythonKit
let np = Python.import("numpy")

I should have installed on this way and implemented this way exactly.

Thanks regards!

Xcode: 11.3.1

Swift: 4.2

Toolchain: Swift for TensorFlow 0.8 Release

How to setup swift project in a terminal to enable python

Turns out the procedure is rather trivial. Here are the steps I did on macOS:

  1. Download Tensforflow swift toolchain, it will install new swift toolchain into /Library/Developer/Toolchains

  2. setup environment

    export PATH=/Library/Developer/Toolchains/swift-latest/usr/bin:$PATH
  3. Create new package

    swift package init --type executable
  4. Adjust created Package.swift file to have this section (replace MyCde with whatever your package/dir name is:

    let package = Package(
    name: "MyCode",
    platforms: [
    .macOS(.v10_15),
    ], ....

The latter changes requires to enable support for Python in TF (my understanding the macOS should be above 10.13).

After that I was able to compile my package as simple as

swift build

How to I import 3rd party frameworks into Xcode Playground?

Edited: As of Beta5 this is now supported when the playground is part of the workspace that builds the framework as a target. There are more details on the Apple dev forums site, but hopefully @Rick Ballard will add them to his answer here and that should becoke the definitive answer for this question.

Don't do the below anymore!


For the short term, while there's no supported solution, if you're producing a Module/Framework you can copy it into the SDKs System/Library/Frameworks directory and then just import <#Module#> the same way as you import system Frameworks.

For an OS X Playground: /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/System/Library/Frameworks

And for iOS: /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk/System/Library/Frameworks/

But before you do that... go file a radar as @Rick says in his answer.

python: import numpy as np from outer code gets lost within my own user defined module

The correct approach is to do an import numpy as np at the top of your sub-module as well. Here's why:

The key thing to note is that in Python, global actually means "shared at a module-level", and the namespaces for each module exist distinct from each other except when a module explicitly imports from another module. An imported module definitely cannot reach out to its 'parent' module's namespace, which is probably a good thing all things considered, otherwise you'll have modules whose behavior depends entirely on the variables defined in the module that imports it.

So when the stack trace says global name 'np' is not defined, it's talking about it at a module level. Python does not let the WC_Class module access objects in its parent module by default.

(As an aside, effbot has a quick note on how to do inter-module globals)

Another key thing to note is that even if you have multiple import numpy as np in various modules of your code, the module actually only gets loaded (i.e. executed) once. Once loaded, modules (being Python objects themselves) can be found in the dictionary sys.modules, and if a module already exists in this dictionary, any import module_to_import statement simply lets the importing module access names in the namespace of module_to_import. So having import numpy as np scattered across multiple modules in your codebase isn't wasteful.

Edit: On deeper digging, effbot has an even deeper (but still pretty quick and simple) exploration of what actually happens in module imports. For deeper exploration of the topic, you may want to check the import system discussion newly added in the Python 3 documentation.

Call Python code from an existing project written in Swift

If anyone is ever interested in calling python from swift, here is some helpful material I found:

  • U the python framework - https://developer.apple.com/library/ios/technotes/tn2328/_index.html
  • PyObjC (a little more challenging) -
  • Cobbal - https://github.com/cobbal/python-for-iphone
  • Python docs (you would need to make C-Swift bridge)

Most of it is for Objective-c, but if you need to use swift you can easily just create an ObjC-Swift bridge (super-super easy) - Lookup the apple docs

Playground Import: No Such Module 'Foo'

The solution, from Apple Support, was to adjust my Xcode Preferences. On Locations :: Advanced my configuration was 'Legacy'. By changing to 'Unique', and undoing any paths I'd attempted to insert, Playgrounds can now import frameworks.

Swift 4.2, String firstIndex() function error in Xcode playground

You can try index(of

let index = greeting.index(of: ",") ?? greeting.endIndex

as firstIndex exists in Xcode 10 beta Doc

I can't import Python modules in Xcode 11 using PythonKit

First SO answer, so please forgive formatting / etc. I suffered through this for a while myself with different errors, but generally same issues. I hope this helps you -- a few resources to consider:

1) Pyto -- a fully embedded Python environment for iOS/Catalyst; with LXML and Python Library porting instructions <-- this is what you need to model after to run on iOS, my solution works for Mac Catalyst (Macs with Python preloaded)

2) Python Kit Tutorial -- this guy goes through, step by step, how to implement PythonKit

Here's what worked for me:

1) Disable App Sandbox in Signing and Capabilities:

In top right corner of App Sandbox, under Signing & Capabilities there is an "X", click that to remove App Sandbox

2) In "Hardened Runtime" under Signing and Capabilities: check "Disable Library Validation"

Image of checkbox for Disable Library Validation

Now, I haven't yet submitted an app to the App Store under these provisions, but at least my python files and libraries load / build / run!


UPDATE 05/15/2020:

For Mac Developer Distribution, you will have to sign all .so or .dylib's included with your app, along with the Python Interpreter and bin folder. I made a quick bash script to run through each one when done with dev.

function signThese() {
find . -name "*.$1" | while read line; do
codesign --force --verbose=4 --options=runtime --timestamp --sign "Developer ID Application: [INSERT YOUR CERT HERE]" $line
done
}


This will allow you to use AppSandbox in Signing and Capabilities, and all Hardened Runtime Options (as in not disabling library validation).

Xcode Playgrounds Shared Directory Not Working

That's the path that iOS playground use. If you make a macOS playground it will have the path ~/Documents/Shared Playground Data



Related Topics



Leave a reply



Submit