ImportError: dynamic module does not define module export function (PyInit_cv2) - ipython

When I try to import cv2 in IPython , I am getting this error-
ImportError: dynamic module does not define module export function (PyInit_cv2)
I have opencv 3.0, ubuntu 14.04, python 3.4.
I searched online but couldn't get any answer.

Python3 libraries usually can be found in:
/usr/lib/python3/dist-packages
I found out that the library cv2.cpython-34m.so is needed for Python3.4.
But this file wasn't be contained in my default library directory.
In my environment, this library was stored in /usr/local/lib/python3.4/dist-packages/
So I created a symbolic link.
Try it with:
cd /usr/lib/python3/dist-packages
sudo ln -s /usr/local/lib/python3.4/dist-packages/cv2.cpython-34m.so
python3
>>> import cv2
I hope, this helps you

In my case, I was using opencv library for python2 in python3.
Check your PYTHONPATH by typing
echo $PYTHONPATH
and see there is correct path for python3.
$ echo $PYTHONPATH
:/usr/local/lib/python3.4/site-packages
I define PYTHONPATH at the end of ~/.bashrc file. Modify it if you are loading python2's library.
After source ~/.bashrc or restart terminal, check PYTHONPATH again
It solves my problem.

Related

How can i use PIL in python?

I have successfully installed Pillow:
chris#MBPvonChristoph sources % python3 -m pip install --upgrade Pillow
Collecting Pillow
Using cached Pillow-9.0.1-1-cp310-cp310-macosx_11_0_arm64.whl (2.7 MB)
Installing collected packages: Pillow
Successfully installed Pillow-9.0.1
but when i try to use it in pycharm got:
Traceback (most recent call last):
File "/Users/chris/PycharmProjects/pythonProject2/main.py", line 1, in
from PIL import Image
ModuleNotFoundError: No module named 'PIL'
or using in Blender i got:
ModuleNotFoundError: No module named 'PIL'
I am not a python lib installing pro...so obviously i made something wrong. But how do i fix that?
Maybe i have to say i am working on a M1 Macbook
looks like you may need to repoint your pycharm to your installed python interpreter.
go to command line and find out python interpreter path. On windows you can where python in your command line an it will give you where your python and packages are installed.. You could also activate python directly in command line and find paths from there. For example, open command line then;
python
press enter = activates python
within then you can do:
import sys
for x in sys.path: x
In pycharm make sure you point to path discovered from step 1 and select that to be your python interpreter within pycharm --- check out examples here https://www.jetbrains.com/help/pycharm/configuring-python-interpreter.html#add-existing-interpreter
Should work. Not sure about all the steps you took, but if you installed python with pycharm on top of your regular installation of python i would recommend :
finding all the paths from step 1
deleting python using system
checking if folders found from paths step still exist
if they do, delete those as well
start over just with one python installation
repoint to that in pycharm
first
pip uninstall PIL
after uninstall
python3 -m pip install --upgrade pip
python3 -m pip install --upgrade Pillow
or
brew install Pillow

How to execute the right Python to import the installed tensorflow.transform package?

The version of my Python is 2.7.13.
I run the following in Jupyter Notebook.
Firstly, I installed the packages
%%bash
pip uninstall -y google-cloud-dataflow
pip install --upgrade --force tensorflow_transform==0.15.0 apache-beam[gcp]
Then,
%%bash
pip freeze | grep -e 'flow\|beam'
I can see that the package tensorflow-transform is installed.
apache-beam==2.19.0
tensorflow==2.1.0
tensorflow-datasets==1.2.0
tensorflow-estimator==2.1.0
tensorflow-hub==0.6.0
tensorflow-io==0.8.1
tensorflow-metadata==0.15.2
tensorflow-probability==0.8.0
tensorflow-serving-api==2.1.0
tensorflow-transform==0.15.0
However when I tried to import it, there are warning and error.
WARNING:tensorflow:From /usr/local/lib/python2.7/dist-packages/tensorflow_estimator/python/estimator/api/_v1/estimator/__init__.py:12: The name tf.estimator.inputs is deprecated. Please use tf.compat.v1.estimator.inputs instead.
ImportErrorTraceback (most recent call last)
<ipython-input-3-26a4792d0a76> in <module>()
1 import tensorflow as tf
----> 2 import tensorflow_transform as tft
3 import shutil
4 print(tf.__version__)
ImportError: No module named tensorflow_transform
After some investigation, I think I have some ideas of the problem.
I run this:
%%bash
pip show tensorflow_transform| grep Location
This is the output
Location: /home/jupyter/.local/lib/python3.5/site-packages
I tried to modify the $PATH by adding /home/jupyter/.local/lib/python3.5/site-packages to the beginning of $PATH. However, I still failed to import tensorflow_transform.
Based on the above and the following information, I think, when I ran the import command, it executes Python 2.7, not Python 3.5
import sys
print('\n'.join(sys.path))
/usr/lib/python2.7
/usr/lib/python2.7/plat-x86_64-linux-gnu
/usr/lib/python2.7/lib-tk
/usr/lib/python2.7/lib-old
/usr/lib/python2.7/lib-dynload
/usr/local/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages
/usr/local/lib/python2.7/dist-packages/IPython/extensions
/home/jupyter/.ipython
Also,
import sys
sys.executable
'/usr/bin/python2'
I think the problem is tensorflow_transform package was installed in /home/jupyter/.local/lib/python3.5/site-packages. But when I run "Import", it goes to /usr/local/lib/python2.7/dist-packages to search for the package, rather than /home/jupyter/.local/lib/python3.5/site-packages, so even updating $PATH does not help. Am I right?
I tried to upgrade my python, but
%%bash
pip install upgrade python
Defaulting to user installation because normal site-packages is not writeable
Then, I added --user. It seems that the python is not really upgraded.
%%bash
pip install --user upgrade python
%%bash
python -V
Python 2.7.13
Any solution?
It seems to me that your jupyter notebook is not using the right python environment.
Perhaps, you installed the package under version 3.5,
but the Notebook uses the other one, thus it cannot find the library
You can pick the other interpreter by clicking on: Python(your version) - bottom left.
VS-Code - Select Python Environment 1
However you can do this also via:
CNTRL+SHIFT+P > Select Python Interpreter to start Jupyter Server
If that does not work make sure that the package you are trying to import is installed under the correct python environment.
If not open up a terminal, activate the environment and install it using:
pip install packagename
For example i did the same thing here: (Note: I'm using Anaconda)
installing tensorflow_transform
After a installation, you can import it in your code directly like this:
importing tensorflow_transform

No module named '_tkinter' configured

ModuleNotFoundError: No module named '_tkinter'
I want to import turtle in Python 3.7
Traceback (most recent call last): File "my.py", line 1, in
from turtle import * File "/usr/local/lib/python3.7/turtle.py", line 107, in
import tkinter as TK File "/usr/local/lib/python3.7/tkinter/init.py", line 36, in
import _tkinter # If this fails your Python may not be configured for Tk ModuleNotFoundError: No module named '_tkinter'
When building python from source, if the Tk development files are missing,
the build ends with
The necessary bits to build these optional modules were not found:
_tkinter
The development files can be installed from the distribution packager.
for instance, in openSUSE Leap-15:
sudo zypper install tk-devel
The IDE tells you that your python is not confingured for Tk (tkinter), which is your problem. While I am not certain on how to fix this becuase I never use turtle or Tk, I have a few ideas.
I am using python 3.7 and importing both turtle and Tk works just fine for me, so I definitely recommend updating to 3.7.
Look for Tk in your python module library and reinstall it.
Just find out how to confingure Tk with python 3.2 if updating doesn't work.
I hope I helped!
As #ederag said, I have installed Python3.7 and tkinter in opensuse.
sudo zypper refresh
sudo zypper update
sudo zypper in zlib-devel bzip2 libbz2-devel libffi-devel libopenssl-devel readline-devel sqlite3 sqlite3-devel xz xz-devel gcc tk-devel tcl-devel
cd ~/Downloads
wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tar.xz
tar xf Python-3.7.3.tar.xz
cd Python-3.7.3
./configure
make
sudo make altinstall
sudo ln -s /usr/local/lib64/python3.7/lib-dynload /usr/local/lib/python3.7/lib-dynload
if you are using linux go to terminal and type sudo apt-get install python3-tk
if you are using windows go to command prompt and type cd C:/Program Files/Python/python37/Scripts press enter then type pip install tkinter
hope it helped!

Python Cryptography Error ImportError: cannot import name certificate_transparency

In cryptography which was included using pip install PyCrpto giving an error after including it in a flask application where we are running it using virtual environment have named it venv and have this dependency correctly installed in venv > lib > python2.7 > site-packages.
Below is the exact error which I am getting here.
from cryptography.x509 import certificate_transparency
ImportError: cannot import name certificate_transparency
But as can be seen in screenshot file cryptography.x509.certificate_transparency.py is there in file structure and even on trying python -c "from cryptography.x509 import certificate_transparency" doesn't show any warnings but here it shows error which is also marked in red.
What can be possibly wrong here ?
I came across this problem when I was building an updated Python Crypto module for shipping with Solaris (see https://www.jmcpdotcom.com/blog/2018-02-21-im-embarrassed-to-admit-it/).
The root cause in your case appears to be that you haven't installed it in the right place. While you claim above that it's "correctly installed" in venv > lib > python2.7 > site-packages the screenshot shows that it is in fact in venv/lib/cryptography. If you move the cryptography dir down a level I would expect that your import would work.

Python virtualenv ImportError: No module named 'zlib'

I am on an Ubuntu machine, which has Python 2.7.6 as its default python. It also has Python 3.4.3, with both versions located in /usr/bin/.
I have downloaded virtualenv and virtualenvwrapper. I then downloaded the current version of Python, 3.5.1. In its directory I ran the following commands:
./configure
make
make test
sudo make altinstall
Python 3.5.1 is now installed in /usr/local/bin/.
I now run the following commands:
mkvirtualenv test1
mkvirtualenv test2 -p /usr/bin/python3
mkvirtualenv test3 -p /usr/local/bin/python3.5
Environment test1 successfully created with Python version 2.7.6, and environment test2 successfully created with Python version 3.4.3. However, test3 fails with the following error:
ImportError: No module named 'zlib'
I see mentioned that I need to have "zlib" installed when compiling python to begin with, though make test didn't seem to give any problems. Do I just need to download and compile zlib from www.zlib.net and recompile python3.5?
zlib is a built-in module for Python 3.5.
I think you just need re-compile Python 3.5...
Look that link for Python virtualenv :
https://www.reddit.com/r/linux4noobs/comments/3uwk76/help_using_python_in_linux/
Get python source and extract
wget https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tgz
tar xvf Python-3.5.0.tgz
configure for local install
cd Python-3.5.0/
./configure --prefix=$HOME/python35
make
If it complains about missing dependencies, install them, make clean and repeat.
make install