ImportError: No module named qgis.core ubuntu 16.04 python 2.7 qgis 2.16.2
<p>I keep getting this error in python from trying to call qgis from a script.</p>

<p>The code is:</p>

<pre><code>from qgis.core import *
from qgis.analysis import *
</code></pre>

<p>I have read every posting on SO about this;  wiped QGIS and reinstalled.  Reset my PYTHON_PATH and QGIS_PREFIX variables to the correct directory.  I've also checked the dependencies via <code>dpkg -l | grep qgis</code>and all of my dependencies are the xenial version.</p>

<p>Any other suggestions?</p>

<p>I had the same problem but it was with Windows 7. Following the last point called Running Custom Applications in <a href="http://docs.qgis.org/2.8/en/docs/pyqgis_developer_cookbook/intro.html" rel="nofollow">http://docs.qgis.org/2.8/en/docs/pyqgis_developer_cookbook/intro.html</a> I solved it. </p>

<p>You will need to tell your system where to search for QGIS libraries and appropriate Python modules if they are not in a well-known location â otherwise Python will complain:</p>

<pre><code>&gt;&gt;&gt; import qgis.core
ImportError: No module named qgis.core
</code></pre>

<p>This can be fixed by setting the PYTHONPATH environment variable. In the following commands, qgispath should be replaced with your actual QGIS installation path:</p>

<p><b>on Linux: export </b>PYTHONPATH=/qgispath/share/qgis/python<br>
<b>on Windows: set</b> PYTHONPATH=c:\qgispath\python</p>

<p>The path to the PyQGIS modules is now known, however they depend on qgis_core and qgis_gui libraries (the Python modules serve only as wrappers). Path to these libraries is typically unknown for the operating system, so you get an import error again (the message might vary depending on the system):</p>

<pre><code>&gt;&gt;&gt; import qgis.core
ImportError: libqgis_core.so.1.5.0: cannot open shared object file: No such file or directory
</code></pre>

<p>Fix this by adding the directories where the QGIS libraries reside to search path of the dynamic linker:</p>

<p><b>on Linux: export </b>LD_LIBRARY_PATH=/qgispath/lib<br>
<b>on Windows: set </b>PATH=C:\qgispath;%PATH%<br></p>

<p>These commands can be put into a bootstrap script that will take care of the startup. When deploying custom applications using PyQGIS, there are usually two possibilities:</p>

<p>require user to install QGIS on his platform prior to installing your application. The application installer should look for default locations of QGIS libraries and allow user to set the path if not found. This approach has the advantage of being simpler, however it requires user to do more steps.
package QGIS together with your application. Releasing the application may be more challenging and the package will be larger, but the user will be saved from the burden of downloading and installing additional pieces of software.
The two deployment models can be mixed - deploy standalone application on Windows and Mac OS X, for Linux leave the installation of QGIS up to user and his package manager.</p>

python ubuntu qgis