Python can't find openCV - python

I just ran the script to install OpenCV. I'm running Linux here. I installed it after much struggle but it finally has completed. That being said, for some reason, it still isn't working. I've never had this much trouble installing a package in my life. Here are the last few lines of my terminal:
**********************************************************************
Done. The new package has been installed and saved to
/home/myname/Desktop/OpenCV/opencv-2.4.9/build/build_20140812-1_i386.deb
You can remove it from your system anytime using:
dpkg -r build
**********************************************************************
OpenCV 2.4.9 ready to be used
me:~/Desktop$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named cv2
anyone have any ideas why it's not working?
thanks

From openCV - python installation manual:
After installing:
Installation is over. All files are installed in /usr/local/ folder. But to use it, your Python should be able to find OpenCV module. You have two options for that.
Move the module to any folder in Python Path : Python path can be found out by
entering import sys;print sys.path in Python terminal. It will print out many locations. Move /usr/local/lib/python2.7/site-packages/cv2.so to any of this folder. For example,
su mv /usr/local/lib/python2.7/site-packages/cv2.so /usr/lib/python2.7/site-packages
But you will have to do this every time you install OpenCV.
Add /usr/local/lib/python2.7/site-packages to the PYTHON_PATH: It is to be done only once. Just open ~/.bashrc and add following line to it, then log out and come back.
export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages
Thus OpenCV installation is finished. Open a terminal and try import cv2.
Note that your python version or library location may be different.

Related

Python 3.11 installation: some modules are missing

I'll try to be as clear and specific as possible because I've seen so many people with the same problem, but none of the answers they had worked for me.
The problem in question is: missing modules when trying to import other modules, that (by far as I understand) are installed by default in Python 3.11. eg: missing _sqlite3 when importing sqlite3.
Python 3.11.0 (main, Jan 12 2023, 03:19:52) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/user_name/opt/Python-3.11.0/lib/python3.11/sqlite3/__init__.py",
line 57, in <module> from sqlite3.dbapi2 import *
File "/home/user_name/opt/Python-3.11.0/lib/python3.11/sqlite3/dbapi2.py",
line 27, in <module> from _sqlite3 import *
ModuleNotFoundError: No module named '_sqlite3'
Context
I'm currently working on a Dreamhost VPS, running on Ubuntu 20.04.5. Since it's a Dreamhost VPS keep in mind I can't uso sudo.
I've Python 3.8 installed globally, but I'm trying to set Python 3.11 with venv. The steps to install Python 3.11 were, as indicated by 'Installing a custom version of Python 3 by Dreamhost':
cd /home/user_name/opt
wget https://www.python.org/ftp/python/3.11.0/Python-3.11.0.tgz
tar zxvf Python-3.11.0.tgz
cd Python-3.11.0
./configure --prefix=$HOME/opt/Python-3.11.0
make
make install
When running the make command I saw the following message:
The necessary bits to build these optional modules were not found:
_bz2 _curses _curses_panel
_lzma _tkinter _uuid
readline
To find the necessary bits, look in setup.py in detect_modules() for the module's name.
The following modules found by detect_modules() in setup.py have not
been built, they are *disabled* by configure:
_sqlite3
This is my detect_modules() in setup.py:
def detect_modules(self):
# remove dummy extension
self.extensions = []
# Some C extensions are built by entries in Modules/Setup.bootstrap.
# These are extensions are required to bootstrap the interpreter or
# build process.
self.detect_simple_extensions()
self.detect_test_extensions()
self.detect_readline_curses()
self.detect_crypt()
self.detect_openssl_hashlib()
self.detect_hash_builtins()
self.detect_dbm_gdbm()
self.detect_sqlite()
self.detect_platform_specific_exts()
self.detect_nis()
self.detect_compress_exts()
self.detect_expat_elementtree()
self.detect_multibytecodecs()
self.detect_decimal()
self.detect_ctypes()
self.detect_multiprocessing()
self.detect_tkinter()
self.detect_uuid()
# Uncomment the next line if you want to play with xxmodule.c
# self.add(Extension('xx', ['xxmodule.c']))
self.addext(Extension('xxlimited', ['xxlimited.c']))
self.addext(Extension('xxlimited_35', ['xxlimited_35.c']))
So after running the commands I just mentioned, I proceeded to setup the virtualenv to test Python 3.11:
cd /home/user_name/opt
/home/user_name/opt/Python-3.11.0/bin/python3 -m venv venv
source venv/bin/activate
And then I got the ModuleNotFoundError I mentioned at the start of the question.
Things I tried/saw:
When I checked my Python 3.11 directory, I saw that in /home/user_name/opt/Python-3.11.0/lib/python3.11/lib-dynload the cpython files for the modules I don't have are just not there, e.g: /home/user_name/opt/Python-3.11.0/lib/python3.11/lib-dynload/_sqlite3.cpython-311-x86_64-linux-gnu.so* where as in my Python 3.8, they're indeed there: /usr/lib/python3.8/lib-dynload/_sqlite3.cpython-38-x86_64-linux-gnu.so
I tried copying the cpython file from my 3.8 to my 3.11 and renaming it:
cp /usr/lib/python3.8/lib-dynload/_sqlite3.cpython-38-x86_64-linux-gnu.so /home/user_name/opt/Python-3.11.0/lib/python3.11/lib-dynload
mv /home/user_name/opt/Python-3.11.0/lib/python3.11/lib-dynload/_sqlite3.cpython-38-x86_64-linux-gnu.so /home/user_name/opt/Python-3.11.0/lib/python3.11/lib-dynload/_sqlite3.cpython-311-x86_64-linux-gnu.so
Then when I ran Python 3.11 I got the following error:
Python 3.11.0 (main, Jan 12 2023, 04:33:33) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import _sqlite3 # this works just fine
>>> import sqlite3 # throws error
356306 segmentation fault (core dumped) python
Tried recompiling Python and running ./configure --prefix=$HOME/opt/Python-3.11.0 --enable-loadable-sqlite-extensions and didn't work.
Hope I was clear enough and thanks in advance.

ImportError: No module named 'paramiko'

I have done through the other questions online here, and I feel that mine is different enough to warrant a new question.
So I have a Centos 6 box, which is running a small website for me, acts as an office git server and I am trying to configure Python3 on it.
So I followed the following these steps to set up python3 on the server. However it seems that I cannot import paramiko into my script.
I downloaded the paramiko rpm however I get this message:
When I try to import paramiko I get:
[root#GIT Python-3.4.2]# rpm -ivh /usr/lib/Python-3.4.2/Modules/python-paramiko-1.7.5-2.1.el6.noarch.rpm
Preparing... ########################################### [100%]
package python-paramiko-1.7.5-2.1.el6.noarch is already installed
When I run python3 directly:
[root#GIT inserv_health_check]# python3
Python 3.4.2 (default, Jan 21 2015, 06:28:04)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import paramiko
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'paramiko'
>>>
I am sure there is a simple solution to this problem perhaps the path is wrong, or I should have put a symbolic link in somewhere. Any help would be appreciated :)
Before anyone asks, which python output:
[root#GIT Python-3.4.2]# which python
/usr/bin/python
[root#GIT Python-3.4.2]# which pytho~n3
/usr/bin/which: no pytho~n3 in (/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
[root#GIT Python-3.4.2]# which python3
/usr/local/bin/python3
Thanks
You need to do pip install paramiko so that python sees that module. If you work on a virtual environment, you need to workon <env_name> first and then pip install the desired module.
type pip3 install paramiko
if you want to install it for python3

Psycopg2 install with pip works but cannot import module on OS X 10.9

I've installed psycopg2 with
pip install psycopg2
and it worked just fine. The install output had a couple of warning along the lines of
In file included from ./psycopg/psycopg.h:33:
./psycopg/config.h:71:13: warning: unused function 'Dprintf' [-Wunused-function]
static void Dprintf(const char *fmt, ...) {}
^
1 warning generated.
but in the end it says
Successfully installed psycopg2
and it also appears when I run pip list.
Now when I try to import it in python I get an error:
$ python
Python 2.7.5 (default, Aug 25 2013, 00:04:04)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named psycopg2
Why can't Python import the module if it was successfully installed?
(Python 2.7.5 was installed with Homebrew. psycopg2 was installed with pip.)
OS X comes with Python 2.7.5 already; when you install Python with Homebrew, it puts a newer version in a different place without touching the built-in one. Homebrew's Python also comes with Pip, whereas OS X's doesn't. What's going on here is, you're using Homebrew's pip to install psycopg2, then running OS X's python and trying to import it.
Run /usr/local/bin/python (the full path to Homebrew's Python), and try import psycopg2 from there.
If that works, you need to put /usr/local/bin before /usr/bin in your PATH variable, so that your shell finds Homebrew's Python before the OS X one every time. If you use Bash (the default shell in OS X), you can do this by putting the following in your .bash_profile:
export PATH=/usr/local/bin:$PATH
To make sure that you're running the right Python in scripts, use the following shebang line:
#!/usr/bin/env python
env will search PATH for Python and run the script with the first one it finds, the same as typing python from your shell. Bash scripts and such should inherit the PATH variable and find the right python without changes.
You can also hardcode the path to Homebrew Python in your shebang line (#!/usr/local/bin/python), but this means your script will only work on OS X machines with a Homebrew Python installed, and is best avoided.

Issue with Python Path. "ImportError: No module named setuptools"

I recently updated to Python 2.7 in order to start working with Django "Django requires Python, specifically Python 2.6.5 - 2.7.x."
As a result I needed to install some of the tools I have been using when I went through the excellent LPTHW Exercise 46
pip from http://pypi.python.org/pypi/pip
distribute from http://pypi.python.org/pypi/distribute
nose from http://pypi.python.org/pypi/nose/
virtualenv from http://pypi.python.org/pypi/virtualenv
Should be easy enough, I had done it all before...
When I run python in terminal I get the below. Note: Python 2.7.5
$ python
Python 2.7.5 (v2.7.5:ab05e7dd2788, May 13 2013, 13:18:45)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
When I type "which python" I get
$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
However when I try to install distribute, nose or virtualenv I get the below error. Note Python/2.6
$ sudo pip install distribute
Exception:
Traceback (most recent call last):
File "/Library/Python/2.6/site-packages/pip-1.3.1-py2.6.egg/pip/basecommand.py", line 139, in main
status = self.run(options, args)
File "/Library/Python/2.6/site-packages/pip-1.3.1-py2.6.egg/pip/commands/install.py", line 258, in run
import setuptools
ImportError: No module named setuptools
Storing complete log in /Users/brendanspillane/Library/Logs/pip.log
localhost:~ brendanspillane$
I believe the Python/2.6 is the issue here. As a result i tried to set my Python Path (my first time) Using this question as a reference "changing python path on mac?" i typed in
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
This has no apparent effect.
However I am still not able to run sudo pip install distribute and keep getting the same error.
Can anyone help me? Is it my Python Path? or am I looking in the wrong direction?
Any help is as always, much appreciated.
Deepend
I'm not really sure how it worked but it did. #Robert Lujo deserves the up votes for his answer at https://stackoverflow.com/a/16511140/1214163
If the answer is also added here i will accept it.

how to install lpsolve module for Python on Linux Ubuntu 10.04?

I am using Ubuntu 12.04 64-bit, and started learning python today.(I tried to install a pirate version of MATLAB but failed...)
I have a linear programming problem to solve, and I want to use lp_solve module for Python.
I tried for 1~2 hours to find the download file and install the module.
I am not sure if I downloaded a right thing, and I could not install it until now.
How can I install this?
There is no download link in http://lpsolve.sourceforge.net/, and it tells me to run a command
python setup.py install
but there is no setup.py file in anywhere, including the lpsolve source file I downloaded somewhere.
If you know where to download it, and install it, could you teach me how to do them, step by step?
I am not sure about the version of my Python.
Thank you.
Adding few more details to the answer provided by dnozay.
Download the following two files from http://sourceforge.net/projects/lpsolve/files/lpsolve/
lp_solve_5.5.2.0_dev_ux64.tar.gz - contains the .so files
lp_solve_5.5.2.0_Python2.5_exe_ux64.tar.gz - contains the python wrapper scripts for lpsolver, which helps to invoke the native library from .so files.
Unzip the above downloaded files, where each directory formed by unzip will have an lpsolve55.so file, though at different locations.
Specify the paths to lpsolve55.so file in each directory by setting the following two environment variables:
export LD_LIBRARY_PATH=/usr/local/lib:/home/xxx/lp_solve_dev/
export PYTHONPATH=/home/xxx/usr/lib/python2.5/site-packages
To test if lpsolver is configured as expected :
[xx-xxxx#ip-xx-x-x-xx ~]$ python
>>>Python 2.7.9 (default, Apr 1 2015, 18:18:03)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>from lpsolve55 import *
>>>lpsolve()
lpsolve Python Interface version 5.5.0.9
using lpsolve version 5.5.2.0
Usage: ret = lpsolve('functionname', arg1, arg2, ...)
P.S.: make sure you have installed python-dev (if not, type sudo apt-get install python-dev at the command line) before you do this all.
The download link is:
http://sourceforge.net/projects/lpsolve/, or
http://sourceforge.net/projects/lpsolve/files/lpsolve/ for the files tab.
Once you have it installed, you may need to tweak your PYTHONPATH.
You also may want to look into cvexp:
http://pypi.python.org/pypi/cvexp

Categories