Importing opencv with Python - python

using this post: How to install Python 2.7 bindings for OpenCV using MacPorts for reference I installed opencv and numpy with
sudo port install numpy
sudo port install opencv +python27
This seemed to work, but if I do
import cv
in a python file and try to run it, I get this error:
Traceback (most recent call last):
File "test.py", line 1, in <module>
import cv
ImportError: No module named cv
That post is a couple years old, so I wonder if it might be outdated, or more likely I just don't know what I'm doing.
A little more info. If I run
port installed opencv
I get
The following ports are currently installed:
opencv #2.4.6_0+python27 (active)
So it looks like it's installed (?)

I uninstalled opencv, then did
sudo port install python27
sudo port install opencv +python27
sudo port select --set python python27
and it works

Related

Pytorch is installed but is not working on ubuntu 18.04

I am trying to install Pytorch via pip on ubuntu 18.04.I have python 3.6 and my laptop is HP-Pavilion notebook 15
The installation seems to be right because i get the message:
Installing collected packages: torch, torchvision Successfully
installed torch-1.3.1+cpu torchvision-0.4.2+cpu
i run the verification code and it is ok
from __future__ import print_function
import torch
x = torch.rand(5, 3)
print(x)
However, when i close the terminal or reboot and try to run he same code i get the error:
Traceback (most recent call last):
File "torch.py", line 2, in
import torch
AttributeError: module 'torch' has no attribute 'rand'
How are you executing the python script? Which python are you using? Maybe you installed the package in a different python version?
Try to set alias to the python you want to use:
alias python=/usr/local/bin/python3.6
Then pip install the package with that python alias you will always be using.
python pip install <package name>
Python now will install the package in the python files with the alias python - heading to files: /usr/local/bin/python3.6
Let me know if the error still occurs!
Install pytorch using pip through the below command:
pip3 install torch==1.3.1+cpu torchvision==0.4.2+cpu -f https://download.pytorch.org/whl/torch_stable.html
for any reference go through the official website of pytorch.
Change your file .py to another name, you named torch.py when you import torch it will call ur torch.py

ImportError: No module named configobj

I created .exe file using pyinstaller 3.3.1 and I got this message
Traceback (most recent call last):
File "install.py", line 14, in <module>
ImportError: No module named configobj
[8468] Failed to execute script install
I'm working with python 2.7 using conda environment. Lately I had updated pyinstaller to version 3.3.1 so this happened to me while I'm checking that the update is safe.
I have also created another .exe file from different script that uses also this import and its went well, so any help will be welcome.
I got this error from certbot on Ubuntu 20.04 (focal). On this version of Ubuntu, python 2 seems to be mostly deprecated and many things don't work when using it. Everything needs to be configured to use python3.
When I ran certbot, it was still using python 2.
File "/usr/local/lib/python2.7/dist-packages/certbot/main.py", line 9, in <module>
import configobj
ImportError: No module named configobj
Python 2 pip is no longer available from apt on this version of Ubuntu, so I was not able to install the proper libraries for python 2 using pip.
The version of certbot in apt is supposed to be for python3. (python3-certbot). The executable for certbot gets installed at /usr/bin/certbot.
After further investigating, I found that I had an older python 2 version of certbot hanging around at /usr/local/bin/certbot. Once I removed that (sudo rm /usr/local/bin/certbot), the python3 version of certbot runs just fine and is able to find all its libraries.
You need to install configobj via pip apt install python-pip

DLL load failed in opencv with python

I wish if anyone could give me a hand with my prob,
I have installed python 3.6 amd64 and add it to path then installed opencv 3.2 successfully using "pip" ... However, when I'm trying to import the cv2 (the library of opencv) and here is the error that I have got:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import cv2
ImportError: DLL load failed: The module specified is not found.
Make sure that cv2.pyd is in your Python's lib\site-packages\ directory.
Also check if OPENCV_DIR = C:\Program Files\OpenCV 3.2.0\x64\vc14, or similar, is in your Windows' system variables setting. The sample path OPENCV_DIR is for the binary compiled with VC14 which is Visual Studio 2015 VC++.
I'm guessing you've installed pip2(for python2) instead of pip3.(for python3)
Try this to install pip for python3
sudo apt-get install python3-pip
Now use
pip3 install python3-opencv

Sklearn installation in OSX

I have installed sklearn using the following commands (I tried both) but when I include it I receive the following error:
sudo port install py26-scikit-learn
or:
sudo port install py27-scikit-learn
The problem is:
>>> import sklearn
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named sklearn
You know what maybe wrong?
ANSWER: This worked for me:
sudo port select --set python python26
Make sure you are using the correct python binary. When you install scikits using macports, it becomes available in the macports version of python - not apple's. Use the port select command to change which python is the default when you simply call "python"

fontforge python bindings with macports

I am on Mac OS X and I would like to use a python script like this one to manipulate an OTF with fontforge. The problem is, how do I access fontforge? Do I need a special build for that?
This is what I get when I run the file
Traceback (most recent call last):
File "myfile.py", line 6, in <module>
import fontforge
ImportError: No module named fontforge
(line 6 is import fontforge)
first uninstall what you currently have installed:
port uninstall fontforge
then install the python variant:
port install fontforge +python27
there's also a python26 variant. to see all variants port variants fontforge
You can get the FontForge development version's python module with HomeBrew:
$ brew install python
$ brew install fontforge --HEAD

Categories