Sphinx link to python standard library documentation - python-3.x

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?

Related

pyQgis processing algortihm

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,

How Can I Import A Python 3 Module With A Period In The Filename?

What is the proper way to import a script that contains a period, such as program_1.4.py, ideally using importlib?
(Now that the imp module is deprecated, this this answer no longer applies: How to reference python package when filename contains a period .)
After looking through the CPython quite a lot and coming back to some other solutions (especially Import arbitrary python source file. (Python 3.3+)), I realized that I needed to pass the full path to my module. Here is the cross-platform, call-location-independent solution:
"""
import os, sys # For running from Notepad++ shortcut, etc
import importlib.machinery
program_1_4 = importlib.machinery.SourceFileLoader('program_1.4', os.path.join(sys.path[0], 'program_1.4.py')).load_module()
print(program_1_4)
program_1_4.main()
"""

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.

How to implement a Meta Path Importer on Python 3

I'm struggling to refactor some working import-hook-functionality that served us very well on Python 2 the last years... And honestly I wonder if something is broken in Python 3? But I'm unable to see any reports of that around so confidence in doing something wrong myself is still stronger! Ok. Code:
Here is a cooked down version for Python 3 with PathFinder from importlib.machinery:
import sys
from importlib.machinery import PathFinder
class MyImporter(PathFinder):
def __init__(self, name):
self.name = name
def find_spec(self, fullname, path=None, target=None):
print('MyImporter %s find_spec fullname: %s' % (self.name, fullname))
return super(MyImporter, self).find_spec(fullname, path, target)
sys.meta_path.insert(0, MyImporter('BEFORE'))
sys.meta_path.append(MyImporter('AFTER'))
print('sys.meta_path:', sys.meta_path)
# import an example module
import json
print(json)
So you see: I insert an instance of the class right in front and one at the end of sys.meta_path. Turns out ONLY the first one triggers! I never see any calls to the last one. That was different in Python 2!
Looking at the implementation in six I thought, well THEY need to know how to do this properly! ... 🤨 I don't see this working either! When I try to step in there or just put some prints... Nada!
After all:IF I actually put my Importer first in the sys.meta_path list, trigger on certain import and patch my module (which all works fine) It still gets overridden by the other importers in the list!
* How can I prevent that?
* Do I need to do that? It seems dirty!
I have been heavily studying the meta_path in Python3.8
The entire import mechanism has been moved from C to Python and manifests itself as sys.meta_path which contains 3 importers. The Python import machinery is cleverly stupid. i.e. uncomplex.
While the source code of the entire python import is to be found in importlib/
meta_path[1] pulls the importer from frozen something: bytecode=?
underscore import is still the central hook called when you "import mymod"
--import--() first checks if the module has already been imported in which case it retrieves it from sys.modules
if that doesn't work it calls find_spec() on each "spec finder" in meta_path.
If the "spec finder" is successful it return a "spec" needed by the next stage
If none of them find it, import fails
sys.meta_path is an array of "spec finders"
0: is the builtin spec finder: (sys, _sre)
1: is the frozen import lib: It imports the importer (importlib)
2: is the path finder and it finds both library modules: (os, re, inspect)
and your application modules based on sys.path
So regarding the question above, it shouldn't be happening. If your spec finder is first in the meta_path and it returns a valid spec then the module is found, and remaining entries in sys.meta_path won't even be asked.

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