# PythonKit not finding Python 3 library

**URL:** <https://forums.swift.org/t/pythonkit-not-finding-python-3-library/45381>\
**Category:** Related Projects\
**Tags:** python, question, pythonkit\
**Created:** [March 6, 2021, 9:02am UTC](https://forums.swift.org/t/pythonkit-not-finding-python-3-library/45381 "2021-03-06T09:02:26Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/carltongibson/32/14811_2.png) [@carltongibson](https://forums.swift.org/u/carltongibson)\
**Post date:** [March 6, 2021, 9:02am UTC](https://forums.swift.org/t/pythonkit-not-finding-python-3-library/45381/1 "2021-03-06T09:02:27Z")

</div>

Hi. 👋

I'm experimenting with @pvieito's great looking PythonKit. I'm having a weird `dlopen`/search path issue.  
I'm wondering if anyone can advise?

### Problem

Following the project README I have a very simple hello world setup that imports PythonKit, sets `sys.path` to find my local script, imports that, and calls a function that prints a _Hello, World!_.

Thus far, all great. Out of the box, PythonKit is finding Python 2.7, so (again following the README) I try setting the `PYTHON_VERSION` and `PYTHON_LIBRARY` environment variables. (I also set `PYTHON_LOADER_LOGGING` for good measure.)

**The issue is** `PYTHON_LIBRARY` seems correct — it reports as loading correctly — but Python 2.7 is still being loaded.

This is the sample code from the README:

```swift
let sys = Python.import("sys")
print("Python \(sys.version_info.major).\(sys.version_info.minor)")
print("Python Version: \(sys.version)")
print("Python Encoding: \(sys.getdefaultencoding().upper())")

```

Which (with `PYTHON_LIBRARY` set) outputs:

```lldb
Loading symbol 'Py_Initialize' from the Python library...
Trying to load library at '/opt/boxen/homebrew/Cellar/python@3.8/3.8.7/Frameworks/Python.framework/Python'...
Library at '/opt/boxen/homebrew/Cellar/python@3.8/3.8.7/Frameworks/Python.framework/Python' was sucessfully loaded.
Loading symbol 'PyEval_GetBuiltins' from the Python library...
Loading symbol 'Py_IncRef' from the Python library...
Loading symbol 'PyRun_SimpleString' from the Python library...
Loading symbol 'PyImport_ImportModule' from the Python library...
Loading symbol 'PyObject_GetAttrString' from the Python library...
Loading symbol 'Py_DecRef' from the Python library...
Loaded legacy Python library, using legacy symbols...
Loading symbol 'PyString_FromStringAndSize' from the Python library...
Loading symbol 'PyObject_GetItem' from the Python library...
Loading symbol 'PyErr_Occurred' from the Python library...
Loading symbol 'PyTuple_New' from the Python library...
Loading symbol 'PyTuple_SetItem' from the Python library...
Loading symbol 'PyObject_CallObject' from the Python library...
Loading symbol 'PyString_AsString' from the Python library...
Python 2.7
Python Version: 2.7.16 (default, Jan 22 2021, 05:09:26) 
[GCC Apple LLVM 12.0.5 (clang-1205.0.19.9) [+internal-os, ptrauth-isa=deploymen
Python Encoding: ASCII

```

With the `Library at <PATH> was sucessfully loaded.`, which is the **3rd line of the output above** , I'm expecting Python 3.8, but 2.7 is in fact what we get.

With a test script such as:

```
print("Hello, world! (From Python 💃)")

```

... we get a crash, without declaring the file encoding — Ah, yes, definitely Python 2… 🙂

I'm not quite sure (yet) how to get the planets aligned here. How can I get PythonKit to pick up the right Python version? Is there something obvious I'm missing? (Quite likely.)

Many thanks if you're to advise! I know this sort of thing is difficult to comment on.  
Kind Regards,

Carlton

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/carltongibson/32/14811_2.png) [@carltongibson](https://forums.swift.org/u/carltongibson)\
**Post date:** [March 6, 2021, 9:53am UTC](https://forums.swift.org/t/pythonkit-not-finding-python-3-library/45381/2 "2021-03-06T09:53:46Z")

</div>

[Possibly a related post in another thread](https://forums.swift.org/t/how-to-implement-python-into-xcode-using-pythonkit-3rd-party-software-or-any-others-please-suggest/32319/13) suggesting the need to edit the `PythonLibrary.swift` file to set the search paths correctly. 🤔

---

<div class="post-metadata">

**Author:** ![pvieito](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pvieito/32/33613_2.png) [@pvieito](https://forums.swift.org/u/pvieito)\
**Post date:** [March 6, 2021, 11:21am UTC](https://forums.swift.org/t/pythonkit-not-finding-python-3-library/45381/3 "2021-03-06T11:21:40Z")

</div>

Hi! That may be related to the Python 2 library being linked to the executable, you can check that with `otool`:

```swift
$ otool -L path_to_your_executable
	/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1770.255.0)
	/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1292.60.1)
	/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1770.255.0)
	@rpath/libswiftCore.dylib (compatibility version 1.0.0, current version 1200.2.41)
	@rpath/libswiftCoreFoundation.dylib (compatibility version 1.0.0, current version 1.6.0, weak)
	@rpath/libswiftCoreGraphics.dylib (compatibility version 1.0.0, current version 2.0.0, weak)
	@rpath/libswiftDarwin.dylib (compatibility version 1.0.0, current version 0.0.0)
	@rpath/libswiftDispatch.dylib (compatibility version 1.0.0, current version 4.40.2, weak)
	@rpath/libswiftFoundation.dylib (compatibility version 1.0.0, current version 20.0.0)
	@rpath/libswiftIOKit.dylib (compatibility version 1.0.0, current version 1.0.0, weak)
	@rpath/libswiftObjectiveC.dylib (compatibility version 1.0.0, current version 1.0.0, weak)
	@rpath/libswiftXPC.dylib (compatibility version 1.0.0, current version 1.1.0, weak)

```

Make sure no references are made to Python and that you are not linking nor importing the `Python` module in your code.

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/carltongibson/32/14811_2.png) [@carltongibson](https://forums.swift.org/u/carltongibson)\
**Post date:** [March 6, 2021, 3:04pm UTC](https://forums.swift.org/t/pythonkit-not-finding-python-3-library/45381/4 "2021-03-06T15:04:02Z")

</div>

Hi @pvieito — thanks for the quick response!

I can't see that I'm linking against Python...

```swift
$ otool -L /Users/carlton/Library/Developer/Xcode/DerivedData/FromPython-axlvqoykyiwdfwddzgxlgofxeral/Build/Products/Debug/FromPython
/Users/carlton/Library/Developer/Xcode/DerivedData/FromPython-axlvqoykyiwdfwddzgxlgofxeral/Build/Products/Debug/FromPython:
	/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1770.255.0)
	/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1292.60.1)
	/usr/lib/swift/libswiftCore.dylib (compatibility version 1.0.0, current version 1200.2.41)
	/usr/lib/swift/libswiftCoreFoundation.dylib (compatibility version 1.0.0, current version 1.6.0, weak)
	/usr/lib/swift/libswiftCoreGraphics.dylib (compatibility version 1.0.0, current version 2.0.0, weak)
	/usr/lib/swift/libswiftDarwin.dylib (compatibility version 1.0.0, current version 0.0.0)
	/usr/lib/swift/libswiftDispatch.dylib (compatibility version 1.0.0, current version 4.40.2, weak)
	/usr/lib/swift/libswiftFoundation.dylib (compatibility version 1.0.0, current version 20.0.0, weak)
	/usr/lib/swift/libswiftIOKit.dylib (compatibility version 1.0.0, current version 1.0.0, weak)
	/usr/lib/swift/libswiftObjectiveC.dylib (compatibility version 1.0.0, current version 1.0.0, weak)
	/usr/lib/swift/libswiftXPC.dylib (compatibility version 1.0.0, current version 1.1.0, weak)

```

🤔 Still thinking.

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/carltongibson/32/14811_2.png) [@carltongibson](https://forums.swift.org/u/carltongibson)\
**Post date:** [March 6, 2021, 3:45pm UTC](https://forums.swift.org/t/pythonkit-not-finding-python-3-library/45381/5 "2021-03-06T15:45:39Z")

</div>

Hey @pvieito — it's always SIP 😀

Set "Enable Hardened Runtime" to "No" and I get the correct Python load.  
💃 — I'm off. 🎉

> [@pvieito](#):
>
> ...may be related to the Python 2 library being linked to the executable

I'll experiment but, this makes me wonder if static linking the `libpython` would work (despite the `dlopen`)... — that would be quite handy. Gonna have a read of [Use RTDL\_SELF to load embedded symbols by kewlbear · Pull Request #30 · pvieito/PythonKit · GitHub](https://github.com/pvieito/PythonKit/pull/30).

Thanks so much for your efforts on this lib. Very exciting! (I see you don't have Issues or Discussions enabled, so you prefer it here yes?)

Have a great rest of your weekend, and thank you for the help! 🏅

---

<div class="post-metadata">

**Author:** ![pvieito](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pvieito/32/33613_2.png) [@pvieito](https://forums.swift.org/u/pvieito)\
**Post date:** [March 7, 2021, 11:13am UTC](https://forums.swift.org/t/pythonkit-not-finding-python-3-library/45381/6 "2021-03-07T11:13:35Z")

</div>

Oh, gotcha! Yes, with the “Hardened Runtime” enabled the process won't load libraries not signed by Apple or the developer. You can embed the Python framework in the app and sign it with your developer certificate.

---

<div class="post-metadata">

**Author:** ![photonclock](https://avatars.discourse-cdn.com/v4/letter/p/34f0e0/32.png) [@photonclock](https://forums.swift.org/u/photonclock)\
**Post date:** [March 15, 2021, 11:19am UTC](https://forums.swift.org/t/pythonkit-not-finding-python-3-library/45381/7 "2021-03-15T11:19:40Z")

</div>

Do you know the exact steps to accomplish that (embed the Python framework in the app)?

---

<div class="post-metadata">

**Author:** ![jbmaxwell](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jbmaxwell/32/19788_2.png) [@jbmaxwell](https://forums.swift.org/u/jbmaxwell)\
**Post date:** [October 8, 2021, 11:17pm UTC](https://forums.swift.org/t/pythonkit-not-finding-python-3-library/45381/8 "2021-10-08T23:17:37Z")

</div>

I'm having very weird problems loading python today. It's been loading fine until recently, and I'm really not sure what could have changed. My python lib is from Conda so I point to that in my `PythonLibrary.useLibrary(at:)` call. The `PYTHON_LOADER_LOGGING` indicates that it is trying to load the intended version. I'm running the app on macOS and I've disabled App Sandbox and Hardened Runtime in the Build Settings:

```swift
Loading symbol 'Py_Initialize' from the Python library...
Trying to load library at '/Users/xxxx/anaconda3/envs/torch_17/lib/libpython3.8.dylib'...
PythonKit/PythonLibrary.swift:46: Fatal error: Python library not found. Set the PYTHON_LIBRARY environment variable with the path to a Python library.
2021-10-08 16:15:09.479242-0700 SpliqGenerate[4532:37353] PythonKit/PythonLibrary.swift:46: Fatal error: Python library not found. Set the PYTHON_LIBRARY environment variable with the path to a Python library.

```

Based on the error I also added `PYTHON_LIBRARY` to my scheme's Environment Variables, but it makes no difference.

Any ideas? (Particularly curious what might have made a working lib suddenly stop working.)

PS — Just running python at the terminal in this Conda env works as expected.

UPDATE: The one thing that's changed is that I've added other package dependencies to the project. Is it possible that one of these is somehow preventing Python from loading?

A little further info: it _is_ able to load my non-anaconda (brew, I think) system python3 (3.8.1), so something specifically related to conda?...

UPDATE 2: Tried a new basic test app, with no dependencies, and it's the same thing. Seems to be isolated to Conda env python versions. Weird. It was working... (I suppose I could install all my dependencies into my brew python3, but I don't love the idea...)  
I've tried other envs, too, with no success.

UPDATE 3: Finally figured this out. It wasn't clear to me the difference between `Enable App Sandbox` in Build Settings and the actual `App Sandbox` entitlement in my app's `.entitlements` file. It seems the **file** has to have its `App Sandbox` key set to `NO`. It's very strange to me that Xcode doesn't include that in the actual app GUI, but I suppose they're _actively discouraging_ people from changing it... who knows...??

---

<div class="post-metadata">

**Author:** ![acediac](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/acediac/32/27869_2.png) [@acediac](https://forums.swift.org/u/acediac)\
**Post date:** [December 7, 2022, 5:06am UTC](https://forums.swift.org/t/pythonkit-not-finding-python-3-library/45381/9 "2022-12-07T05:06:59Z")

</div>

Confirmed, to get PythonKit in my swift app to load my macports python library I had to:

1. Set environment variable PYTHON\_LIBRARY = "/opt/local/Library/Frameworks/Python.framework/Python" under Scheme-\>Run
2. Remove App Sandbox section from build settings -\> Signing and capabilities (click the trashcan icon next to the whole section)

---

<div class="post-metadata">

**Author:** ![boiwantlearncode](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/boiwantlearncode/32/28651_2.png) [@boiwantlearncode](https://forums.swift.org/u/boiwantlearncode)\
**Post date:** [February 5, 2023, 9:15am UTC](https://forums.swift.org/t/pythonkit-not-finding-python-3-library/45381/10 "2023-02-05T09:15:04Z")

</div>

Works when I set the environment variable PYTHON\_LIBRARY but mine's in a different path. Had to run "which -a python" to find the path

---

<div class="post-metadata">

**Author:** ![sspringer](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@sspringer](https://forums.swift.org/u/sspringer)\
**Post date:** [February 5, 2023, 9:30am UTC](https://forums.swift.org/t/pythonkit-not-finding-python-3-library/45381/11 "2023-02-05T09:30:26Z")

</div>

> [@acediac](#):
>
> Set environment variable PYTHON\_LIBRARY = "/opt/local/Library/Frameworks/Python.framework/Python"

To my knowledge, PYTHON\_LIBRARY should reference the Python library file and not the directory where Python is installed.

---

<div class="post-metadata">

**Author:** ![begginer](https://avatars.discourse-cdn.com/v4/letter/b/c67d28/32.png) [@begginer](https://forums.swift.org/u/begginer)\
**Post date:** [January 29, 2025, 5:56pm UTC](https://forums.swift.org/t/pythonkit-not-finding-python-3-library/45381/12 "2025-01-29T17:56:11Z")

</div>

I tried all of this ways but there is still this error, can someone answer in Russian what should I do (preferably)

---

<div class="post-metadata">

**Author:** ![sspringer](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@sspringer](https://forums.swift.org/u/sspringer)\
**Post date:** [January 31, 2025, 7:17am UTC](https://forums.swift.org/t/pythonkit-not-finding-python-3-library/45381/13 "2025-01-31T07:17:35Z")

</div>

Try to run [my little example](https://github.com/stefanspringer1/SwiftHelloPython) (do what its README says).
