Cannot import opencv besides in site-packages directory with python3 - python-3.x

I am following a lynda openCV python course and its opencv installation guide but got stuck on importing the cv2.
I used pip3 install opencv-python to install cv2.
I can only import cv2 when my current directory is /usr/local/lib/python3.6/site-packages . Anywhere else would give me
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'
Following are my system versions:
Python 3.6.0
macOS High Sierra 10.13.2
Opencv version '3.4.0'
Here's the __init__.py found in /usr/local/lib/python3.6/site-packages/cv2, not sure how this matters though
import importlib
import os
from .cv2 import *
from .data import *
# wildcard import above does not import "private" variables like __version__
# this makes them available
globals().update(importlib.import_module('cv2.cv2').__dict__)
UPDATE
I added this code and it seems to be working from any directory, still question is why do I have to do this? I can just import numpy from anywhere.. weird
import sys
sys.path.append('/usr/local/lib/python3.6/site-packages')

It is because of your cv2.so compiled library file. If you look into your /usr/local/lib/python3.6/site-packages/cv2/python-3.6 folder (or for some users, its inside /usr/local/python/cv2/python-3.6), you should see the file cv2.cpython-36m-darwin.so like so:
(cv) fangran#Fangs-MacBook-Pro ~ % ls /usr/local/lib/python3.6/site-packages/cv2/python-3.6
cv2.cpython-36m-darwin.so
Now, what you'd want to do is to cd into that directory and rename it to cv2.so (so its easier)
(cv) fangran#Fangs-MacBook-Pro ~ % cd /usr/local/lib/python3.6/site-packages/cv2/python-3.6
(cv) fangran#Fangs-MacBook-Pro python-3.6 % sudo mv cv2.cpython-36m-darwin.so cv2.so
In case you're in a virtual environment (which is recommended), make sure to do into the site-packages of that environment and do a symlink:
(cv) fangran#Fangs-MacBook-Pro site-packages % cd ~/.virtualenvs/cv/lib/python3.6/site-packages/
(cv) fangran#Fangs-MacBook-Pro site-packages % ln -s /usr/local/python/cv2/python-3.6/cv2.so cv2.so
or if not, just make sure that whatever env you're in can get access to the cv2.so file.

Related

Installing tensorflow object_detection api in a repo in google drive

I have cloned the models repo from TensorFlow and installed it into my project folder in google drive.
but when I try to run the model_builder_tf2_test.py I am getting ModuleNotFoundError: No module named 'object_detection' error.
import sys
nb_path = '/content/drive/MyDrive/project/lib'
sys.path.insert(0, nb_path)
and installed it using the following command
!python -m pip install --target=$nb_path .
the files are downloaded into the project folder but can't be accessed by the files in models repo.
however, I can access the modules using the:
import sys
sys.path.append('/content/drive/MyDrive/project/lib')
The error I am getting after running:
%cd /content/drive/MyDrive/project/models/research
!python object_detection/builders/model_builder_tf2_test.py
is:
Traceback (most recent call last):
File "object_detection/builders/model_builder_tf2_test.py", line 24, in <module>
from object_detection.builders import model_builder
ModuleNotFoundError: No module named 'object_detection'
I would add this as a comment but still don't have enough reputation.
did you try using this?
pip install tensorflow-object-detection-api
if it helps,
believe the question is a duplicate?

How to specify __init__.py while building a python library

This is the first python library I am building. The folder structure looks like:
mylibrary
- build
- data
- docs
- environment.yml
- README.md
- license
- setup.py
- my library
- __init__.py
- Module1
- __init__.py
- module1_worklfow.py
- Module 2
- __init__.py
- module2_worklfow.py
On the init.py under "mylibrary" I have the statement from .Module1 import classfrommodule1workflow and from .Module2 import classfrommodule2workflow.
On the __init__ files that are inside the modules, I have from mylibrary.module1.module1_workflow import classfrommodule1workflow and the same for module2 init.
Now if I install the library through the conda command line with wheel, the library imports well. I am able to run in python from mylibrary import module1 but then if in the same command line I open a jupyter notebook, I am not able to import the library within jupyter. It says - no module named mylibrary.module1
I'm almost sure there's something wrong with my main init file, just can't figure out what it is.
EDIT: folder with files for MCVE here.
Steps on anaconda prompt:
cd folder_path
conda env create -f environment.yml
conda activate mylibrary
python setup.py bdist_wheel
pip install path_to_wheel
python
import mylibrary
import mylibrary.module1
quit()
This won't throw any errors, which is great! Then on the same command line:
jupyter notebook
then on the jupyter notebook:
import mylibrary
yields:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-8c582ad816fa> in <module>
----> 1 import mylibrary
ModuleNotFoundError: No module named 'mylibrary'
and
import mylibrary.module1
yields:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-2-64d831ac2965> in <module>
----> 1 import mylibrary.module1
ModuleNotFoundError: No module named 'mylibrary'
Here's an image for clarity:
Edit:
OK, I downloaded your code and it seems to be an issue with your setup.py after all. You forgot to add packages=find_packages(), to the argument list of setup, so the package you were installing was effectively empty. When import mylibrary was working for you when launching python from command line, it was probably because import also loads modules from current directory (try launching python from a different directory and check if it works).
By the way, you don't have to build a wheel if you just want to install from source. You can use pip install . or python setup.py install. Alternatively, for development purposes it's useful to do pip install --editable . or python setup.py develop.
Original answer:
Sounds like your package/library is OK but when you're launching jupyter notebook it's probably using a different interpreter. It should help to install jupyter in the conda environment. If it still doesn't work, you can try launching it with python -m jupyter notebook to ensure that you're using the same interpreter as for installing your package.

Python Circular Import Module _gi causes apt-add-repository command to fail in linux

I was trying to add a repository today when i find a strange error that i cannot seem to solve.
I was trying to do this command
sudo apt-add-repository ppa:fish-shell/release-3
When it always returned this error message
Traceback (most recent call last):
File "/usr/bin/apt-add-repository", line 11, in <module>
from softwareproperties.SoftwareProperties import SoftwareProperties, shortcut_handler
File "/usr/lib/python3/dist-packages/softwareproperties/SoftwareProperties.py", line 67, in <module>
from gi.repository import Gio
File "/usr/lib/python3/dist-packages/gi/__init__.py", line 42, in <module>
from . import _gi
ImportError: cannot import name '_gi' from partially initialized module 'gi' (most likely due to a circular import) (/usr/lib/python3/dist-packages/gi/__init__.py)
I've tried diagnosing it with this command
sudo update-alternatives --config python3
and it shows this
There is only one alternative in link group python3 (providing /usr/bin/python3): /usr/bin/python3.8
Nothing to configure.
I've also tried going to the /usr/lib/python3/dist-packages/gi
and adding a symbolic link there with this command
sudo ln -s _gi.cpython-{36m,37m}-x86_64-linux-gnu.so
Still it didn't work
I've tried adding the correct python version to /usr/bin/apt-add-repository
I have also tried to use
python3 -m pip install gi
Could not find a version that satisfies the requirement gi (from versions: )
No matching distribution found for gi
How do i fix this annoying error?
done:
sudo apt-get install graphviz
And the code:
import objgraph
import random
import io
from graphviz import Source
from io import StringIO
q = StringIO()
#objgraph.show_refs([SAFE_TAGS], output=s)
objgraph.show_chain(
objgraph.find_backref_chain(
random.choice(objgraph.by_type('dict')),
objgraph.is_proper_module),
output=q)
q.seek(0)
Source(q.read())
chart

ImportError: cannot import name 'Coroutine'

I am trying to follow this instructions here:
https://github.com/hyperledger/indy-agent/blob/master/python/README.md
in order to simulate Hyperledger indy interactions, on a ubuntu 18.04 AWS instance.
I already cloned the repository and installed dependencies.(I am following dev mode instruction because the docker fails)
Then i launch the command:
python3 indy-agent.py 8094
And here the logs I have:
Traceback (most recent call last):
File "indy-agent.py", line 17, in <module>
import aiohttp_jinja2
File "/home/ubuntu/.local/lib/python3.5/site-packages/aiohttp_jinja2/__init__.py", line 6, in <module>
from aiohttp import web
File "/home/ubuntu/.local/lib/python3.5/site-packages/aiohttp/__init__.py", line 6, in <module>
from .client import (
File "/home/ubuntu/.local/lib/python3.5/site-packages/aiohttp/client.py", line 12, in <module>
from typing import ( # noqa
ImportError: cannot import name 'Coroutine'
This the aiohttp/client.py file:
"""HTTP Client for asyncio."""
import asyncio
import base64
import hashlib
import json
import os
import sys
import traceback
import warnings
from types import SimpleNamespace, TracebackType
from typing import ( # noqa
Any,
Coroutine,
Generator,
Generic,
….
)
This is the aiohttp/init.py file:
version = '3.5.4'
from typing import Tuple # noqa
from . import hdrs
from .client import (
….
And this is the aiohttp_jinja2/init.py:
import asyncio
import functools
import warnings
import jinja2
from collections import Mapping
from aiohttp import web
….
Any ideas on how to fix it?
Should I change some lines of code in one of these files?
Thanks
ADDITIONAL INFORMATION(BASED ON ANSWERS)
After the feedback from the answers, I updated to python 3.6.9.
While the asyncio problems fixed, still I get problems While setting up docker in the /python directory, I got the following:
1)make docker-build
2)sudo make docker-start PORT=8094
docker run -it -p $PORT:$PORT -e PORT=$PORT --name indy-agent_$PORT indy-agent
Traceback (most recent call last):
File "indy-agent.py", line 20, in <module>
from modules.connection import Connection, AdminConnection
File "/app/modules/__init__.py", line 1, in <module>
from python_agent_utils.messages.errors import ValidationException
ModuleNotFoundError: No module named 'python_agent_utils'
Makefile:5: recipe for target 'docker-start' failed
make: [docker-start] Error 1 (ignored)
docker rm indy-agent_$PORT
indy-agent_8094
When I try to install manually(without docker), I try to launch “pip install -r requirements.txt”, and I get the following:
ERROR: Could not find a version that satisfies the requirement aiohttp==3.5.4 …….ERROR: No matching distribution found for aiohttp==3.5.4 (from -r requirements.txt (line 2))
Then, when I try to launch the command “python3.6 indy-agent.py 8094”, I get continuously logs about missing modules. I installed them manually, which allowed me to go a little further in the process. But then I receive logs about modules that I cannot install through python.
This fact of having to install all the modules manually seems very strange to me, but I installed the agent outside the indy-sdk directory and also inside indy-sdk directory, and I get the same results.
Here’s the commands I launch and the logs I get:
1)python3.6 indy-agent.py 8094
ModuleNotFoundError: No module named 'aiohttp_jinja2'
2)python3.6 -m pip install aiohttp_jinja2
3)python3.6 indy-agent.py 8094
ModuleNotFoundError: No module named 'base58'
4)python3.6 -m pip install base58
No module named 'dateutil'
5)python3.6 -m pip install python-dateutil
6)python3.6 indy-agent.py 8094
ModuleNotFoundError: No module named 'indy'
7)sudo apt-get update
8)sudo apt-get install -y libindy
9)python3.6 -m pip install indy
10)python3.6 indy-agent.py 8094
ImportError: cannot import name 'did'
11)python -m pip install did
12)sudo python3 -m pip install did
The directory '/home/ubuntu/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/ubuntu/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Could not find a version that satisfies the requirement did (from versions: )
No matching distribution found for did

How can I import bs4 from Beautiful Soup in Python3 on OSX 10.12.5?

I'm trying to fix what seems like a common problem with importing a module in Python 3. I'm running OS X 10.12.5 and have Python 3 installed on my MacBook Air and am using Sublime Text to edit and run my code.
When I try this import:
from bs4 import BeautifulSoup
...I get this error:
Traceback (most recent call last):
File "/Users/<myname>/Python/code-python3/Pgm#001", line 5, in <module>
from bs4 import BeautifulSoup
ImportError: No module named 'bs4'
I successfully installed with this PIP and on every re-install I see this:
$ pip install beautifulsoup4
Requirement already satisfied: beautifulsoup4 in ./anaconda/lib/python3.5/site-packages
I've tried qualifying the location with things like:
from ./anaconda/lib/python3.5/site-packages/bs4 import BeautifulSoup
...but invariably get a variety of syntax errors; usually on the first '/'.
I am not using a virtual environment but do plan to read up on that approach as these kind of configuration and setup errors are big time wasters.
When I try to run this right in python3 I get slightly different errors:
>>> from bs4 import BeautifulSoup
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/<myname>/anaconda/lib/python3.5/site-packages/bs4/__init__.py", line 30, in <module>
from .builder import builder_registry, ParserRejectedMarkup
File "/Users/<myname>/anaconda/lib/python3.5/site-packages/bs4/builder/__init__.py", line 314, in <module>
from . import _html5lib
File "/Users/<myname>/anaconda/lib/python3.5/site-packages/bs4/builder/_html5lib.py", line 70, in <module>
class TreeBuilderForHtml5lib(html5lib.treebuilders._base.TreeBuilder):
AttributeError: module 'html5lib.treebuilders' has no attribute '_base'
Any tips about where the obvious answer exists that I am still missing would be greatly appreciated. I've seen a lot of things about PATH and PYTHONPATH in similar questions but had no success with any of these solutions either.
Yes, I'm not a fan of anaconda but looks like the path is being pointed to:
"/Users//anaconda/lib/python3.5/site-packages"
... and for the sake of keeping things as clean as possible (this being a good example/reason why you'll see people say to use env for your python projects)
Lets google here! Here's how to remove anaconda:
conda install anaconda-clean #first the configs
anaconda-clean --yes
rm -rf ~/anaconda.* #then conda, the ".*" is regex to cover any version
The check your bash_profile and/or bashrc; and here you can set your Python path. Make sure it not pointing to anaconda
if you run
ls -lh `which python # orpython3
You'll get the path of which is the one you'll be setting as the path.

Resources