6 Replies
      Latest reply on Jan 11, 2020 7:19 PM by squidsirymchenry
      squidsirymchenry Level 1 Level 1 (0 points)

        Hi,

        During the research, I found that it was possible for me to create a python file using the cross-platform function inside Xcode, however, I'm not sure if I am able to use both kinds of programming at the same time? Or do I have to create an app in Python, and then somehow link that into the Swift app that I have at the start?

        Every single question related to this topic either dates back a few years ago, or all the questions remained open, and there weren't any answers to them.

        If I'm unclear please tell me so I can respond, help would be greatly appreciated!

        Thanks

        • Re: How to implement Python along with Swift in Xcode
          Claude31 Level 8 Level 8 (8,525 points)

          I'm not sure if I am able to use both kinds of programming at the same time?

          No, you should nor be able to code directly in Python in your code.

           

          however, I'm not sure if I am able to use both kinds of programming at the same time?Did you see this one ? Lots of hints on how to.

           

          https://stackoverflow.com/questions/31927604/call-python-code-from-an-existing-project-written-in-swift

            • Re: How to implement Python along with Swift in Xcode
              squidsirymchenry Level 1 Level 1 (0 points)

              So I researched some more and I found PythonKit, which I found in the website, however, it throws up an error when I try to impliment it.

               

              Fatal error: Python library not found. Set the PYTHON_LIBRARY environment variable with the path to a Python library.: file /Users/****/Library/Developer/Xcode/DerivedData/Y******L-fmpvatcxzxcglggfnkmiateqeipe/SourcePackages/checkouts/PythonKit/PythonKit/PythonLibrary.swift, line 40

              2019-12-31 19:10:33.397840-0700 iSee[4616:1847618] Fatal error: Python library not found. Set the PYTHON_LIBRARY environment variable with the path to a Python library.: file /Users/*****/Library/Developer/Xcode/DerivedData/Y******L-fmpvatcxzxcglggfnkmiateqeipe/SourcePackages/checkouts/PythonKit/PythonKit/PythonLibrary.swift, line 40

               

              I found the location of where the code asks to throw up that error.

              public struct PythonLibrary {
                private static let shared = PythonLibrary()
                private static let pythonLegacySymbolName = "PyString_AsString"
                private static var librarySymbolsLoaded = false
              
                private let pythonLibraryHandle: UnsafeMutableRawPointer
                private let isLegacyPython: Bool
              
                private init() {
                  guard let pythonLibraryHandle = PythonLibrary.loadPythonLibrary() else {
                    fatalError("""
                      Python library not found. Set the \(Environment.library.key) \
                      environment variable with the path to a Python library.
                      """)
                  }
                  self.pythonLibraryHandle = pythonLibraryHandle

              on line 11 it shows that.

              The code was a swift package

              below is the code in viewcontroller

                         //       let os: PythonObject = Python.import("os")
                             //   print ("os")
                                let sys: PythonObject = Python.import("sys")
                                print("sys")
                                sys.path.append("/usr/local/lib/python3.7/site-packages")
                               // sys.path.append("/Users/*****/Applications/Python 3.8")

              The error throw up when it hits line 08.

              Any help here? It would be amazing, again I searched, but I couldn't find a solution that would work, or I didn't understand what they were talking about?

              Thanks.

              • Re: How to implement Python along with Swift in Xcode
                squidsirymchenry Level 1 Level 1 (0 points)

                My answer is currently being monitored for a few days now, i copy pasted an error that I had but it shows its being moderated. I tried editing some of the pasting but it dosent seem to work. So I just wait or am I doing something wrong?