Why can't Pyright resolve this module properly? - python

I'm using a library that is installed in /opt/confd/src/confd/pyapi/.
My $PYTHONPATH is set to /opt/confd/src/confd/pyapi.
The output of python -c 'import sys; print(sys.path)' is
['', '/opt/confd/src/confd/pyapi', '/usr/lib/python39.zip', '/usr/lib/python3.9', '/usr/lib/python3.9/lib-dynload', '/home/kkovacs/.local/lib/python3.9/site-packages', '/usr/local/lib/python3.9/dist-packages', '/usr/lib/python3/dist-packages']
So I am pretty sure that pyright should be able to find the module. When I use python -i, I can import the module and use it just fine. However, running pyright like python -m pyright oss_confd_manager.py --verbose results in this (scroll to the right to see my annotations):
npx: installed 1 in 1.167s
No configuration file found.
pyproject.toml file found at /mnt/c/Users/kkovacsgamma01/home/oss_97542.
Loading pyproject.toml file at /mnt/c/Users/kkovacsgamma01/home/oss_97542/pyproject.toml
Assuming Python version 3.9
Assuming Python platform Linux
Auto-excluding **/node_modules
Auto-excluding **/__pycache__
Auto-excluding .git
stubPath /mnt/c/Users/kkovacsgamma01/home/oss_97542/typings is not a valid directory.
Search paths for /mnt/c/Users/kkovacsgamma01/home/oss_97542
/home/kkovacs/.npm/_npx/18315/lib/node_modules/pyright/dist/typeshed-fallback/stdlib
/mnt/c/Users/kkovacsgamma01/home/oss_97542
/mnt/c/Users/kkovacsgamma01/home/oss_97542/typings
/home/kkovacs/.npm/_npx/18315/lib/node_modules/pyright/dist/typeshed-fallback/stubs/...
/opt/confd/src/confd/pyapi <========================================================================================================================================== correct search path is here
/usr/lib/python3.9
/usr/lib/python3.9/lib-dynload
/home/kkovacs/.local/lib/python3.9/site-packages
/usr/local/lib/python3.9/dist-packages
/usr/lib/python3/dist-packages
Searching for source files
File or directory "/mnt/c/Users/kkovacsgamma01/home/oss_97542/confd-oss/python/--" does not exist.
Found 1 source file
... some output omitted ...
Could not import '_confd.dp' in file '/mnt/c/Users/kkovacsgamma01/home/oss_97542/confd-oss/python/oss_confd_manager.py'
Looking for typeshed stdlib path
Attempting to resolve using root path '/home/kkovacs/.npm/_npx/18315/lib/node_modules/pyright/dist/typeshed-fallback/stdlib'
Typeshed path not found
Looking in stubPath '/mnt/c/Users/kkovacsgamma01/home/oss_97542/typings'
Attempting to resolve stub package using root path '/mnt/c/Users/kkovacsgamma01/home/oss_97542/typings'
Attempting to resolve using root path '/mnt/c/Users/kkovacsgamma01/home/oss_97542/typings'
Looking in root directory of execution environment '/mnt/c/Users/kkovacsgamma01/home/oss_97542'
Attempting to resolve stub package using root path '/mnt/c/Users/kkovacsgamma01/home/oss_97542'
Attempting to resolve using root path '/mnt/c/Users/kkovacsgamma01/home/oss_97542'
Looking in python search path '/opt/confd/src/confd/pyapi'
Attempting to resolve stub package using root path '/opt/confd/src/confd/pyapi'
Attempting to resolve using root path '/opt/confd/src/confd/pyapi'
Resolved import with file '/opt/confd/src/confd/pyapi/_confd/__init__.py' <================================================================================================= it says it resolved this import...
Looking in python search path '/usr/lib/python3.9'
Attempting to resolve stub package using root path '/usr/lib/python3.9'
Attempting to resolve using root path '/usr/lib/python3.9'
Looking in python search path '/usr/lib/python3.9/lib-dynload'
Attempting to resolve stub package using root path '/usr/lib/python3.9/lib-dynload'
Attempting to resolve using root path '/usr/lib/python3.9/lib-dynload'
Looking in python search path '/home/kkovacs/.local/lib/python3.9/site-packages'
Attempting to resolve stub package using root path '/home/kkovacs/.local/lib/python3.9/site-packages'
Attempting to resolve using root path '/home/kkovacs/.local/lib/python3.9/site-packages'
Looking in python search path '/usr/local/lib/python3.9/dist-packages'
Attempting to resolve stub package using root path '/usr/local/lib/python3.9/dist-packages'
Attempting to resolve using root path '/usr/local/lib/python3.9/dist-packages'
Looking in python search path '/usr/lib/python3/dist-packages'
Attempting to resolve stub package using root path '/usr/lib/python3/dist-packages'
Attempting to resolve using root path '/usr/lib/python3/dist-packages'
Looking for typeshed path
Looking for typeshed stubs path
Typeshed path not found
/mnt/c/Users/kkovacsgamma01/home/oss_97542/confd-oss/python/oss_confd_manager.py
/mnt/c/Users/kkovacsgamma01/home/oss_97542/confd-oss/python/oss_confd_manager.py:6:8 - error: Import "oss_ns" could not be resolved (reportMissingImports)
/mnt/c/Users/kkovacsgamma01/home/oss_97542/confd-oss/python/oss_confd_manager.py:7:8 - warning: Stub file not found for "_confd" (reportMissingTypeStubs)
/mnt/c/Users/kkovacsgamma01/home/oss_97542/confd-oss/python/oss_confd_manager.py:8:8 - error: Import "_confd.dp" could not be resolved (reportMissingImports) <========================================== the import failed :(
/mnt/c/Users/kkovacsgamma01/home/oss_97542/confd-oss/python/oss_confd_manager.py:9:8 - error: Import "_confd.maapi" could not be resolved (reportMissingImports)
/mnt/c/Users/kkovacsgamma01/home/oss_97542/confd-oss/python/oss_confd_manager.py:19:25 - warning:
... more errors ...
3 errors, 48 warnings, 13 infos
Completed in 0.705sec
I'm not sure why this is happening. I suppose I don't fully understand how the modules are resolved. I thought that since the module is in the search path, pyright should be able to find it. Also, it's weird to me that I can import the module in the interactive interpreter, but pyright doesn't know what _confd.dp is.
How can I get pyright to properly detect this module and import it?

Related

ERROR when trying to compile protoc files: file not found or had errors

I am trying to compile protoc files using this command:
protoc/bin/protoc models/research/object_detection/protos/*.proto --python_out=.
but I am getting this output on cmd
object_detection/protos/flexible_grid_anchor_generator.proto: File not found.
object_detection/protos/grid_anchor_generator.proto: File not found.
object_detection/protos/multiscale_anchor_generator.proto: File not found.
object_detection/protos/ssd_anchor_generator.proto: File not found.
models/research/object_detection/protos/anchor_generator.proto:5:1: Import "object_detection/protos/flexible_grid_anchor_generator.proto" was not found or had errors.
models/research/object_detection/protos/anchor_generator.proto:6:1: Import "object_detection/protos/grid_anchor_generator.proto" was not found or had errors.
models/research/object_detection/protos/anchor_generator.proto:7:1: Import "object_detection/protos/multiscale_anchor_generator.proto" was not found or had errors.
models/research/object_detection/protos/anchor_generator.proto:8:1: Import "object_detection/protos/ssd_anchor_generator.proto" was not found or had errors.
models/research/object_detection/protos/anchor_generator.proto:14:5: "GridAnchorGenerator" is not defined.
models/research/object_detection/protos/anchor_generator.proto:15:5: "SsdAnchorGenerator" is not defined.
models/research/object_detection/protos/anchor_generator.proto:16:5: "MultiscaleAnchorGenerator" is not defined.
models/research/object_detection/protos/anchor_generator.proto:17:5: "FlexibleGridAnchorGenerator" is not defined.
So what can be the problem
Thank you
You need to run the protobuf compiler in the correct directory. In that case, it would be models/research:
$ cd models/research
$ ../../protoc/bin/protoc object_detection/protos/*.proto --python_out=.
The protobuf files will be compiled to python. In the directory object_detection/protos/, you should have python files named after the protobuf file (i.e <name_protobuf>_pb2.py).
There is relative imports in those protobuf files, so it's important that the protobuf compiler is run in the correct directory. You have a hint that it might be the error as the File not Found error message lists a different path than your current directory.
I faced this issue recently. I cleared it by changing the file paths in the proto file.
For example, a proto file had following imports.
import "object_detection/protos/flexible_grid_anchor_generator.proto";
import "object_detection/protos/grid_anchor_generator.proto";
import "object_detection/protos/multiscale_anchor_generator.proto";
import "object_detection/protos/ssd_anchor_generator.proto";
I changed it to the following.
import "flexible_grid_anchor_generator.proto";
import "grid_anchor_generator.proto";
import "multiscale_anchor_generator.proto";
import "ssd_anchor_generator.proto";
The reason is all the proto files are present in the same object_detection/protos folder so path is not required.
I changed the references in all the proto files and it worked.

Python: Appending system path to import the module

I am trying to import a module from the prgoram called 'Power factory" in Python. The folder where power factory file located looks as follow:
I have written a script to import the powerfactory module as follow:
import sys
sys.path.append("PAth of folder")
import powerfactory as pf
When I ran the above code, it throws the following error:
ImportError: DLL load failed while importing powerfactory: The specified module could not be found.
I copied the .dll file present in the power factory folder into the Python DLL folder but no luck. Could anyone help me where am I making the mistake?
Searching the net I found this (from here)
I am not able to import powerfactory module: DLL load failed Category:
Scripting
If an error message appears when importing the powerfactory module
stating “ DLL load failed: the specified module could not be found”,
this means that Microsoft Visual C++ Redistributable for Visual Studio
2012 package is not installed on the computer.
To overcome this problem the user should add the PowerFactory
installation directory to the os path variable within his python
script.
import os
os.environ["PATH"] = r'C:\Program Files\DIgSILENT\PowerFactory 2016;' + os.environ["PATH"]
Copy the .dll file from your digsilent folder, eg. Program Files\DIgSILENT\PowerFactory 2020 SP2A\Python\3.8\boost_python38-vc141-mt-x64-1_68.dll
and place the .dll file directly in your system!
Save it to your C:\Windows\System32 folder.
&
Save it also to your C:\Windows\SysWOW64 folder.
You should be good to go.

Python3.7 not showing up during module load on linux

I've installed python3.7 on my school's computing cluster without using pip or sudo. I added the python path to $PATH variable in the bashrc. But it still doesnt show the module when I try to do module spider python. Am I missing any steps?
Thanks in advance
To find a newly installed software through the module command, there should be a modulefile describing this software installation under an enabled modulepath.
Let's say /usr/share/modulefiles is a directory containing modulefiles. Create a python directory in this modulepath directory. Then create the 3.7 file in this directory:
#%Module
append-path PATH /the/path/to/your/python/3.7/installation
With this new file create in the modulepath directory, you have create a new modulefile: python/3.7.
Enable the modulepath directory:
$ module use /usr/share/modulefiles
Now you can see the python/3.7 module:
$ module av python
--------- /usr/share/modulefiles ---------
python/3.7
$ module show python/3.7
-------------------------------------------------------------------
/usr/share/modulefiles/python/3.7:
append-path PATH /the/path/to/your/python/3.7/installation
-------------------------------------------------------------------

Why python setup.py installed module cannot find the installed resources?

I've correctly installed my python module into /usr/lib/python2.7/site-packages/mymod.
However, when I attempt to run it with
python2 -m mymod
It runs only from /home/me/dev/mymod/mymod/ dir and fails if I do the same from any other directory.
IOError: [Errno 2] No such file or directory: 'mymod/data/icons/mymod.ico'
It has all the correct paths available to it:
/usr/lib/python2.7/site-packages/line_profiler-1.0-py2.7-linux-x86_64.egg
/usr/lib/python2.7/site-packages/textblob-0.11.1-py2.7.egg
/usr/lib/python2.7/site-packages/nltk-3.2-py2.7.egg
/usr/lib/python2.7/site-packages/pyCNN-0.0.0-py2.7-linux-x86_64.egg
/usr/lib/python27.zip
/usr/lib/python2.7
/usr/lib/python2.7/plat-linux2
/usr/lib/python2.7/lib-tk
/usr/lib/python2.7/lib-old
/usr/lib/python2.7/lib-dynload
/usr/lib/python2.7/site-packages
/usr/lib/python2.7/site-packages/gst-0.10
/usr/lib/python2.7/site-packages/gtk-2.0
/usr/lib/python2.7/site-packages/wx-3.0-gtk2
Why can't it find mymod/data/icons/mymod.ico inside /usr/lib/python2.7/site-packages when it is there. I tried using different paths in mymod.py with mymod/data/icons/ and with data/icons but nothing helps.
$ file /usr/lib/python2.7/site-packages/mymod/data/icons/mymod.ico
/usr/lib/python2.7/site-packages/mymod/data/icons/mymod.ico: MS Windows icon resource - 1 icon, 128x128
This issue has been plaguing all my python projects with setup.py and I think that there is something I clearly misunderstand about how python modules should be run.
I somehow think that your local reference is messing things up.
If you start python -m module from another directory, the relative file reference may still be interpreted to be relative to your working directory, not to the module which requires the file.
Try to reference your module-local file as follows and see whether it solves the problem:
from os import path
datadir = path.join(path.dirname(__file__), 'data')
icofile = path.join(datadir, 'icons', 'mymod.ico')
As proposed in this answer.

Building opencv with mingw. Fails when I import cv2. DLL load failed. Looking for .dll, but I have .dll.a's

I'm trying to build OpenCV with MSYS / MinGW so I can use the cv2 module in python.
I'm on Windows 7 64-bit and using 32 bit Python 2.7. Building OpenCV works, but I cannot seem to use it without getting an "ImportError: DLL load failed: The specified module could not be found." after importing cv2. I've been debugging it for awhile, and the common google solutions seem not to work.
These are the steps I have taken:
Removed all other instance of opencv from my computer (there were a few)
Built opencv with CMake and I manually set these flags:
'CMAKE_INSTALL_PREFIX' : 'C:/Program Files (x86)/OpenCV'
'CMAKE_BUILD_TYPE' : 'Release',
'CMAKE_C_FLAGS' : '-m32',
'CMAKE_CXX_FLAGS' : '-m32',
'BUILD_opencv_gpu' : False,
'BUILD_opencv_gpuarithm' : False,
'BUILD_opencv_gpubgsegm' : False,
'BUILD_opencv_gpucodec' : False,
'BUILD_opencv_gpufeatures2d' : False,
'BUILD_opencv_gpufilters' : False,
'BUILD_opencv_gpuimgproc' : False,
'BUILD_opencv_gpuoptflow' : False,
'BUILD_opencv_gpustereo' : False,
'BUILD_opencv_gpuwarping' : False
I do a cmake -G "MSYS Makefiles" ..., make -j9, and finally a make install. The printouts show that the libraries go where I expect them to.
My PATH includes
C:\MinGW\bin
C:\MinGW\msys\1.0\bin
C:\MinGW\libexec\gcc\mingw32\4.6.2
C:\Python27\
C:\Python27\Scripts
C:\Program Files (x86)\OpenCV
C:\Program Files (x86)\OpenCV\lib
My PYTHONPATH includes
C:\Python27\Lib\site-packages
C:\Python27
I then copy
cv2.pyd and libcv2.dll.a from C:\Python\Lib\site-packages
all libopencv_*249.dll.a from C:\Program Files (x86)\OpenCV\lib
libgcc_s_dw2-1.dll libstdc++-6.dll from C:\MinGW\bin
I MOVE ALL OF THESE into a directory I named cv2
In the cv2 directory I made an __init__.py file containing the following code:
import os, sys
from os.path import realpath, dirname
tpl_cv2 = realpath(dirname(__file__))
print(tpl_cv2)
sys.path.insert(0,tpl_cv2)
os.environ['PATH'] = tpl_cv2 + os.pathsep + os.environ['PATH']
try:
from cv2 import *
except Exception as ex:
print(repr(ex))
print(os.environ['PATH'])
print(sys.path)
raise
I then open a IPython terminal and enter the command "import cv2"
And I get the error: ImportError('DLL load failed: The specified module could not be found.',)
To debug this I've:
I load up the cv2.pyd file in depends.exe It shows that
LIBOPENCV_CALIB3D249.DLL and all the other opencv libs are not found. However, these are all in the same directory as cv2.pyd as well as in both the PATH and PYTHONPATH.
But they are named .dll.a because I compiled with MSYS / MinGW. I don't understand why its looking for the .dll without the .a suffix. I think this probably has something to do with the error, but I don't know where to go from here.
I've also built a small C++ application which reads and image using the OpenCV that I've built. It builds just fine, but I get the error: The program can't start because libopencv_core249.dll is missing from your computer. So, I'm more convinced this .dll.a thing is the issue.
I've tried simply renaming the libs from .dll.a to .dll, but that didn't work.
I'm hoping someone can shed light on this issue.
I feel really stupid. The dlls were in "C:\Program Files (x86)\bin" not "C:\Program Files (x86)\lib" It seems to work now.
This post helped me a lot. The answer I found was to make sure the compiled bin files were part of my PATH variable.
My CMAKE_INSTALL_PREFIX was C:\opencv\src\build\install
and adding C:\opencv\src\build\install\x86\vc11\bin to my PATH variable made cv2 start working.
Just to make sure other users can be helped with this answer:
Imagine you have compiled OpenCV and have several *.dll and the cv2.pyd file.
You need to copy those files to 'DLLs' folder within the python directory.
Then import the module to check wether it is ok.
I have also copied the *.lib files into the appropriate folder.
Best regards.

Categories