cannot import cv on OpenCV2.1 - python

I have downloaded OpenCV2.1 and Python2.6.
when i try to import cv using sys.path.append("C:\OpenCV2.1\Python2.6\Lib\site-packages") and then import cv, it tells me ImportError: DLLload failed..
I have tried the solutions given in previous thread to copy the two files in the above path to C:\Python26\Lib\site-packages, but i still can't import cv.
Is it because I missed out doing some steps after installing? What could be the reasons to why the system cannot find the module cv?

The OpenCV DLLs seem not to be found. Did you add OpenCV's bin directory to your PATH, "C:\OpenCV2.1\bin" in your case?
To use the Python wrappers
sys.path.append("C:\OpenCV2.1\Python2.6\Lib\site-packages")
import cv
should work, as well as copying cv.lib and cv.pyd to your C:\Python26\Lib\site-packages (assuming you installed Python to C:\Python26).
But those wrappers have to find the DLLs they should wrap. So make sure, they are in a path Python can find them.
Maybe this question Installing OpenCV on Windows 7 for Python 2.7 may help, especially Gia Thuy's journal post he mentions in his answer. Although he uses Python 2.7 and OpenCV 2.2, the procedure remains the same.

Related

How to import .Net Classes in Python using Python.Net

I'm using Python.NET to create wrapper for iText.net (https://github.com/itext/itext7-dotnet).
The dll is named itext.kernel.dll and the python script is in the samne folder of all the itext dlls.
With Jetbrain dotPeek I see that itext.kernel has various namespaces; the namespace iText.Kernel.Pdf is what I need, in particular the public class PdfReader.
Here's what I'm doing (from the docs on https://pythonnet.github.io/):
import clr
clr.AddReference('itext.kernel')
sys.path.append(os.getcwd()) # the script is currently in the dll folder
from iText.Kernel.Pdf import PdfReader
But this gets me a "ModuleNotFoundError: No module named 'iText'" error.
I can't understand what it's wrong, can someone point me to the right direction? Thanks.
edit: I can import and use iTextSharp just fine, it seems iText7 has something different.
Well, long story short I really wanted this to work... and solved my issue!
I tried using older builds, and I found out that build 7.0.8 worked fine.
I compared it with build 7.1.11 (the one I was having problems with) using dotPeek, and noticed it has some references, namely BouncyCastle.Crypto, Common.Logging and Common.Logging.Core.
I did put them in the project folder, but still no dice... until I noticed I was using BouncyCastle.Crypto build 1.8.6, while the itext.kernel referred build 1.8.5! Replacing with that version solved the issue.
Basically the newer builds of iText7 have 3 referenced libs, that must be present in the exact same version (at least to be imoported by pythonnet).
I hope this can help someone that has issues similar to mine.

Adding to Python Path DLLs

I am currently trying to use the camera module from Pygame, a set of Python modules designed for writing video games. Trying to initialize the camera gives the error
from VideoCapture import vidcap as vc
ModuleNotFoundError: No module named 'VideoCapture'
I am trying to resolve this error using the instructions from python pygame.camera.init() NO vidcapture, but I am not sure how to execute step 2 in the recommended solution, which says to add the corresponding version of dll from a downloaded program to "your python path"\DLLs. I understand this is a reference to the python system path, but am not sure what it means to add the dll.
"your python path" is the path of your python installation. DLLs is the directory DLLs under that very path.
So if your python installation is at (e.g.)
C:\Users\max.power\AppData\Local\Programs\Python\Python37-32
then the "your python path"\DLLs directory refers to
C:\Users\max.power\AppData\Local\Programs\Python\Python37-32\DLLs
Also note that the camera module is marked as experimental, only works with v4l2 cameras and only supports linux out of the box, so I don't know if the answer you linked works at all.

Is it possible to specify the search path for a module in a python script? If it is, how do I do that?

I have been coding in python for about 2 months, but I'm only familiar with basic object-oriented programming, so I do not really understand things like how searching for modules is implemented. (Basically I'm a noob.)
I pip installed a package called Opentrons Opentrons 2.5.2 and all its dependencies into the samefolder as a python script I'm currently writing. However when I tried to import the module below[1], I get an error saying that "Opentrons is not a module". Then, I tried shifting it into the python library because I found out the search path using the pprint module and it seems to work. I was wondering if I can specify the search path from the .py file itself instead of manually printing the search path and putting the file into the library that the script searches for. (Willing to put in images of the directories I put the opentrons package in if it helps.)
[1]
import sys
import pprint
pprint.pprint(search.path)
from opentrons import robot, containers, instruments
Edit: I realise that the fact that I am running all my scripts in a Spyder console located in a python 3.6 environment might be important.
You can try using the __import__ function, or importlib. This should allow you to specify the path.

Where do I download grass.scripts and/or grass.pygrass?

I am trying to learn how to do some scripting to run Grass externally with Python but I can't seem to even find the basic modules called for this type of script. All of the documentation I have seen describes using grass.scripts module but I don't know where to download this. I also saw some information on grass.pygrass but I can't find this either.
I thought that maybe it was builtin to a newer version of Grass so I just downloaded 7.0.0 beta2 and I still find grass.scripts files. Is this a python module like others (matploblib, numpy, scipy etc.) or is it internal to Grass? Sorry for the remedial questions but I am lost here.
I ran the following script (taken from http://grasswiki.osgeo.org/wiki/GRASS_and_Python with quotations added since I run Python 2.7)
GISBASE= 'C:\GRASS-64'
GISRC= 'C:\Documents and Settings\user\.grassrc6'
LD_LIBRARY_PATH= 'C:\GRASS-64\lib'
PATH= 'C:\GRASS-64\etc;C:\GRASS-64\etc\python;C:\GRASS-64\lib;C:\GRASS-64\bin;C:\GRASS-64\extralib;C:\GRASS-64\msys\bin;C:\Python26;'
PYTHONLIB= 'C:\Python26'
PYTHONPATH= 'C:\GRASS-64\etc\python'
GRASS_SH= 'C:\GRASS-64\msys\bin\sh.exe'
and it is fine (though I don't know what it does) but when I add
from grass.pygrass.modules import Module
it returns
ImportError: No module named gras.pygrass.modules
Normally I would download and install the module and the problem would be fixed but I can't find it anywhere.
I ran the following script (taken from http://grasswiki.osgeo.org/wiki/GRASS_and_Python with quotations added since I run Python 2.7)
GISBASE= 'C:\GRASS-64'
GISRC= 'C:\Documents and Settings\user\.grassrc6'
LD_LIBRARY_PATH= 'C:\GRASS-64\lib'
PATH= 'C:\GRASS-64\etc;C:\GRASS-64\etc\python;C:\GRASS-64\lib;C:\GRASS-64\bin;C:\GRASS-64\extralib;C:\GRASS-64\msys\bin;C:\Python26;'
PYTHONLIB= 'C:\Python26'
PYTHONPATH= 'C:\GRASS-64\etc\python'
I don't know why you think Python 2.7 requires you to add quotes. It doesn't.
But it does require you to replace all those Python26 with Python27. And that could easily be your problem. You don't have anything at C:\Python26.
And if you installed Grass somewhere other than C:\GRASS-64 you obviously need to change that as well. (From elsewhere in the file, I get the feeling that 64 refers to version 6.4, not to being 64-bits or something else, and you've downloaded 7.0.0, so I suspect it got installed to something like C:\GRASS-70.)
Or it could be this:
ImportError: No module named gras.pygrass.modules
If you import gras.pygrass.modules instead of grass.pygrass.modules, that's obviously not going to work.
Also, are you actually running that script in the same cmd.exe window that you use to start Python? If not, it's not going to help.
Meanwhile:
All of the documentation I have seen describes using grass.scripts module but I don't know where to download this.
The documentation you linked to answers that question, although it may not be obvious to a novice (once you figure this out, you might want to suggest documentation improvements upstream):
The related files are at $GISBASE/etc/python/grass/script/*.py.
On Windows, of course, that's $GISBASE isn't literally right; it's actually %GISBASE%, which is something like C:\GRASS-64, but it's the same thing. The files are already on your system; there is nothing to download. That's the whole point of setting PYTHONPATH to C:\GRASS-64\etc\python.
Is this a python module like others (matploblib, numpy, scipy etc.) or is it internal to Grass?
Well, it's not exactly "internal to Grass", but it's distributed with Grass, rather than as a separate module.
I'm still not sure I understand everything but I seem to be past this hurdle. I used the script from (https://gis.stackexchange.com/questions/89452/problem-with-python-script-to-control-grass-gis-from-outside-how-to-import-gra/90160#90160) and changed all the relevant path information to suite my installation and apparently, now the grass.script module is accessed. Here is my working script
import os
import sys
gisbase = os.environ['GISBASE'] = 'C:\program files\grass gis 6.4.3' #GISBASE needs to point the root of the GRASS installation directory
gisrc = 'C:\grassdata'
gisdbase = 'C:\grassdata'
location = 'newLocation'
mapset = 'TC'
LD_LIBRARY_PATH = 'C:\program files\grass gis 6.4.3\lib'
PATH = 'C:\program files\grass gis 6.4.3\etc';'C:\program files\grass gis 6.4.3\etc\python';'C:\program files\grass gis 6.4.3\lib';'C:\program files\grass gis 6.4.3\bin';'C:\Python27';'C:\program files\grass gis 6.4.3\Python27';'C:\program files\grass gis 6.4.3\msys'
PYTHONLIB = 'C:\Python27'
PYTHONPATH = 'C:\program files\grass gis 6.4.3\etc\python'
GRASS_SH = 'C:\OSGeo4W64\apps\msys\bin\sh.exe'
sys.path.append(os.path.join(os.environ['GISBASE'], 'etc', 'python'))
import grass.script as grass

RubyPython can't import nltk on OS X Lion

I've got a copy of Lion with all the dev tools installed. Both Python (2.7) and Ruby (1.8) are running just fine. I've installed the Natural Language Tool Kit for Python and tried it out in the Python interpreter and it works
import nltk
>>true
So that works. I've also installed the RubyPython gem, and it seems to work too, but it can't find the nltk module. It's possible I'm doing something wrong. In irb:
require "RubyGems"
require "rubypython" #both true
RubyPython.start # true
n = RubyPython.import "nltk" # RubyPython::PythonError: ImportError: No module named nltk
c = RubyPython.import "cPickle" # works!
RubyPython.stop
I can't figure this one out. The PythonError seems to indicate to me (because it's just a call to the direct C APIs) that the nltk module can't be found by any form of python. But the interpreter finds it just fine. RubyPython, however, cannot.
I've also tried forcing RubyPython to use python2.7 but no change.
What am I missing?
It's simple! For some reason, RubyPython was looking in the wrong place for my Python modules. This was verified by importing sys in both the RubyPython script and in Python, and comparing sys.path. I ended up fixing it by taking the path list of sys in the Ruby script and adding what was missing from the pure Python's path. Then I could load NLTK.

Categories