import QGIS modules into Python (Anaconda) - python

control QGIS from within Anaconda's Spyder.
I set PAYTHONPATH to C:\Program Files\QGIS Pisa\apps\qgis\bin, but it still gives this error while import qgis.core module:
import qgis.core
ImportError: No module named qgis.core
How can I import the module?

The answer provided by j08lue works for me. But we also could do this in an Anaconda virtual environment in a specific environment-wide way. So, please try the following steps:
Create a conda environment using conda create -n conda-qgis and then activate this new environment by using conda activate conda-qgis.
Install QGIS through conda-forge in the current environment using conda install -c conda-forge qgis.
Open QGIS by running qgis.
Use the Python console in QGIS GUI, and run:
import sys
sys.path
and you might get system paths like below:
'C:/Anaconda3/envs/conda-qgis/Library/./python', 'C:/Users/Guohan/AppData/Roaming/QGIS/QGIS3\\profiles\\default/python', 'C:/Users/Guohan/AppData/Roaming/QGIS/QGIS3\\profiles\\default/python/plugins', 'C:/Anaconda3/envs/conda-qgis/Library/./python/plugins', 'C:\\Anaconda3\\envs\\conda-qgis\\Library\\python', 'C:\\Anaconda3\\envs\\conda-qgis\\Library\\python\\plugins', 'C:\\', 'C:\\Anaconda3\\envs\\conda-qgis\\python39.zip', 'C:\\Anaconda3\\envs\\conda-qgis\\DLLs', 'C:\\Anaconda3\\envs\\conda-qgis\\lib', 'C:\\Anaconda3\\envs\\conda-qgis\\Library\\bin', 'C:\\Anaconda3\\envs\\conda-qgis', 'C:\\Anaconda3\\envs\\conda-qgis\\lib\\site-packages', 'C:\\Anaconda3\\envs\\conda-qgis\\lib\\site-packages\\win32', 'C:\\Anaconda3\\envs\\conda-qgis\\lib\\site-packages\\win32\\lib', 'C:\\Anaconda3\\envs\\conda-qgis\\lib\\site-packages\\Pythonwin', 'C:/Users/Guohan/AppData/Roaming/QGIS/QGIS3\\profiles\\default/python'
Copy all the paths above and get back to the command prompt and run:
conda-develop PASTEHERE -n conda-qgis
This will create a conda.pth file at the site-package directory, which stores all the environment path variables specified for this conda-qgis environment.
Finally, you should be able to use import qgis in an Anaconda environment.

The Python packages shipped with QGIS live in \path\to\QGIS\apps\Python27\Lib. So you need to add that to PYTHONPATH, rather than ...\qgis\bin.
It is best to do this on script-basis, rather than system-wide, like so:
import sys
sys.path.append("C:\Program Files\QGIS Pisa\apps\Python27\Lib")
import qgis.core
But be aware that the QGIS Python packages were likely built for a different version of Python. So things might not work smoothly.
Note: QGIS Python plugins are installed here: ~\.qgis2\python\plugins, so you might need to sys.path.append that too.

Related

Issue with Anaconda package manager - library installed, but not found in Spyder

I installed cairo using Anaconda Navigator (in case it's important, cairo is also installed in the other two environments: miniconda3 and spyder-env):
I launch Spyder from Anaconda Navigator, and try to import cairo, and get an error saying:
ModuleNotFoundError: No module named 'cairo'
Spyder seems to be using the correct python environment. If I run `conda list cairo', I get the following output:
What am I doing wrong?
I had problem similar to this with VaderSentiment analysis library. Jupyter notebook couldn't find it although it was installed. Either you can use a more stable Python version such as Python 3.7 for compatibility or you can use importlib.
conda install python=3.7
I solved it using importlib This does not answer your question "what am i doing wrong", but it solved the problem in my case. Just locate cairo.py in your Anaconda folder whatever the environment is. You can make necessary adjustments. I hope this helps if you have emergency to use that package.
import importlib.util
import sys
spec = importlib.util.spec_from_file_location("vaderSentiment", r"C:\Users\matt\Anaconda3\envs\sentiment\Lib\site-packages\vaderSentiment\vaderSentiment.py")
foo = importlib.util.module_from_spec(spec)
sys.modules["vaderSentiment"] = foo
spec.loader.exec_module(foo)

I couldn't import or use tdclient library

I got an error with this code:
from td.client import TDClient
Error message:
ModuleNotFoundError: No module named 'tdclient'
and I installed tdclient through the command line:
pip install td-client
and it was successfully installed.
If you are using virtual environments (venv), ensure you installed the module in the right venv.
If you are indeed using the right virtual env, or you're not using virtual environments at all, there may be a typo in the import statement.

Cannot import package in conda environment (but works fine in base env)

I am running python in a jupyter notebook. I have imported a package (pymatsolver) to a new conda environment. When I try to import Pardiso from this package, i get ImportError: cannot import name 'Pardiso' from 'pymatsolver' Which is odd, because it works fine in my base environment. Other imports are functioning correctly. Pymatsolver is there when I type conda list.

New to anaconda and spyder. using different sys.executables

For some reason when I install a package through Anaconda, it is not available in Spyder. When I execute the following command in anaconda and in spyder I get different files.
Anaconda:
import sys; sys.executable
'C:\\Users\\onp1ldy\\AppData\\Local\\conda\\conda\\envs\\deeplearning\\python.exe'
Spyder:
import sys; sys.executable
'C:\\Users\\onp1ldy\\AppData\\Local\\conda\\conda\\envs\\deeplearning\\pythonw.exe'
Can anyone help me with this? I am not sure what to do...
By Default: You can control which one of the executables will run your script. Such as, when opened from Explorer by choosing the right file_name like:
1. python.exe is a terminal-based (console) application to run and lunch CLI-Type Python-scripts.
*.py files are by default associated (invoked) with python.exe
2. pythonw.exe is a GUI-based app for lunching Graphical User interface-(No_UI_at_all_Scripts)
*.pyw files are by default associated (invoked) with pythonw.exe
TO SUMMARIZE AND COMPLEMENT MY OPINION:
First of all, Python-Binary that you're trying to run doesn't have Package installed. It does have a directory path named as package_name like torch at the search path of modules, and it is being treated as a Package namespace like for me:
torch.Tensor(5, 3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'torch' has no attribute 'Tensor'
For your current python binary, you need to install your package correctly. Visit Reference: Home-Page
python3.7 -m pip install http://download.pytorch.org/whl/cu80/torch-0.2.0.post3-cp35-cp35m-manylinux1_x86_64.whl
python3.7 -m pip install torchvision
More importantly, replace the pip or pip3 as the Home-Page instructions use with python3.7 -m pip;. In the end, don't forget to include python3.7 to be the full path to your Python binary.
Run this in spyder and see if you can access the package.
import subprocess
subprocess.call('pip install numpy', shell=True)
import numpy
based on your description the problem may be connected to the Python interpreter that you are using in Spyder. There is a similar issue on Stack Overflow at this url:
Issue on Anaconda and Spyder
You can try the solution proposed by Bremsstrahlung.
Hope this can help you.

Conda openpxyl install results in No module named 'openpyxl'

I installed several conda environments with different Python versions.
After activating my Python 3.5 environment I have installed openpxyl via the line given here: https://anaconda.org/anaconda/openpyxl
However, when I try to import I get
ImportError: No module named 'openpxyl'
When I type conda list I do see
openpyxl 2.4.7 py35_0 anaconda
in the list.
(code to import is just import openpyxl or from openpyxl import *)
Any suggestions? Thanks!
You can try deleting all openpyxl material then reinstall it using pip.
that's how i solved my problem at least.
Even I faced the Same issue. This might Help you. If we have given python path in the windows environment variables which is present before we installed anaconda. We can test it by using
python --version
It will give you the version if the path is already present as
Python 3.6.5
Please remove this path of python which is already mentioned.
Then give the path of the python which is is anaconda directory. Normally it will in the path
C:\Users\lenovo\Anaconda3\
Just add this path to environment variables and again check for the python version
that might give you the following result
Python 3.6.5 :: Anaconda, Inc.
then it is confirmed that you are using the python which is downloaded using anaconda package manager.
Now you use the conda modules anywhere.

Categories