Swift: Can't change PythonLibrary version in S4TF v0.8 and development 2020-03-25

Created on 2 Apr 2020  路  7Comments  路  Source: tensorflow/swift

Input

import PythonKit
PythonLibrary.useVersion(3)

Error

Thread 1: Fatal error: Python library not found. Set the PYTHON_LIBRARY environment variable with the path to a Python library.

I am stuck on Python 2.7.

Most helpful comment

Based on what you describe, I believe this is due to missing or strangely located Python libraries for version 3+ on your Catalina system. I was able to replicate this error on a pretty clean Catalina system, because by default the only Python library installed in /usr/lib is libpython2.7.dylib. Even when you run python3 for the first time to install the Command Line Tools, I don't see a corresponding libpython3 library being installed with that.

What you might need to do is install Python3 via homebrew or another mechanism so that you can get the appropriate libpython3 libraries installed on your system, unless I'm missing where these get installed on Catalina. The toolchain and PythonKit appear to be functioning as intended, it's just that if there's no local libpython3 they are unable to use that version.

All 7 comments

To confirm, if you use a 0.7 toolchain on your system under the same circumstances (only replacing import PythonKit with import Python), does this work then?

Nope, it doesn't work.

@BradLarson any solution please?

wfm ubuntu 18.04 master fwiw

Welcome to Swift version 5.3-dev (LLVM e8106fe77b, Swift 0f3775689b).
Type :help for assistance.
  1> import PythonKit 
  2. PythonLibrary.useVersion(3) 
  3. Python.versionInfo.description
$R0: String = "sys.version_info(major=3, minor=6, micro=9, releaselevel=\'final\', serial=0)"

@brettkoonce thanks for responding (I urgently need a solution). By executing your code I again get the error on line 3:

Thread 1: Fatal error: Python library not found. Set the PYTHON_LIBRARY environment variable with the path to a Python library.

But if I comment out line 2 then it prints the following string to console.

sys.version_info(major=2, minor=7, micro=16, releaselevel='final', serial=0)

I'm stuck on Python 2.7 version.

My system's information:

  • macOS Catalina Version 10.15.4
  • Xcode Version 11.4

Based on what you describe, I believe this is due to missing or strangely located Python libraries for version 3+ on your Catalina system. I was able to replicate this error on a pretty clean Catalina system, because by default the only Python library installed in /usr/lib is libpython2.7.dylib. Even when you run python3 for the first time to install the Command Line Tools, I don't see a corresponding libpython3 library being installed with that.

What you might need to do is install Python3 via homebrew or another mechanism so that you can get the appropriate libpython3 libraries installed on your system, unless I'm missing where these get installed on Catalina. The toolchain and PythonKit appear to be functioning as intended, it's just that if there's no local libpython3 they are unable to use that version.

@BradLarson thanks for detailed explanation.

I simply removed all python3 stuff:

sudo rm -rf /usr/local/bin/python3*

and then uninstalled and again installed python3 with brew:

brew uninstall python3
brew install python3

Now it works and Python 3 is default!

Input

import PythonKit
print(Python.versionInfo.description)
PythonLibrary.useVersion(2)
print(Python.versionInfo.description)

Output

sys.version_info(major=3, minor=7, micro=7, releaselevel='final', serial=0)
sys.version_info(major=2, minor=7, micro=16, releaselevel='final', serial=0)
Was this page helpful?
0 / 5 - 0 ratings