Can not import opencv and dlib in anaconda enviroment - python

When i create a project by Pycharm in Anconda environment, use : conda info --envs i get this :
base C:\Users\Admin\Anaconda3
env_dlib C:\Users\Admin\Anaconda3\envs\env_dlib
opencv-env C:\Users\Admin\Anaconda3\envs\opencv-env
then use command : conda activate env_dilb i can import dlib easily by using this command :python then import dlib, but i can not import cv2 by import cv2, it get notice : "ModuleNotFoundError: No module named 'cv2'".
And conversely when i use conda activate opencv-env, I can only do
import cv2.
How can i import two of both, and some other libraries too.

In the environment "dlib" package is available but "opencv" is not available.
Please follow the below steps to install opencv in the environment:
conda activate env_dlib
pip install opencv-python
After that try importing opencv using below command:
import cv2

Related

ModuleNotFoundError: No module named 'flair'

I have installed flair library via the following command
!pip install flair
but when i tries to import it, it will generate error like "ModuleNotFoundError: No module named 'flair'"
Code:
import torch
import numpy as np
from flair.data import Sentence
from flair.embeddings import TransformerDocumentEmbeddings
install via the following command make sure you use --user option otherwise you will get a permission error in windows 10.
!pip install --user flair
after install flair you have to restart kernel in jupyter notebook

Why does installing Tensorflow with conda ruin matplotlib?

Starting from a new anaconda environment, it looks like this (summarized, not a full paste):
[myenv]$ python
Python 3.8.8 ...
>> import matplotlib.pyplot as plt
>>
[myenv]$ conda install tensorflow-gpu
...
[myenv]$ python
>> import tensorflow
>> tensorflow.__version__
'2.4.1'
>> import matplotlib.pyplot as plt
ModuleNotFoundError: No module named 'matplotlib'
>>
[myenv]$ conda install matplotlib
...
[myenv]% python
>> import tensorflow
>> import matplotlib.pyplot as plt
<tons of traceback>
RunTimeError: the sip module implements API v12.0 to v12.4 but the PyQt5.core module requires API v12.5
How can I have tensorflow and matplotlib at the same time?
Do you have the complete traceback? Based on the Error you posted, it seems like its a dependency issue on the sip module. I'll recommend using you own virtualenv instead of anacondas.
FOR CREATING YOUR OWN VIRTUALENV:
open terminal
go to the directory you want your project to be
pip install virtualenv
virtualenv venv
source venv/bin/activate (for linux or mac)
pip install tensorflow
pip install matplotlib
pip install <whatever library you want>

Module Not found when importing PyCaret in Jupyter

I'm trying to learn PyCaret but having a problem when trying to import it in Jupyter Lab.
I'm working in a virtualenv and installed pycaret via pip:
pip install pycaret
I can confirm its installed via pip list:
prompt-toolkit 3.0.7
protobuf 3.13.0
py 1.9.0
pycaret 2.1.2
pycparser 2.20
The very first line in the notebook is:
from pycaret.nlp import *
however this results in:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-3-7c206b4a9ead> in <module>
----> 1 from pycaret.nlp import *
2 import psycopg2
3 import sys, os
4 import numpy as np
5 import pandas as pd
ModuleNotFoundError: No module named 'pycaret'
I'm pulling my hair out trying to figure this out and can't find anyone else with something similar.
I've tried to import via the python shell as well and that works perfectly.
You should create a seperate environment for installing time series alpha module
after creating a new environment and switching into
pip install pycaret-ts-alpha
and then you will be able to access
https://towardsdatascience.com/announcing-pycarets-new-time-series-module-b6e724d4636c
I forgot that you had to install modules via Jupyter.
Following this guide: http://jakevdp.github.io/blog/2017/12/05/installing-python-packages-from-jupyter/index.html
Installing like so:
# Install a pip package in the current Jupyter kernel
import sys
!{sys.executable} -m pip install numpy
Got it working
First Create a new environment conda documentation
Second Download the Pycaret with this instruction
Third check your sklearn version is greater thansklearn>=0.23.2.
Because if it's greater PyCaret is not compatible with that.
Nothing works for you? Download directly from github with this command
pip install git+https://github.com/pycaret/pycaret.git#egg=pycaret
I read on the tutorial page of pycaret that to install it through a Jupyter-notebook you should add an exclamation mark in front of the python command in the Jupyter-cell:
!pip install pycaret

ModuleNotFoundError: No module named 'numpy' - Jupyter Notebook

I'm facing weird issue in my Jupyter-notebook.
In my first cell:
import sys
!{sys.executable} -m pip install numpy
!{sys.executable} -m pip install Pillow
In the second cell:
import numpy as np
from PIL import Image
But it says : ModuleNotFoundError: No module named 'numpy'
I have used this command to install Jupyter notebook :
sudo apt install python3-notebook jupyter jupyter-core python-ipykernel
Additional information :
pip --version
pip 20.2.2 from /home/maifee/.local/lib/python3.7/site-packages/pip (python 3.7)
python --version
Python 3.7.5
Thanks to #suuuehgi.
When Jupyter Notebook isn't opened as root:
import sys
!{sys.executable} -m pip install --user numpy
I've had occasional weird install issues with Jupyter Notebooks as well when I'm running a particular virtual environment. Generally, installing with pip directly in the notebook in this form:
!pip install numpy
fixes it. Let me know how it goes.
Restarting the kernal from the Jupyter Notebook solved this issue for me
I had a similar issue. Turns out I renamed an upstream path. And I hadn't deactivated my conda env first. When I deactivated the env.
conda deactivate
Then when I activated it again, everything was as it should have been.
conda activate sample
Now I am seeing other issues with jupyter themes... but its not impacting my numpy code. So, at least I fixed the "ModuleNotFoundError: No module named 'numpy'" error
I have the same problem. My numpy is installed, I am using the same folder as usual.
If I try 'conda deactivate', I get the message:
ValueError: The python kernel does not appear to be a conda environment. Please use %pip install instead.
I added a print of the 'pip install numpy' result and the 'Module not found error' after
Here is a solution which worked for me:
lib_path="c:\\users\\user\\python_39\\lib\\site-packages\\"
MODULE_NAME = "module_to_import"
MODULE_PATH = lib_path+MODULE_NAME+"\\__init__.py"
import importlib
import sys
spec = importlib.util.spec_from_file_location(MODULE_NAME, MODULE_PATH)
module = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = module
spec.loader.exec_module(module)
import module_to_import

ImportError: cannot import name 'dnn_superres' for python example of super resolution with opencv

I am trying to run an example for upscaling images from the following website:
https://towardsdatascience.com/deep-learning-based-super-resolution-with-opencv-4fd736678066
This is the code I am using:
import cv2
from cv2 import dnn_superres
# Create an SR object
sr = dnn_superres.DnnSuperResImpl_create()
# Read image
image = cv2.imread('butterfly.png')
# Read the desired model
path = "EDSR_x3.pb"
sr.readModel(path)
# Set the desired model and scale to get correct pre- and post-processing
sr.setModel("edsr", 3)
# Upscale the image
result = sr.upsample(image)
# Save the image
cv2.imwrite("./upscaled.png", result)
I have downloaded the already trained model from the website, called "EDSR_x3.pb" and when I run the code I get the following error:
Traceback (most recent call last):
File "upscale.py", line 2, in <module>
from cv2 import dnn_superres
ImportError: cannot import name 'dnn_superres'
I now it seems like there is no such method or class, but I have already installed opencv and the contrib modules. Why do I get this error?
The key is in the documentation for opencv-python. dnn_superres is an extra module and requires you to install opencv-contrib-python
pip install opencv-contrib-python
your opencv version should be opencv4.2.0+, the same question:
https://github.com/opencv/opencv_contrib/issues/2544
solution:
pip install --upgrade opencv-python
pip install --upgrade opencv-contrib-python
I had the same problem with Python 3.6.9 and opencv 4.2.0, but after the upgrade to 4.3.0, the problem disappeared. If you have no problem upgrading the version, try 4.3.0.
As of this date, to add something to the currently accepted answer :
If you are doing it in your local system :
If you have
opencv-python
you need to uninstall it first, you can check if you have above package or not by the command :
pip show opencv-python
if you have it, uninstall it first by
pip uninstall opencv-python
and install
opencv-contrib-python
by
pip install opencv-contrib-python
by default it will download the latest version but still check the version by the show command above for opencv-contrib-python this time, just to check the version is above opencv4.2.0+, to force download a speicific version go through this : pip install package_name==version_number
If you are doing it in Google Colab :
opencv-python is already installed in colab but the version is 4.1.2, you can check it by :
import cv2
cv2.__version__
you don't need to uninstall it, you just need to install opencv-contrib-python but if you install by :
!pip install opencv-contrib-python
It won't install it, it would default to same preinstalled opencv
so run this :
!pip install opencv-contrib-python==
which will output all the available versions for colab, just select any version 4.3.x or above and run again, like:
!pip install opencv-contrib-python==4.3.0.36
Run the below commands into your terminal
pip install --upgrade opencv-contrib-python
pip install --upgrade opencv-python
Then, Restart your Jupyter notebok kernel or Reactivate your virtual environment so that the updated package can be used properly.

Categories

Resources