Python: Appending system path to import the module - python

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.

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.

'System.IO.FileNotFoundException: Unable to find assembly' in clr library on Python

I am getting an assembly error in my code using in a .dll file. This is a template code from another thread, and a lot of people claimed that it worked.
import clr
import os
file = 'CPUThermometerLib.dll'
print('Does this filepath exist?',os.path.isfile(file))
clr.AddReference(file)
There is no problem with the file path I guess since it return true on the .isfile function. Here is the output that I'm getting:
Does this filepath exist? True
File "<stdin>", line 1, in <module>
System.IO.FileNotFoundException: Unable to find assembly 'CPUThermometerLib.dll'.
at Python.Runtime.CLRModule.AddReference(String name)
I have checked multiple threads and none of them is giving a solution. I am using Windows 10, and moreover, my .NET framework version is '4.0.30319.42000'. My laptop processor is an Atom Z3537F.
The clr module is not installed by default on the Windows python installation. You will need to run pip install clr at a prompt to have it added and then your code will be able to import it properly.
You need to provide the full path to the dll file, not just the file name.
If the dll file is in the same directory as your Python script, you can get the full path by using os.path.abspath('CPUThermometerLib.dll').
Full code:
import clr
import os
file = 'CPUThermometerLib.dll'
print('Does this filepath exist?', os.path.isfile(file))
clr.AddReference(os.path.abspath(file))

Python and C++ (Pybind11): Python Extension Module Import Error

I try to wrap a C++ - function with pybind to create a python-module. After compiling with Visual Studio I accomplished to create a .pyd file.
I tried to import this file (python>>> import pymodule) but get this error:
Import Error: dynamic module does not define module export function (PyInit_pymodule)
Is it usually the right approach to import a .pyd file the way I explained above? Why do I get this error?
1a) You have to copy the .pyd file into the Python-installation folder (Lib-> side packages).
1b) You can open Python in the same folder where the .pyd - file is placed.
2) the python module must have the same name as the directory defined in CMake

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.

Creating and using of procname.so

I would like to edit the process name of my python scripts by the use of http://code.google.com/p/procname/
I downloaded the source file of procname, extracted the content into my script directory and changed the makefile to:
PYTHON?=python3
TESTFLAGS=-p -v
TESTOPTS=
SETUPFLAGS=
GCC=gcc
VER=3.1
DESTDIR=.
Then I did make and procname.so was created in the same directory where my script is.
In the script I added import procname and tried to edit the process name by procname.setprocname('test_name.py').
Now i get an error:
import procname
ImportError: /path/to/script_directory/procname.so: undefined symbol: Py_InitModule3
The procname extension module does not work in Python 3. procnamemodule.c uses Py_InitModule3, which no longer exists in Python 3.

Categories