Let's go back to basics.
Here's a basic demo package using PythonKit:
If I run that swift code, it prints the following:
Python 3.12
Python Version: 3.12.3 (main, Apr 9 2024, 08:09:14) [Clang 15.0.0 (clang-1500.3.9.4)]
Python PATH
['/opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python312.zip', '/opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12', '/opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12/lib-dynload', '/opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages']
Error importing module 'camelcase': Python exception: No module named 'camelcase'
Program ended with exit code: 0
As expected it shows the only version of Python (3.12.3) that I have on my machine (installed via homebrew.)
The import fails because there is no Python module named camelCase
in any of the paths listed.
In that package there is also a Python script (pythonScript.py
) that does the same thing.
If I chmod +x
that script and run it from the terminal it outputs pretty much the same thing: (same version of Python and same PATH
values)
Python 3.12
Python Version: 3.12.3 (main, Apr 9 2024, 08:09:14) [Clang 15.0.0 (clang-1500.3.9.4)]
Python PATH: ['/Users/diggory/Code/OpenSource/PythonKitSimpleTest', '/opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python312.zip', '/opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12', '/opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12/lib-dynload', '/opt/homebrew/lib/python3.12/site-packages']
Traceback (most recent call last):
File "/Users/diggory/Code/OpenSource/PythonKitSimpleTest/./pythonScript.py", line 15, in <module>
import camelcase
ModuleNotFoundError: No module named 'camelcase'
So far so good. In fact we can confirm in the terminal that camelCase
isn't installed.
pip list
Package Version
------- -------
pip 24.0
wheel 0.43.0
Now, if I swap to my virtual environment that I made to install pip packages (as recommended by brew) you can see that camelCase is installed.
source ~/python/venv/bin/activate
pip list
Package Version
--------- -------
camelcase 0.2
pip 24.0
And I can see that the pure python script can now import the module.
./pythonScript.py
Python 3.12
Python Version: 3.12.3 (main, Apr 9 2024, 08:09:14) [Clang 15.0.0 (clang-1500.3.9.4)]
Python PATH: ['/Users/diggory/Code/OpenSource/PythonKitSimpleTest', '/opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python312.zip', '/opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12', '/opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12/lib-dynload', '/Users/diggory/python/venv/lib/python3.12/site-packages']
No error and you can see that PATH includes my virtual environment's 'site-packages' path.
Now if I add the following line to my swift code (line 16 in main.swift
)
sys.path.append(os.path.abspath("/Users/diggory/python/venv/lib/python3.12/site-packages"))
The Swift code also works.
Python 3.12
Python Version: 3.12.3 (main, Apr 9 2024, 08:09:14) [Clang 15.0.0 (clang-1500.3.9.4)]
Python PATH
['/opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python312.zip', '/opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12', '/opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12/lib-dynload', '/opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages']
Program ended with exit code: 0