Unable to import opencv in virtualenv - python

I have installed opencv successfully in my system and able to import it without virtualenv. I know I need to copy cv2.so files in my virtualenv directory to be able to use it within virtualenv, but the problem is there is NO cv2.so files in my local site packages. All I can see some .so files named with libopencv-core * alike.
I grep-ed it, tried finding it manually in site-packages, py-modules, but no clue why its isn't there?
I've successfully build and make all files, I am sure there is nothing missed while installing it, because without virtualenv I am easily executing it.
OpenCV Version: 2.4.8
Python Version: 2.7.8
OS: Ubuntu 14.01

To import opencv using your virtualenv you should install it on your virtualenv or copy the cv2.pyd (on windows) to your venv site-packages directory.
If you are on linux you could install it using sudo apt-get install python-opencv.
If you are building it from source you should follow the steps listed here.
On step 12:
Also make sure that in the PYTHON field, everything is filled. (Ignore PYTHON_DEBUG_LIBRARY).
Look at the image presented in this step. The python paths listed there should be your venv python paths not your system's python paths.
Hope it helps!

Related

Manually installed python package but cannot import

I tried to install the keras_contrib package in a virtual machine which does not allow internet access. So I manually unzip the package, navigate to folder and using python setup.py install to install it. After that I can find the package using pip list, however when I import the package cannot be found. And I cannot find the package folder in the anaconda/lib/site-packages.
(link of the package: https://github.com/keras-team/keras-contrib)
These are the screenshots at the beginning and ending during installation.
Any suggestion? Thanks very much.
You should look at your $PYTHONPATH environment variables. If you are using Mac or Linux, write which python command on your terminal. Also, you can look at the link below for a setup that is similar to your problem
https://github.com/sparklingpandas/sparklingpandas/wiki/setup.py-Install-for-Anaconda-Python

How to move all modules to new version of Python (from 3.6 to 3.7)

I just upgraded to python 3.7 and I realized that all my modules stuck with the previous version. Even Django is not recognised anymore. How can I do to transfer everything to the new version? I am a little lost right now, don't even know where the new version has been installed.
Edit:
When I do $ which python3.6 the terminal tells me it doesn't exist, but I have a python3.6 directory in /usr/local/lib/, where all modules are installed.
In the same directory /usr/local/lib/ I also have a python3.7 directory with some modules installed but many are missing. However when I search for the file python3.7 in my finder it doesn't appear. when I do $ which python3.7 the path is /usr/local/bin so not the same path as the directory.
Anyone sees what happened and knows how I can transfer all modules to python3.7?
Even if the old python version has been removed, it is possible to use the pip of the current python version with the --path option to list all the modules installed in the previous version.
For example, migrating all my user installed python modules from 3.7 to 3.8
pip freeze --path ~/.local/lib/python3.7/site-packages > requirements.txt
pip install --user -r requirements.txt
Incidentally, I always use pip install with --user and leave the system wide installations to the package manager of my linux distro.
It is safer to re-install all packages due to possible compatibility issues:
pip3.6 list | awk '{print $1}' | xargs -I{} pip3.7 install {}
in older version of Python --run the command
pip freeze > requirements.txt
download and install newer version on python.. change the PATH variable to the new version
and run the command
pip install -r requirements.txt
I'm not sure about all modules...but if you want to install a module specifically in python3.7, try this:
python3.7 -m pip install *module_name*
In some cases, we don't have the opportunity to pip freeze in old version--because I've already updated and old version have been purged! There are some measures I've taken to recover some of the packages but I'm NOT sure every package would work with this fix.(e.g. the packages built with wheels)
mv /your/path/to/python3.{6,7}/site-packages/
If the case is packages installed outside venv (in /usr/local/ or ~/.local), reinstall pip with get-pip.py, just to be safe.
If you are recovering a virtualenv. Activate your virtualenv and use my script
Most of your packages should work by now. If anything malfunctions, pip reinstall would works. If you still want it 100% works, pip freeze now.😉
I have an alternative
(Not sure if works outside Windows 10)
I'm currently migrating from 3.7 to 3.8 and the way I found to re-install my previous libraries was by using a script I had that updates all packages via pip install. (Assuming you installed your new Python version as your main version) This checks for all the packages I had and updates/install them in the new Python version.
Note: I prefer to run the script from the command line
Use the file explorer to go to the folder where you have the script;
Click on the path box, write "cmd" and press enter to open a command line from the folder where you are;
Write "python name_of_your_script.py" and press enter to run the command.
The script (adapted from this solution):
import pkg_resources
from subprocess import call
packages = [dist.project_name for dist in pkg_resources.working_set]
[call("pip install " + name + " --upgrade") for name in packages]
I faced a similar problem, now that I upgraded from python 3.7 to python 3.8 (new)
I installed Python 3.8, but the system kept the python37 subfolder with the already installed packages(...\Python37-32\Lib\site-packages) even with the Pyhton38 subfolder created, with the new python.exe.
Usually, it's possible to keep on using the old libraries on your new Python version, because the existent libraries installation folder are already registered in your local computer system path (*).
Even though, I've had problems to use some libraries (some worked in Jupyter Notebook but not in Spyder). I tried the alternatives others proposed to migrate libraries but it did not worked (maybe I did not
So I used brutal force solution.. Not elegant at all, but it worked:
remove the OLD python version folders from the system path or even remove the folder itself for good..
Folders: C:\Users\USERNAME\AppData\Roaming\Python\Python37
C:\Users\USERNAME\AppData\Local\Programs\Python\Python37
Reinstall the packages you need, preferably via Anaconda prompt.
python -mpip install library_name
OR
pip install --user --force-reinstall package_name
OR
pip install --user --force-reinstall package_name == specify_package_version
The libraries will be installed at c:\users\USERNAME\anaconda3\lib\site-packages and recognized by your new python version.
(*) to add the folder to the PATH: System properties --> environment variables --> click "Path"--> edit --> add folder name)

Install PyQt and other python modules for multiple versions in MAC OS X Mavericks

I have shed blood sweat and tears to make a python environment working in MAC OS Mavericks.
THE DATA/FACTS:
OS: MAC OS X Mavericks
What I am trying to do: install some modules in python directories so I can import them later and use them in code, like PyQt4 or 5
What have I done: Installed installed homebrew, brew installed python 3.3, installed pip, installed easy_install, pip installed numpy, gtk etc.
OS X has pre-installed python 2.7
which python: /usr/bin/python
which python3: /usr/local/bin/python3
THE PROBLEM:
I get a message that the library cannot be found when I import it through python prompt.
I have basic unix commands knowledge and I am a beginner in python. Any help will be appreciated !
i faced the same problem few weeks back, while installing kivy and accessing it from terminal in Mac OSX Mavericks.
you can edit the .bash_profile file or create a virtualEnv
I found create a VirtualEnv, where you can create and isolated python dev environment and install packages that you want to use.The following links will guide you to do that and learn to use VirtualEnv
you can learn about VirtualEnv using
http://iamzed.com/2009/05/07/a-primer-on-virtualenv/
then you can use the tutorial below to build your ....in Mac OSX
http://www.lowindata.com/2013/installing-scientific-python-on-mac-os-x/
go to your home directory ~/ and type ls -al for viewing all hidden files
locate .bash_profile and view using nano .bash_profile
you may have multiple files or files like .bash_profile.pysave which you can delete and edit your .bash_profile and set the path as mentioned in the tutorial above.if you don't have one create one.
creating a virtualEnv works best for using python and installing all modules locally..
which python
output:after creating a virtualEnv.https://pypi.python.org/pypi/virtualenv
/usr/local/bin/python
I had the same problem and i was able to fix it by creating symbolic links from the PyQt4/5 modules at our desired Virtualenv.
So Because we installed Qt and PyQt4/5 with brew it is not located together with our system modules, so when we create a environment, virtualenv does not know how to get it, so we have do do it manually. I did it through a shell script. But there may be better ways of doing it.
So, in order to do that i run this lines of code within my virtualenv root:
#!/bin/bash
ln -s /usr/local/Cellar/sip/4.15.3/lib/python2.7/site-packages/sip.so lib/python2.7/site-packages/sip.so
ln -s /usr/local/Cellar/sip/4.15.3/lib/python2.7/site-packages/sipconfig.py lib/python2.7/site-packages/sipconfig.py
ln -s /usr/local/Cellar/sip/4.15.3/lib/python2.7/site-packages/sipdistutils.py lib/python2.7/site-packages/sipdistutils.py
ln -s /usr/local/lib/python2.7/site-packages/PyQt4 lib/python2.7/site-packages/PyQt4
It may not be needed to create all of the symlinks above, as i understand just the PtQt4/5 folder and the sip.so will do the trick.
After doing that you should be able to import the PyQt4/5 modules from your python environment.
Editing to link a video tutorial on how to fix the issue:
video

Anaconda installation to home directory

I have set up a SSH connection to a remote server. I want to run some of my python programs on it so am having to download all the modules I had been using.
I just downloaded Ananconda (I don't have root access so installed it in ~) and added ~/anaconda/bin to my PATH. However when I try import numpy in Python, it says the module is not found. How do I fix this?
You might be running the wrong version of Python.
To check, use which -a python
james#bodacious:~$which -a python
/usr/bin/python
james#bodacious:~$
In my case, I'm running the version from /usr/bin/python, and that's the only version found in my $PATH. You should see the version from ~/anaconda/bin in your list as well, and for it to be run when you type python it needs to be at the top.
If it's not, you can check your $PATH and, if necessary, add ~/anaconda/bin to the front of it.
james#bodacious:~$echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/james/bin
james#bodacious:~$PATH=~/anaconda/bin:$PATH
james#bodacious:~$echo $PATH
/Users/james/anaconda/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/james/bin
james#bodacious:~$
I haven't any Fedora/Redhat systems handy, but I believe you can:
yum install numpy
HTH
You've said that all you really want is to be able to use numpy - based on that, using anaconda is probably overkill.
It sounds as though what you're really asking is "Since I don't have root access and can't install system packages, how can I set up a python environment in my home dir that has what I need?"
This sounds like a job for... Super Grover! no wait, I meant virtualenv.
Hopefully your system will already have virtualenv installed for you. If it does, it's fairly simple for you to create your own environment with your own set of packages:
james#bodacious:~$mkdir venv/
james#bodacious:~$cd venv/
james#bodacious:venv$virtualenv .
New python executable in ./bin/python
Installing Setuptools..............................................................................................................................................................................................................................done.
Installing Pip.....................................................................................................................................................................................................................................................................................................................................done.
james#bodacious:venv$source bin/activate
(venv)james#bodacious:venv$pip install numpy
Downloading/unpacking numpy
Downloading numpy-1.7.1.zip (3.1MB): 3.1MB downloaded
Once that completes, you'll have your own copy of numpy which you can access in this environment just by using cd venv; source bin/activate to set your $PATH and $PYTHONPATH to point at your custom install.
If you don't already have virtualenv installed things get more tricky....

How can I get FEniCS working in Ubuntu 12.04 with EPD python?

FEniCS that comes in the Ubuntu 12.04 repository does not work with Enthought EPD unless I do some crazy stuff with PYTHONPATH which can often result in EPD using Ubuntu repository python modules rather than EPD modules.
The alternative then is to compile and install all of the FEniCS modules manually. This is screwy because FEniCS needs sudo to install in the normal EPD directory, /usr/local/EPD. If you use sudo, this means that PATH environment variable is not being sourced from ~/.bashrc so it thinks it's working with the native python, not EPD. I tried using the -i option on sudo, and that did some screwy things also.
I managed to solve my own problem. There were a bunch of issues with this technique that I am about to describe, and they are detailed here and here. For reasons that I don't understand, reinstalling Ubuntu fixed the problems described in the links, but that's beyond the scope of what I'm trying to cover here. Suffice it to say that it's good to install Ubuntu with / and /home as separate partitions because it makes complete reinstall very easy.
Procedure for Installing FEniCS for use with EPD
Download all of the packages here. Create the directory ~/.local/src/fenics and save them there. Run tar -xvf on all the files in that directory. An easy easy to do this is with the command for i in *.tar.gz; do tar -xvf $i; done.
First install the python modules FFC, FIAT, Instant, Viper and UFL by going into each of their directories and running python setup.py install --user. The user flag causes them to be installed in /.local/lib.. something. This will be added to your sys.path in python. You can read more about the --user flag here.
Then navigate to the directories for dolfin and ufc, and in each of them run the following commands: cmake -DCMAKE_INSTALL_PREFIX=~/.local ., make, make install.
Lastly, add source /home/chad/.local/share/dolfin/dolfin.conf to ~/.bashrc using gedit or emacs if you want to use a powerful text editor.
EDIT
You must also install ScientificPython using python setup.py install --user, and this is relatively painless.
EDIT
This should get you up and running for the demos in ~/.local/share/dolfin/demo/pde/poisson/python. I hope this helps someone.

Categories

Resources