Django & GDAL - Could not find the GDAL library - django

I have a macbook with the M1 Chip and I'm trying to set up a django project. The project works with Geospatial libraries gdal.
I installed gdal with homebrew on version 3.3.1_3 and inside my virtual env version 3.3.1
If I type python manage.py migrate I get an error:
django.core.exceptions.ImproperlyConfigured: Cloud not find the GDAL
library (tried "gdal", "GDAL", "gdal3.1.0", "gdal3.0.0", "gdal2.4.0",
"gdal2.3.0", "gdal2.2.0", "gdal2.1.0"). Is GDAL installed? If it is,
try settings GDAL_LIBRARY_PATH in your settings.
By pip list shows me GDAL at version 3.1.0. The Django Docs say version 3.3.x is not supported but I cant install a specific gdal version with homebrew.
Is there a different solution to get the django project up and running?

I happen to own new MacBook M1 Pro and facing some issue. Here are the steps I did that helped me resolved this issue.
Step 1: First check whether you have installed gdal and geos.
gdal-config --version
If no version shows up, then run brew install gdal in your terminal.
Step 2: Find out path of your gdal installation.
which gdal-config
This should return in case of M1 Mac OS -
/opt/homebrew/opt/gdal/bin
Step 3: Check if gdal lib file is present in below location.
/opt/homebrew/opt/gdal/lib/libgdal.dylib
If it does, then great! Do the same setps for geos if you require that as well.
Once all done, in your Django Project in Settings.py,
Add below lines.
GDAL_LIBRARY_PATH = '/opt/homebrew/opt/gdal/lib/libgdal.dylib'
GEOS_LIBRARY_PATH = '/opt/homebrew/opt/geos/lib/libgeos_c.dylib'
It should work now. Also if you have installed and using PostgreSQL it does come with GDAL and GEOS libs, you can also give those paths as well if needed.

Unfortunately the suggestions by #Ameya didn't work for me, but they did send me down the right path in my investigation. The root cause of my issue was related to python ctypes, from the pyenv distro that I use, not being able to find the GDAL shared library installed from homebrew. Below are some other explanations that I found during my investigation.
The following thread on python bugs gives some good background and detail into Apple's semi-recent(?) increased security constraints that were causing the underlying issue:
https://bugs.python.org/issue43964
This post on pyenv's github gives excellent detail on the issue and why pyenv does not have a general purpose solution:
https://github.com/pyenv/pyenv/issues/2339
Here is one way to test if you are having the same underlying issue that I did. Start a python shell and try to manually load the library with ctypes:
╰─➤ python3
Python 3.10.8 (main, Dec 5 2022, 14:59:49) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
--> from ctypes.util import find_library
--> find_library('libgdal.dylib')
If ctypes finds the library, a path is shown. If no lib is found, no response is given.
It appears that the ctypes loader now(?) only has a fixed set of paths on macos. The work-around that I used is to simply find one of the existing paths that was not used. I then created a symbolic link there, pointing to the homebrew libs location. For example ctypes appears to search $HOME/lib and /usr/local/lib, both of which were empty (or non-existent) on my machine. So I created a symbolic link as follows:
╰─➤ sudo ln -s /opt/homebrew/lib /usr/local/lib
or
╰─➤ ln -s /opt/homebrew/lib ~/lib
Now ctypes finds the library and as a result, django starts properly:
╰─➤ python3
Python 3.10.8 (main, Dec 5 2022, 14:59:49) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
--> from ctypes.util import find_library
--> find_library('libgdal.dylib')
'/usr/local/lib/libgdal.dylib'
-->

Related

Python 2.7 Opencv error, ImportError: DLL load failed: The specified module could not be found

Working on python 2.7(not anaconda). Operating system Windows server 2012 R2
The pypi installation mentions two fixes for this, installing Visual C++ redistributable 2015 and Universal C Runtime.
As well as a manual fix for older anaconda versions, which is for python3(I am not using anaconda version, and also my python version is 2.7 so presuming it doesn't apply in my case)
link here:
https://pypi.org/project/opencv-python/
installed both the dependencies but couldn't get it to work.
Any ideas on how I may fix this?
Since Python 2.7 is still supported, this could be the possible solution.
Installing OpenCV from prebuilt binaries:
Below Python packages are to be downloaded and installed to their default locations.
Python-2.7.x
Numpy
Matplotlib
Install all packages into their default locations. Python will be installed to C:/Python27/.
After installation, open Python IDLE. Enter import numpy and make sure Numpy is working fine.
Download latest OpenCV release and install it : Latest OpenCV-build
Goto opencv/build/python/2.7 folder.
Copy cv2.pyd to C:/Python27/lib/site-packeges.
Open Python IDLE and type following codes in Python terminal.
import cv2
print cv2.__version__

What's the best way to install two versions of python

I want a easy and clean installation, in case a need to uninstall in future.
I'm running Sierra on my mac, currently I have a Anaconda 2.4 (with python 2.7) and now I need to use python 3.5 too. I looked to quite a few answers and the process seemed a little tricky to un-do if necessary.
Please, if possible, post also the uninstall process. Thanks.
Don't let the long answer intimidate you, the process can be resumed to 6 lines (really), but I tried to write the answer that I was looking for.
The option that suit me the best is virtualenv.
Installation
The simplest way is using brew:
brew install pyenv pyenv-virtualenv pyenv-virtualenvwrapper
to check the list of python versions we can use the command:
pyenv install -l
since I am interested in 3.5.1, I can easily install with the same command
pyenv install 3.5.1
Setup
Once installed, we can check the result using
$ pyenv versions
* system
3.5.1 (set by /Users/macbookpro/.pyenv/version)
this command list all versions installed, and indicates that I'm currently using system's version.
Before we change to 3.5.1 version we need to add pyenv path to .bash_profile.
In the file .bash_profile :
add the following lines. Pay attention if the PATH variable is the same in your case.
#added for pyenv
export PATH="/Users/macbookpro/.pyenv:$PATH"
eval "$(pyenv init -)"
eval "(pyenv virtualenv-init -)"
So now we have everything setup, let's test it
First, we check which version is currently in use:
$ pyenv versions
* system
3.5.1 (set by /Users/macbookpro/.pyenv/version)
Ok, now we know we have to change to 3.5.1. We can do it using:
$ pyenv global 3.5.1
$ python
Python 3.5.1 (default, Oct 20 2016, 21:43:48)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
We also verified that this is actually working. We now test the system version too.
$ pyenv global system
$ pyenv versions
* system (set by /Users/macbookpro/.pyenv/version)
3.5.1
$ python
Python 2.7.12 (default, Sep 5 2016, 20:55:16)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Everything is working fine.
Uninstall
Simply :
$ rm -fr ~/.pyenv
And remove all the .bash_profile stuff that we added in setup.
Extras
For the Anaconda folk.
In jupyter :
In [1]: import sys
sys.version
Out [1]: '2.7.12 |Anaconda 4.1.1 (x86_64)| (default, Jul 2 2016, 17:43:17) \n[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)]'
Nothing changed.
The best way is to use virtualenv. It allows you to keep different working versions of python on the same machine without interfering with each other.
EDIT:
These are the instructions on how to install virtualenv on Mac OS.
Install the latest python version on your system:
brew install python (or python3)
Then using pip:
pip install virtualenv
Then in your home directory edit the .bashrc file. Create it if it doesn't exist. Add this line to the .bashrc file:
export PIP_REQUIRE_VIRTUALENV=true
The line above will make pip run only in virtual environment.
Apply the changes:
. .bashrc (if you in your home directory, note the dot and space before .bashrc file).
Now you should be able to use virtualenv.
If you in your project folder:
virtualenv some_venv
Then activate it:
. ./some_venv/bin/activate (note the dot).
Then you can install everything you need using pip. All the packages will go to the some_venv directory.
You want to deactivate the virtualenv, then run deactivate.
If you want to delete all the installed packages when you were under virutalenv, then just delete some_venv. That's it.
Regarding the pyenv. Honestly I have never used it. But there is an easy to follow tutorial on the github page of the project.
Hope it helps.

Not being able to run Theano on windows XP 32 with Anaconda

I’m trying to run Theano on windows XP 32 with Anaconda (Python 2.7). I installed Theano following the steps provided in the Anaconda section here: http://deeplearning.net/software/theano/install_windows.html. Everything went fine but when I try to run the script import theano I get this error message:
ImportError: DLL load failed: The specified procedure could not be found. Struggling quite a bit, could anyone please help?
I had the same issue. Now I can use Theano on Windows XP 32bits with Anaconda.
With dependency walker, we found when 'import theano', the error 'ImportError' caused by LIBSTDC++6.DLL(a g++ DLL) cannot import the non-exists symbol 'vsnprintf' in 'MSVCRT.DLL'.
The key to fix this issue is, replace the mingw installed by 'conda install mingw' (rev 4.7.0). Install mingw g++ (rev 4.9.3) with 'mingw-get-setup.exe' from sourceforge instead.
Make user you PATH environment has 'C:\MinGW\bin;C:\MinGW\lib;' (default install location) to use the correct g++.
Good luck!

Unable to import lib tiff after installing Anaconda on Mac OS X 10.9.2

I have developed some software using Python under Windows 7.
I have given it to a colleague to run on a Mac (OS X 10.9.2). I have never used a Mac and am having trouble helping them to get started. I have downloaded and installed Anaconda 1.9.2 on the Mac. According to the continuum documentation, libtiff is included, but when I run my python file using the Spyder IDE I get the following error when it tries to import libtiff:
ImportError: No module named libtiff.
Following one of the answers on Stack Ooverflow, I tried:
conda install libtiff
This runs and returns:
All requested packages already installed.
However on Windows 7 I can see a libtiff folder under \python27\lib\site-packages. On the Mac there is no libtiff folder under /lib/python2.7/site-packages.
Can anyone tell me what I am missing?
This question is answered here:
Installing Python modules with Anaconda or Canopy
If pip install libtiff does not work, you can download the source for PyLibTiff as directed at https://code.google.com/p/pylibtiff/source/checkout and run setup.py with whichever interpreter you would like for PyLibTiff to be installed to.
Also, you do not have to have the the C libraries that Anaconda installs installed for PyLibTiff to work if you have libtiff libraries installed elsewhere.
Unclear on this. But what you can do to begin with is to type echo $PATH from the terminal and see what paths are set. Unclear on how Anaconda interacts with the system, but a good hunch is that if the library file is not in a path then that would cause this.
Also, looking at this thread on Google Groups it seems that Anaconda installs it’s own libraries that might need to be symbolically linked into the main /usr/local/lib directory. The user Denis Engemann—who made the post—posts this bash script in the last response in the thread:
for lib in ~/anaconda/lib/*;
do
ln -s $lib /usr/local/lib/$(basename $lib);
done
I would recommend checking those two directories first before linking to make sure all is as expected.

How to install python-poppler-qt4 on windows

I would like to know how to install python-poppler-qt4 on windows.I have already installed python 2.7
,pyqt4 using window installer as well as SIP. However, when I tried to install python-poppler-qt4 through pip install, error was found :
import PYQT4.pyqtconfig
ImportError:No module named pyqt.config
From what I have understand, it is because I installed the binary packages for pyqt4 rather than from the source packages so this pyqt.config.Yet if I installed it from source, it mean that I would have to make use of MSVC or MinGW 32 bits?
How do I resolve this problem? Thank you