How to implement Python into Xcode using pythonkit 3rd party software, or any others please suggest

so I'm pretty new to swift, but I have a problem with implimenting Pythonkit. But basically to provide a short summary, the kit basically allows you to be able to utilize python inside of swift (using certain swift functions in order to save variables, do functions, however, can execute the code of a .py file). In the process of doing this however this error pops up

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-

So I tried updating my Python software to 3.0 and above (I'm not quite sure, I think it was 3.7), however when I run python it shows it is at version 2.7. This kind of confused me a bit, however it was until the error did I notice something was wrong. Why does it show that error? What does it mean? (I did find a similar post with a similar error in stack overflow, however I didn't have enough reputation to comment as well as I don't think the answer was addressed to what I wanted so I'm just going to continue writing here) The swift code itself has updated (xcode11) and following the steps a lot of the functions have changed within Xcode, what is it that I did wrong that caused the error? Following the link it seems I have to utilize pyto? Checking that link the functions of pyto are different than the ones of PythonKit. In PythonKit you can utilize python within your swift code, while pyto seems to be a python integration for iOS? Any advice? And please ask if anything needs clarification, or if there's a different solution please tell me. Thanks,Vince

iOS/tvOS/watchOS, or macOS? I don't think python is installed on iOS/tvOS/watchOS. Pyto installs a python installation on iOS that is local to the application.

Should work on macOS

it's macOS Xcode version 11

How did you update your Python software? /usr/bin/python runs the interpreter in /System/Library/Frameworks/Python.framework which is 2.7.16 (MacOS Catalina, 10.15.2). That is, I think, what PythonKit tries to use if you haven't specified the PYTHON_LIBRARY environment variable.

You need to setup the PYTHON_LIBRARY environment variable to point at your installed Python3 installation.

ok....so how do I setup the environment variable?

At your shell, type export PYTHON_LIBRARY=<filename here>. If you are going do it a lot, you should put it into your startup scripts.
If that doesn't work, you might want contact the PythonKit folks

wait...help me out here, when you mean shell, where do I put that? Since I downloaded python kit as a framework into swift, and those files aren't editable. And in the viewController, is that what we add? But won't it show a error since there is no variable python_library?
So where do we put it?

What do you know about Unix, environment variables, login scripts? By "shell," I'm referring to the command line interpreter that is run when you open Terminal. PYTHON_LIBRARY appears to be a shell environment variable that is supposed to point to the Python shared library that underpins the python interpreter. A good place to set that variable is in your login scripts (.login for tcsh, .profile for bash, not sure what to use for zsh, have to look at the man page.)

What did you do to upgrade to Python3?

The biggest question is where is your Python.framework located? I have a Python.framework loaded in /Library/Frameworks that has both 2.7 and 3.8 infrastructure. I downloaded the PythonKit package and built PythonTool which found the right library without having to set anything.

Ok, so I'm not very used to working with scripts, (bash that sort). I usually work with IDE, playground, but not scripts, ever since a few years ago. So I am using Xcode, and not the bash to execute my programs.
I went to the website and I clicked download python 3.8, currently, it is /Applications/Python 3.8
I don't see the 2.7 infrastructure inside applications .
I can't help but notice you said something about Python tool, I was following this video (Swift Today 1: Python Swift Interoperability! - YouTube), and I don't recall them saying anything about it, looking in the file it says a file with .main, so am I supposed to have that function??

And my terminal is bash by the way

If you go to your PythonKit directory and run ```swift package generate-xcodeproj", and then open the project, you'll see a target called PythonTool. If you build and run that target (it will build the PythonKit framework as well), it will tell you what version of the library it found (it can do other stuff as well).

But, it looks like you are using the GUI version of the python interpreter (IDLE). If you open IDLE, and look at the "Path Browser" results, you should see what python environment is being accessed. When I look at "Path Browser", I get my Python framework that is installed in /Library/Frameworks, and points at the 3.8 version. That's what PythonKit finds when it goes through and searches for python frameworks. For some reason, your python framework does not seem to be installed at a location that PythonKit scans, and so it's giving you the error and states that you need to setup the PYTHON_LIBRARY environment variable. You should have a Python.framework installed in either /Library/Frameworks, or $HOME/Library/Frameworks (it may also look into /usr/local/lib). What it's looking for is a python shared library (or maybe a static library), not the interpreter itself.

When you find where your python 3 framework is located, I would edit the .bashrc file in your home directory and add:

export PYTHON_LIBRARY=<Path to your Python framework>/Versions/3.8. You may need to add Python at the end, that is the actual shared library.

or, move it into one of the standard locations.

UPDATE: I did some more digging into the PythonKit code, and the search paths that it looks for Python frameworks is the current working directory (would probably be $SRCROOT if you are running in Xcode), or /usr/local/Frameworks (probably assuming a Homebrew installation of Python). I added the the system locations (/Library/Frameworks and /System/Library/Frameworks) and everything worked fine.

The relevant line in PythonKit to modify is in file PythonLibrary.swift, and its the setting of librarySearchPaths in the Darwin section.

You might want to contact the PythonKit developers and ask them about what their intentions are about using PythonKit with Xcode and MacOS. You can install Python using Homebrew, or copy an existing Python.framework into /usr/local/Frameworks and it should work okay, too.

I also tried setting the PYTHON_LIBRARY environment variable to both the Python.framework/Python file (which is a dynamic library), and the libpython3.8.dylib in the lib directory. Couldn't make either work. It looks like the best bet is to put Python.framework in /usr/local/Frameworks, or modify the library search paths to find the appropriate framework.

1 Like

Hmm.. Let me test some things out, I need to try some things out, but thanks for the advice and feedback. I hope this works

I use this solution that I posted to the other thread