pyQgis processing algortihm - python-3.x

import sys
sys.path.append("c:\\osgeo4w64\\app\qgis\\pyto\\plugins")
import processing, os,glob
layer1 = QgsVectorLayer(layer1ShpFilePath, "layer1", "ogr")
layer2 = QgsVectorLayer(layer2ShpFilePath, "layer2", "ogr")
params = {
'INPUT': layer1,
'OVERLAY': layer2,
'OUTPUT': "TEST.shp"
}
intersectLayer = processing.run("saga:intersect", params)
I wanted to use pyQgis to find out the intersect polygon between 2 vector layer, but I always encountered error
AttributeError: module 'processing' has no attribute 'run'
Can anyone please help me? I am writing a python using QGis on a window machine, I have already added the basic QGIS path to the environment variables, but I am no sure if I need to add SAGA package to the window environment variables.

had the same problem - it maybe because your import path in sys.path.append is incorrect (I see a typo in your post).
To check your installation you can type sys.path in qgis console:
Python Console
Use iface to access QGIS API interface or Type help(iface) for more info
Security warning: typing commands from an untrusted source can harm your computer
import sys
sys.path
['/usr/share/qgis/python', '/home/<username>/.local/share/QGIS/QGIS3/profiles/default/python', '/home/<username>/.local/share/QGIS/QGIS3/profiles/default/python/plugins', '/usr/share/qgis/python/plugins', '/usr/lib64/python39.zip', '/usr/lib64/python3.9', '/usr/lib64/python3.9/lib-dynload', '/home/<username>/.local/lib/python3.9/site-packages', '/usr/local/lib/python3.9/site-packages', '/usr/lib64/python3.9/site-packages', '/usr/lib/python3.9/site-packages', '/home/<username>/.local/share/QGIS/QGIS3/profiles/default/python']
All the best,

Related

Why do I have to import a library twice in Python (IDLE and the imported file)?

I am running Python 3.7.6 shell and have the library numpy installed correctly.
In my shell I type:
import numpy as np
and can use numpy however I desire. I then proceed to import 'my_lib.py' which contains:
def softmax(x):
e_x = np.exp(x - np.max(x))
return e_x / e_x.sum(axis=0)
In my shell I can call the function softmax(x) but I immediately get the error
NameError: name 'np' is not defined
My hypothesis here would be I've imported numpy into 'shell scope' and i've also imported softmax(x) into 'shell scope' so everything should be happy. To fix this problem I have to add
import numpy as np
into 'my_lib.py'.
How come I have to import numpy twice?
The code in each module can only use identifiers (names) that have be defined in or imported into that module. The global dict in each module only contains names global to that module. It might better be called the module dict or modular dict, but the name goes back to when there were no modules in computing.
You might benefit from reading https://docs.python.org/3/tutorial/modules.html and probably elsewhere in the tutorial.
(None of this has anything to do with the editor you use to write code or the IDE or shell you use to pass code to Python.)

Sphinx link to python standard library documentation

In order to be able to reference standard python documentation I have added to my config file the following:
import os
import sys
sys.path.insert(0, 'C:/ProgramData/Anaconda3/lib/site-packages')
sys.path.insert(0, os.path.abspath('../..'))
master_doc = 'index'
extensions = ['sphinx.ext.intersphinx']
intersphinx_mapping = {'python': ('https://docs.python.org/3.6', None)}
I have a rst file that makes reference to a python function as:
See :py:func:`io.open`.
When the documentation is built, it correctly recognizes io.open as an external function, creates a link, and when I pass the mouse over it shows the message (in Python 3.6), so I believe it is somehow working.
However, the link that it uses is:
file:///C:/mylib/docs/build/python/library/io.html#io.open
instead of:
https://docs.python.org/3.6/library/io.html#io.open
Am I missing something extra in the config? What am I doing wrong?

how to use "Type Hints - PEP 484" when creating new objects in python 3.7 to be able code completion in pycharm 2019.2.4

system: fedora 31
Pycharm doesn't get me auto completion when type my_var_hash. , there is no problem with venv.
How use Type-Hints with modules like hashlib and that way can we use auto complete options (CTRL+space).
Example of code bellow but auto completion doesn't work.(functions: my_var_hash.update(b'lmao') , my_var_hash.hexdigest() ).
import hashlib
my_var_hash = hashlib.sha256()
print(my_var_hash)
my_var_hash.update(b'lmao')
print(my_var_hash.hexdigest())
print(type(my_var_hash.hexdigest()))
the problem is that hashlib.sha256() does not have type defined or some docstring (when using standard cptyhon)
PyCharm therefore assigns it the "Any" type that does not have any extra info
This is a library issue, not a problem with your setup

NameError: name 'log10' is not defined in function called in script

Why log10() is failing to be recognized when called within a function definition in another script? I'm running Python3 in Anaconda (Jupyter and Spyder).
I've had success with log10() in Jupyter (oddly without even calling "import math"). I've had success with defining functions in a .py file and calling those functions within a separate script. I should be able to perform a simple log10.
I created a new function (in Spyder) and saved it in a file "test_log10.py":
def test_log10(input):
import math
return math.log10(input)
In a separate script (Jupyter notebook) I run :
import test_log10
test_log10.test_log10(10)
I get the following error:
"NameError: name 'log10' is not defined"
What am I missing?
Since I'm not using the environment of Jupyther and alike, I don't know how to correct it in these system, perhaps there is some configuration file over there,check the documentation.
But exactly on the issue, when this happens its because python has not "linked" well something at the import, so I suggest a workaround with the libs in the next way:
import numpy as np
import math
and when you are using functions from math, simply add the np. before, i.e.:
return math.log10(input)
to
return np.math.log10(input)
Exactly I don't know why the mismatch, but this worked for me.

seeing source code of objects in a python module

I came up with this question when I was trying to use pygame. I wrote the following line
pygame.time.
but pyCharm didn't give me a list of methods to choose. I wanted to use pygame.time.Clock() but when this happened I tried to see the source code of time but I couldn't. I was just able to see the source code of pygame module and in that, there was just the following line on 'time':
try:
import pygame.time
except (ImportError, IOError):
time = MissingModule("time", geterror(), 1)
So my question is that, what is 'time' object and where is it? is it just a compiled python file that came with pygame when I installed it? Can I see the methods inside it or is there a way to let pyCharm suggest the methods inside of it?
So my question is that, what is 'time' object and where is it?
time is not an object it is a module.
Pygame is well documented. A complete documentation of the pygame.time module can be found at pygame.time.
It is not necessary to import the module. Via pygame.time you can access all objects of the module. However, you can import all objects form the module with:
from pygame.time import *

Resources