I'm using PythonKit in my Swift project for MacOS. At the moment I'm using Python 2.7 but from MacOs 12.3 it isn't no more supported so I'm trying to migrate to Python 3 but it doesn't work.
func applicationDidFinishLaunching(_ notification: Notification) {
if #available(OSX 12, *) {
PythonLibrary.useVersion(3)
}
else {
PythonLibrary.useVersion(2)
}
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())")
sys.path.append(Bundle.main.resourceURL!.absoluteURL.path)
let checker = Python.import("checkLibrary")
_ = Array(checker.check())
}
This is the error message:
PythonKit/PythonLibrary.swift:46: Fatal error: Python library not found. Set the PYTHON_LIBRARY environment variable with the path to a Python library.
The code fail on MacOs 12 on line 9th line (let sys = Python.import("sys")), so I can't interact so sys in any way.
I've already tried to disable sandbox and Hardened Runtime but is seems useless.
I was having the same issue.
where python3
which python3
type -a python3
I could clearly see that Python3 was present using any of the above commands from terminal. Python3 wasnt something that I directly installed, (probably something I added during an install of XCode) but I could see it located at "/usr/bin/python3"
I had already removed the sandbox and disabled the hardened runtime.
Adding the environment variable to XCode did not work and Google ultimately told me to do what had already been done.
Finally, I decided to just perform a fresh install, following the blog as guidance.
https://www.dataquest.io/blog/installing-python-on-mac/#installing-python-mac
https://www.python.org/downloads/macos/ (direct URL for Python download)
After installing, everything worked as expected.
PythonLibrary.useVersion(3)
PythonLibrary.useLibrary(at: "/usr/local/bin/python3")
I could use either of the above methods, without adding the environment variable to the XCode scheme.
Hopefully that helps
I am trying to get set up with the Bloomberg Python API. I have been following along with this tutorial. I am able to successfully python -m pip install — index-url=https://bloomberg.bintray.com/pip/simple blpapi, but then when I try to run import blpapi in the Python interpreter, I get the following error. How do I resolve this error (wondering if it has something to due with me using Python 3.8)?
ImportError:
---------------------------- ENVIRONMENT -----------------------------
Platform: Windows-10-10.0.17763-SP0
Architecture: ('32bit', 'WindowsPE')
Python: 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:21:23) [MSC v.1916 32 bit (Intel)]
Python implementation: CPython
blpapi 64-bit will be loaded from: "C:\blp\API\blpapi_cpp_3.13.1.1\lib\blpapi3_64.dll"
blpapi 32-bit will be loaded from: "C:\blp\API\blpapi_cpp_3.13.1.1\lib\blpapi3_32.dll"
System PATH: (* marks locations where blpapi was found)
"C:\Windows\system32"
"C:\Windows"
"C:\Windows\System32\Wbem"
"C:\Windows\System32\WindowsPowerShell\v1.0\"
"C:\Windows\System32\OpenSSH\"
"C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL"
"C:\Program Files\Intel\Intel(R) Management Engine Components\DAL"
"c:\nx_dll"
"C:\Program Files\nodejs\"
* "C:\blp\API\blpapi_cpp_3.13.1.1\lib"
"C:\Program Files\MiKTeX 2.9\miktex\bin\x64\"
"C:\Users\cpage\AppData\Local\Programs\Python\Python38-32\Scripts\"
"C:\Users\cpage\AppData\Local\Programs\Python\Python38-32\"
"C:\Users\cpage\AppData\Local\Microsoft\WindowsApps"
* "C:\blp\DAPI"
"C:\blp\DAPI\DDE"
"C:\Users\cpage\AppData\Roaming\npm"
"C:\Program Files\Sublime Text 3"
"C:\Users\cpage\AppData\Local\hyper\app-3.0.2\resources\bin"
"C:\Users\cpage\AppData\Local\Programs\Microsoft VS Code\bin"
* "C:\blp\API\blpapi_cpp_3.13.1.1\lib"
blpapi package at: "C:\Users\cpage\AppData\Local\Programs\Python\Python38-32\lib\site-packages"
Current directory: "C:\Users\cpage"
----------------------------------------------------------------------
No module named '_versionhelper'
Could not open the C++ SDK library.
Download and install the latest C++ SDK from:
http://www.bloomberg.com/professional/api-library
If the C++ SDK is already installed, please ensure that the path to the library
was added to PATH before entering the interpreter.
You need to download the python blpapi package having the version matching your C++ lib.
In your case, since your C++ lib is 3.13.1.1, you can download the 3.13.1 python package:
pip install --index-url=https://bloomberg.bintray.com/pip/simple blpapi==3.13.1
Just managed to make it run using version blpapi-3.15.2 using C++ API blpapi_cpp_3.15.0.1
To install run:
python -m pip install --index-url=https://bloomberg.bintray.com/pip/simple blpapi
to import use:
import os
with os.add_dll_directory('<...>\blpapi_cpp_3.15.0.1\lib'):
import blpapi
or add '<...>\blpapi_cpp_3.15.0.1\lib' to PATH
The following procedure works out of the box for CPython 2.7 and 3.7. A Bloomberg rep claims he verified it for CPython 3.5 as well:
Download the C++ BLP API from the official Bloomberg repository. If this link breaks, use Bloomberg API Support homepage to find the up-to-date ones.
Unpack the archive to C:\Programs\blpapi_cpp_3.14.3.1
Exit from the shell.
Set the environment variable BLPAPI_ROOT=C:\Programs\blpapi_cpp_3.14.3.1\. Note that the trailing slash is important.
Start a new shell.
Execute python -m pip install --index-url=https://bloomberg.bintray.com/pip/simple blpapi==3.14.0
Check the installation by executing python -c "import blpapi", which should pass without displaying messages.
I've been trying to setup my windows computer such that I can have a local postgreSQL with PostGIS extension. With this installed I hope to be able to create a project with geodjango locally before putting it in the cloud. I have been working with Django for a little while now on my local machine with the SQLite DB, but since the next project will partly be based on coordinate based data I wanted to setup the right environment.
Import note: I've installed mini-conda to run in a seperate environment. I do activate this environment "development" when I work though
I've tried to follow most of the geodjango information/tutorials online, but can't get it to work. What I've done (mostly followed this: https://docs.djangoproject.com/en/2.0/ref/contrib/gis/install/#windows):
Download and install the latest (10.3) PostgreSQL setup from https://www.enterprisedb.com/downloads/postgres-postgresql-downloads
After installation I also installed used the Application Stack Builder to install PostGis
I've installed OSGeo4W from https://trac.osgeo.org/osgeo4w/
I've created a batch script as described on the geodjango website (https://docs.djangoproject.com/en/2.0/ref/contrib/gis/install/#windows) and ran it as administrator (except for the part where it sets the path to python, cause python was already in there since I've been using python for a while now)
I've tried some command in psql shell and I think I've created a database with name: geodjango, username: **** and pass: ****.
I don't know if I have given the geodjango user all priveleges, but I suspect so.
After all of this I created a new django project and in settings.py I've added some parts:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.gis',
'nameOfMyApp',
]
I've also got this in settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'geodjango',
'USER': '****',
'PASSWORD': '****',
'HOST': 'localhost',
}
}
# FOR GEODJANGO
POSTGIS_VERSION = (2, 4, 3)
When I try to set up the database in django I run (in the right folder):
python manage.py makemigrations
I get the following error:
django.core.exceptions.ImproperlyConfigured: Could not find the GDAL library (tried "gdal202", "gdal201", "gdal20", "gdal111", "gdal110", "gdal19"). Is GDAL installed? If it is, try setting GDAL_LIBRARY_PATH in your settings.
I've tried to fix that, but nothing seems to work.
Can anybody give me some help in setting this all up locally?
Update 7-3-2018:
I've tried installing GDAL manually myself, (from: http://www.gisinternals.com/query.html?content=filelist&file=release-1911-x64-gdal-2-2-3-mapserver-7-0-7.zip the generic core components)
I've installed (what I assume are) the python bindings from https://www.lfd.uci.edu/~gohlke/pythonlibs/. Still I get the same error.
I've also tried setting the GDAL_LIBRARY_PATH to the GDAL directory or the gdal-data directory (which resides in the GDAL directory).
Now I get the following error:
OSError: [WinError 126] The specified module could not be found
(while the .dll is there...)
I have found the following to work for windows:
Run python to check if your python is 32 or 64 bit.
Install corresponding OSGeo4W (32 or 64 bit) into C:\OSGeo4W or C:\OSGeo4W64:
Note: Select Express Web-GIS Install and click next.
In the ‘Select Packages’ list, ensure that GDAL is selected; MapServer and Apache are also enabled by default, may be unchecked safely.
Make sure the following is included in your settings.py:
import os
if os.name == 'nt':
import platform
OSGEO4W = r"C:\OSGeo4W"
if '64' in platform.architecture()[0]:
OSGEO4W += "64"
assert os.path.isdir(OSGEO4W), "Directory does not exist: " + OSGEO4W
os.environ['OSGEO4W_ROOT'] = OSGEO4W
os.environ['GDAL_DATA'] = OSGEO4W + r"\share\gdal"
os.environ['PROJ_LIB'] = OSGEO4W + r"\share\proj"
os.environ['PATH'] = OSGEO4W + r"\bin;" + os.environ['PATH']
Run python manage.py check to verify geodjango is working correctly.
After updating some OSGEO4W on my Windows 10 Pro machine I started having problems with the GDAL bindings again. I previously used a combination of the solutions posted here and with this tutorial.
This is what works for me using Windows 10 Pro 64-bit, Django 3.0.6 and GDAL 3.0.4 using a python 3.7 virtual environment. I have tested it without OSGEO4W and it seems to work.
First, download the GDAL wheel from Christoph Gohlke's Unofficial Windows Binaries for Python Extension Packages.
pip install "/path/to/GDAL‑3.0.4‑cp37‑cp37m‑win_amd64.whl"
Modify the libgdal.py file in the virtual envrironment site packages by adding 'gdal300' to line 23 of the Django GDAL package python file (/path/to/virtual_env/Lib/site-packages/django/contrib/gis/gdal/libgdal.py):
elif os.name == 'nt':
# Windows NT shared libraries
lib_names = ['gdal300', 'gdal204', 'gdal203', 'gdal202', 'gdal201', 'gdal20']
Finally, in your settings.py file in your Django project add
if os.name == 'nt':
VENV_BASE = os.environ['VIRTUAL_ENV']
os.environ['PATH'] = os.path.join(VENV_BASE, 'Lib\\site-packages\\osgeo') + ';' + os.environ['PATH']
os.environ['PROJ_LIB'] = os.path.join(VENV_BASE, 'Lib\\site-packages\\osgeo\\data\\proj') + ';' + os.environ['PATH']
In my case (Windows10Pro+Python3.7.1), having the (automatically chosen) dll present was not enough, namely gdal111.dll.
I realized that I also had gdal204.dll located at C:\OSGeo4W\bin and tried to "enrich" the list variable named lib_names with 'gdal204', at line 24 (regarding Windows NT shared libraries) of %PYTHON_ROOT%\Lib\site-packages\django\contrib\gis\gdal\libgdal.py, i.e.
#[...]
elif os.name == 'nt':
# Windows NT shared libraries
lib_names = ['gdal204', 'gdal202', 'gdal201', 'gdal20', 'gdal111', 'gdal110', 'gdal19']
#[...] ^^^^^^^
No negative consequences for now.
For Microsoft Windows 10 & Python3.6.8, I installed GDAL 2.3.3 from Unofficial Windows Binaries for Python Extension Packages, modified libgdal.py adding gdal203 in the lib_names list env\Lib\site-packages\django\contrib\gis\gdal\libgdal.py.
Finally, added osgeo and proj to the PATH, and set the GDAL_LIBRARY_PATH as below (beginning of settings.py):
os.environ['PATH'] = os.path.join(BASE_DIR, r'env\Lib\site-packages\osgeo') + ';' + os.environ['PATH']
os.environ['PROJ_LIB'] = os.path.join(BASE_DIR, r'env3\Lib\site-packages\osgeo\data\proj') + ';' + os.environ['PATH']
GDAL_LIBRARY_PATH = os.path.join(BASE_DIR, r'env\Lib\site-packages\osgeo\gdal203.dll')
In this case, env is my Python environment.
I had the same error "The specified module could not be found," even though gdal204.dll was present at the expected location, with the right architecture (which I verified by adding asserts in the Python code and loading the DLL from a C program).
It turned out to be an issue with the Python 3.7 app from the Microsoft Store, the one that automatically installs the first time you type python on the command line.
This version of Python refuses to load the GDAL DLL; I don't know if it's a bug or a security feature.
I fixed the issue by:
uninstalling the Python app
removing the app execution aliases (as explained in the app description)
installing the regular Python package from python.org
Download GDAL wheel file that is supported for your platform from here.
Open the command window where the downloaded file is located and activate your virtual environment.
Then install the wheel using command pip install name_of_the_file .
You will see osgeo folder has been created in the location '...\Envs\my_django\Lib\site-packages\' .
Go to osgeo folder and copy the entire path of your gdalxxx.dll file and add to setting.py file as gdal library path. For example
GDAL_LIBRARY_PATH = r'C:\Users\WIN8\Envs\my_django\Lib\site-packages\osgeo\gdal300.dll'
Steps to follow:
Run python to check if your python is 32 or 64 bit.
Install corresponding OSGeo4W (32 or 64 bit) into C:\OSGeo4W or C:\OSGeo4W64:
Note: Select Express Web-GIS Install and click next.
In the ‘Select Packages’ list, ensure that GDAL is selected; MapServer and Apache are also enabled by default.
Make sure the following is included in your settings.py:
import os
GDAL_LIBRARY_PATH = r'C:\OSGeo4W\bin\gdal300'
Now, run the server still if it doesn't work. Run the following commands in terminal.
set OSGEO4W_ROOT=C:\OSGeo4W
set PYTHON_ROOT=C:\Python3X
set GDAL_DATA=%OSGEO4W_ROOT%\share\gdal
set PROJ_LIB=%OSGEO4W_ROOT%\share\proj
Just to follow up on the nice and detailed answer of Udi (cannot comment directly as my rep is under 50, it is the answer marked as the most useful);
After many hours I tried his offered solution which also did not work for me.
I was getting the following error:
OSError: [WinError 193] %1 is not a valid Win32 application
But I stayed there and found out that although I'm running 64 python and operating system (for sure), it kept looking for 32 bit (OSGeo4W) folder. What eventually let me pass was to copy contents of the OSGeo4W64 folder to the OSGeo4W. Hope it will save you time.
One more note:
Be sure that you edit libgdal.py file in your environment folder. It may exist in more than one place - your python folder and environment folder - if you edit the libgdal in your python dir, it is not going to work.
Creating the map compatible database
I recently got a new laptop and had to install Python and the related software on my new machine. I was trying to create a geodjango compatible database using OSGeo4W and had forgotten about the role of PostgreSQL and pgAdmin4.
When you install the latest PostgreSQL setup from https://www.enterprisedb.com/downloads/postgres-postgresql-downloads, you get pgAdmin4 at the same time.
Note - Ecommerce is my personal server group.
When you create a PostgreSQL 13 server, your server group will be different to mine.
When I first opened pgAdmin4 on my new machine, I kept the default username 'postgres'. If when you first logged into pgAdmin4 you chose your own username, you will have to change the values below accordingly.
Connecting to the database
In my code I had
if os.name == 'nt':
import platform
OSGEO4W = r"C:\OSGeo4W"
if '64' in platform.architecture()[0]:
OSGEO4W += "64"
assert os.path.isdir(OSGEO4W), "Directory does not exist: " + OSGEO4W
os.environ['OSGEO4W_ROOT'] = OSGEO4W
os.environ['GDAL_DATA'] = OSGEO4W + r"\share\gdal"
os.environ['PROJ_LIB'] = OSGEO4W + r"\share\proj"
os.environ['PATH'] = OSGEO4W + r"\bin;" + os.environ['PATH']
GDAL_LIBRARY_PATH = r'C:\OSGeo4W64\bin\gdal300.dll'
I was getting an error message regarding the GDAL_LIBRARY_PATH so checked Windows Explorer. On discovering I did not have a gdal300.dll file, I changed my code to
GDAL_LIBRARY_PATH = r'C:\OSGeo4W64\bin\gdal301.dll'
pip install GDAL
(From https://www.lfd.uci.edu/~gohlke/pythonlibs/)
Add In %PYTHON_ROOT%\Lib\site-packages\django\contrib\gis\gdal\libgdal.py
import osgeo
Check Gdal Vesion In: %PYTHON_ROOT%\Lib\site-packages\osgeo\gdal303.dll
lib_names = ['gdal303', 'gdal202', 'gdal201', 'gdal20', 'gdal111', 'gdal110', 'gdal19']
I want to use Blender 2.72b with OpenCV, so I had to build it for Python3 (I have 3.4.2 installed since Blender is using that too). I'm working on Win7 64bit, so I used a 64bit version of Python and NumPy (Blender too of course). For OpenCV I used 3.0.0-beta.
For building I used CMake 2.8 and Visual Studio 2010 Professional (since I have a student-license > Visual Studio 10 Win64 in CMake).
I followed these instructions (lower ones), modified for python3.
These are my folders:
Target build-folder for CMake (OpenCV.sln executed and build in here too of course):
C:\Users\Gunnar\Desktop\build\
OpenCV-source-folder:
C:\Users\Gunnar\Downloads\opencv\sources\
Python installed here:
C:\Python34\
The build went fine, I adjusted the following before generating with CMake (python2 left blank):
PYTHON3_EXECUTABLE -> C:/Python34/python.exe
PYTHON3_INCLUDE_DIR -> C:/Python34/include
PYTHON3_LIBRARY -> C:/Python34/libs/python34.lib
PYTHON3_NUMPY_INCLUDE_DIRS -> C:/Python34/Lib/site-packages/numpy/core/include
PYTHON3_PACKAGES_PATH -> C:/Python34/Lib/site-packages
BUILD_opencv_python3 -> true
The build-folder has a cv2.pyd at
C:\Users\Gunnar\Desktop\build\lib\Release\
The cv2.pyd got copied to
C:\Python34\Lib\site-packages\
automatically and I copied it to
M:\Programme\blender-2.72b-windows64\2.72\python\lib\site-packages\
If I now open the python IDLE, I get the following error (same in Blender of course):
>>> import cv2
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import cv2
ImportError: DLL load failed: Das angegebene Modul wurde nicht gefunden.
(in english: DLL load failed: Module not found.)
I'm assuming that I'm missing environment variables (from what I've found googling for it).
Looking into them I have no corresponding entry for PATH and no PYTHONPATH.
My problem now is that I don't know what I need to set there. I never really had to change something in there and I don't know what exactly is needed.
If more information are needed let me know.
EDIT:
I got i working now. I'm not quite sure, which one it did in the end, but I first used
cmd > setx -m OPENCV_DIR C:\Users\Gunnar\Desktop\build
and than edited the PATH-variable with these:
C:\Users\Gunnar\Desktop\build;
C:\Users\Gunnar\Desktop\build\lib\Release;
C:\Users\Gunnar\Desktop\build\x64\Release;
C:\Users\Gunnar\Desktop\build\bin\Release
And right now it works even though I removed all of them...
To load matplotlib on cygwin, I have:
Loaded pre-requisites using cygwin 64-bit setup: pkg-config, freetype2, libfreetype-devel, libpng-devel, gtk2.0, libgtk2.0-devel
Downloaded the matplotlib tar file (http://sourceforge.net/projects/matplotlib/files/matplotlib/matplotlib-1.3.1/matplotlib-1.3.1.tar.gz) and changed the source code to get around the "_tri" error as advised here:
matplotlib error while installing pyspeckit
Then built and installed matplotlib:
$ python setup.py build
$ python setup.py install
And am down to what looks like a matplotlib backend error. Does anyone know how to get around this:
/usr/lib/python2.7/site-packages/gtk-2.0/gtk/init.py:57: GtkWarning: could not open display
...
cursors.MOVE : gdk.Cursor(gdk.FLEUR),
et n RuntimeError: could not create GdkCursor object
EDIT: I just finally got it and matplotlib is finally working on cygwin. To do this, I did:
From cygwin setup, loaded the X-Server tools:
xorg-server xinit
From cygwin setup, I also loaded these so that use telnet or ssh connections to run remote X clients:
inetutils openssh
I set my display:
DISPLAY=":0.0"
export DISPLAY
From the cygwin shell, I did:
$ startxwin
Then I ran my python scripts which use matplotlib in the X-window
I had an issue with "python setup.py build" not finding the ft2build.h which comes from the the freetype2 package. I installed the freetype2 development package and I can find it in /usr/include/freetype2/ft2build.h but the error is still there.
After digging into the setupext.py at the function check_include_file(include_dirs, filename, package), I noticed that the package="freetype2" was not concatenated into to the search path. Hence it could not find "/usr/include/ft2build.h" when it should be "/usr/include/freetype2/ft2build.h
Fixing this line #134
if not has_include_file(include_dirs, "%s/%s"%(package,filename)):