what could be the reason for the importerror [duplicate] - python

This question already has answers here:
It Keeps showing this. What Should I do? [closed]
(2 answers)
Closed 2 years ago.
I am trying to train a model of CNN.
When I run the code, it returns following error:
Traceback (most recent call last):
File "train_network.py", line 5, in <module>
import matplotlib
File "/home/kaustubhj/.virtualenvs/dl4cv/lib/python3.7/site-packages/matplotlib/__init__.py", line 138, in <module>
from . import cbook, rcsetup
File "/home/kaustubhj/.virtualenvs/dl4cv/lib/python3.7/site-packages/matplotlib/cbook/__init__.py", line 32, in <module>
import numpy as np
File "/home/kaustubhj/.virtualenvs/dl4cv/lib/python3.7/site-packages/numpy/__init__.py", line 142, in <module>
from . import core
File "/home/kaustubhj/.virtualenvs/dl4cv/lib/python3.7/site-packages/numpy/core/__init__.py", line 102, in <module>
from . import _dtype_ctypes
File "/home/kaustubhj/.virtualenvs/dl4cv/lib/python3.7/site-packages/numpy/core/_dtype_ctypes.py", line 25, in <module>
import _ctypes
ImportError: libffi.so.6: cannot open shared object file: No such file or directory

ImportError generally refers to an import library required for execution is not present within executing systems.
Few possible cases to look for.
In a general case, your system where py is running is missing this package.
In Spark(general-purpose computing cluster), if you are running this cnn then one of your worker node could be missing file/package.
Bugs: In Py bug tracker this bug reported for some package like Pipenv. This resolved as per https://bugs.python.org/issue40367 Just upgrade your used libs to stable version
If its path issue then check for which python you have installed library and which version you are using. I.e. there could be multiple python version in your systems. Python2 pkgs repo and Python3 pkgs repo loc are different.
If its simple OS path issue - https://jackiechen.org/2016/01/13/importerror-libffi-so-6-cannot-open-shared-object-file-no-such-file-or-directory/
Try to just use the latest version or switch to more stable version packages to avoid this error.

Related

ImportError: cannot import name 'COMMON_SAFE_ASCII_CHARACTERS' from 'charset_normalizer.constant'

Traceback (most recent call last):
File "g:\mydrive\ \pdftotext_pdfminer.py", line 3, in <module>
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
File "C:\Users\ \anaconda3\envs\ \lib\site-packages\pdfminer\pdfinterp.py", line 7, in <module>
from .cmapdb import CMap
File "C:\Users\ \anaconda3\envs\ \lib\site-packages\pdfminer\encodingdb.py", line 7, in <module>
from .psparser import PSLiteral
File "C:\Users\ \anaconda3\envs\ \lib\site-packages\pdfminer\psparser.py", line 22, in <module>
from .utils import choplist
File "C:\Users\ \anaconda3\envs\ \lib\site-packages\pdfminer\utils.py", line 31, in <module>
import charset_normalizer # For str encoding detection
File "C:\Users\ \anaconda3\envs\ \lib\site-packages\charset_normalizer\__init__.py", line 23, in <module>
from charset_normalizer.api import from_fp, from_path, from_bytes, normalize
File "C:\Users\ \anaconda3\envs\ \lib\site-packages\charset_normalizer\api.py", line 10, in <module>
from charset_normalizer.md import mess_ratio
File "charset_normalizer\md.py", line 5, in <module>
ImportError: cannot import name 'COMMON_SAFE_ASCII_CHARACTERS' from 'charset_normalizer.constant' (C:\Users\ \anaconda3\envs\ \lib\site-packages\charset_normalizer\constant.py)
This error happens whenever I'm using pdfminer. I also installed pdfminer-six
My code worked just fine until two days ago.
It started to happen today when I tried to just run it again without any adjustment in the file
I'm assuming maybe it's the pdfminer's problem but there's no update about the module...
(I'm running this on my conda env)
Does anyone know what this error means? and how to fix it?
When I encountered this I used:
pip install chardet
there. I faced the same problem when trying to use the pdfplumber package today (2022-11-24) from a script I have long used with no problem. I don't know why this error is happening but found one of the solutions in this link helpful:
How to fix AttributeError: partially initialized module?
Briefly, I removed my entire virtual environment using the command conda env remove --name ds (being ds the name of my environment). Then, I created a new one and installed every package I needed again through conda or pip. It is working perfectly now. Hope it works for you as well.
Out of curiosity, I have installed Tensorflow last week. Maybe it interfered with pdfplumber somehow (not sure). Have you installed any new package since the last time you used pdfminer? Best of luck!
use follow instead.
pip install mmcv-full==1.3.17

How to install gensim and run package in python?

i want to do in python 3.7.4:
and getting this Error:
i already tried:
using conda and pip
using local windows and windows server
multiple reinstallments of diffenent versions of packages (e.g. numpy and scipy)
from gensim.models import Word2Vec
Traceback (most recent call last):
File "c:/Users/Administrator/Documents/GitHub/contract-criteria-identifier-on-aws/schnelltest.py", line 1, in
import gensim
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\gensim__init__.py", line 5, in
from gensim import parsing, corpora, matutils, interfaces, models, similarities, summarization, utils # noqa:F401
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\gensim\parsing__init__.py", line 4, in
from .preprocessing import (remove_stopwords, strip_punctuation, strip_punctuation2, # noqa:F401
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\gensim\parsing\preprocessing.py", line 42, in
from gensim import utils
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\gensim\utils.py", line 40, in
import scipy.sparse
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\scipy\sparse__init__.py", line 230, in
from .csr import *
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\scipy\sparse\csr.py", line 13, in
from ._sparsetools import (csr_tocsc, csr_tobsr, csr_count_blocks,
ImportError: DLL load failed: The specified module could not be found.
PS C:\Users\Administrator\Documents\GitHub\contract-criteria-identifier-on-aws> & C:/Users/Administrator/AppData/Local/Programs/Python/Python37/python.exe c:/Users/Administrator/Documents/GitHub/contract-criteria-identifier-on-aws/schnelltest.py
Traceback (most recent call last):
File "c:/Users/Administrator/Documents/GitHub/contract-criteria-identifier-on-aws/schnelltest.py", line 1, in
import gensim
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\gensim__init__.py", line 5, in
from gensim import parsing, corpora, matutils, interfaces, models, similarities, summarization, utils # noqa:F401
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\gensim\parsing__init__.py", line 4, in
from .preprocessing import (remove_stopwords, strip_punctuation, strip_punctuation2, # noqa:F401
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\gensim\parsing\preprocessing.py", line 42, in
from gensim import utils
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\gensim\utils.py", line 40, in
import scipy.sparse
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\scipy\sparse__init__.py", line 230, in
from .csr import *
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\scipy\sparse\csr.py", line 13, in
from ._sparsetools import (csr_tocsc, csr_tobsr, csr_count_blocks,
ImportError: DLL load failed: The specified module could not be found.
That error is actually suggesting some problem in scipy's installation – that's the source of the actual line of code where the DLL load failed error arises. You should try:
(1) uninstalling & reinstalling conda
(2) ensuring you've activated a "conda environment"
(3) manually conda-installing individually required packages, like scipy, and watching the output carefully for any warnings/errors
(4) when finally running your code, still ensuring you've activated the right conda environment
If you still have problems, expand your question with more specific details about how each part was installed, and whether there was reported success or failure at each step.
Notably, conda is usually very helpful in getting these packages working under Windows. But in general, they all get the most development, use, and quality-attention under more Unix/Linux-like systems. So if you have the option to not use Windows, everything may have a better chance of working, and when you hit problems, you'll have a larger body of prior docs/help to draw upon. (It all can still be made to work on Windows, but often requires using secondary or less-preferred installation/configuration options.)

Exception during calling gensim?

I tried to load gensim in my code. Often it works fine. Today, I get the following exception:
Traceback (most recent call last):
File "/project/6008168/tamouze/just.py", line 2, in <module>
import gensim
File "/project/6008168/tamouze/Python_directory/ENV2.7_new/lib/python2.7/site-packages/gensim/__init__.py", line 5, in <module>
from gensim import parsing, corpora, matutils, interfaces, models, similarities, summarization, utils # noqa:F401
File "/project/6008168/tamouze/Python_directory/ENV2.7_new/lib/python2.7/site-packages/gensim/parsing/__init__.py", line 4, in <module>
from .preprocessing import (remove_stopwords, strip_punctuation, strip_punctuation2, # noqa:F401
File "/project/6008168/tamouze/Python_directory/ENV2.7_new/lib/python2.7/site-packages/gensim/parsing/preprocessing.py", line 40, in <module>
from gensim import utils
File "/project/6008168/tamouze/Python_directory/ENV2.7_new/lib/python2.7/site-packages/gensim/utils.py", line 44, in <module>
from smart_open import smart_open
File "/project/6008168/tamouze/Python_directory/ENV2.7_new/lib/python2.7/site-packages/smart_open/__init__.py", line 1, in <module>
from .smart_open_lib import *
File "/project/6008168/tamouze/Python_directory/ENV2.7_new/lib/python2.7/site-packages/smart_open/smart_open_lib.py", line 29, in <module>
import requests
File "/project/6008168/tamouze/Python_directory/ENV2.7_new/lib/python2.7/site-packages/requests/__init__.py", line 97, in <module>
from . import utils
File "/project/6008168/tamouze/Python_directory/ENV2.7_new/lib/python2.7/site-packages/requests/utils.py", line 26, in <module>
from ._internal_utils import to_native_string
ImportError: cannot import name to_native_string
Im using python 2.7.14 and gensim 3.4.0.
How can I solve this problem?
The error isn't really occurring in gensim, even though that's how you found it. If you look at the stack, it's only triggered because gensim uses smart_open which in turn uses requests. It is in requests that the error happens.
If this was working, but now stopped, something likely changed in your environment, or how you're launching this code, related to the relationship between Python and the requests package.
For such errors, you should try searching Google for the final-two lines of your error stack – those most connected to the problem. Those are:
from ._internal_utils import to_native_string
ImportError: cannot import name to_native_string
(These leave out the file path that's specific to you, but have a number of unique tokens likely to have also been reported by any others.)
A number of people have hit this, from a variety of other projects, but always triggered through requests. Some have reported re-installing requests (perhaps to ensure it's version 2.0.0 or later) helps.
If a simple re-install doesn't help, you could also try one or all of:
uninstall, verify it's not present at all (that requests itself isn't found), then install – this could make sure you don't have overlapping redundant installations in different places that are both confusing the issue
start from a fresh Python environment, reinstalling all packages
double-check that all packages share the same Python2/Python3 compatibility

Python shell working differently on different location in the same machine inside same virtual environment

I have a cloud instance of a Linux machine (openSuSE) with multiple users.
I have created a virtual environment and installed all my required libraries (including Klein).
I have two users "a" and "b".
While logged in as "a" and inside virtualenv, when I open python shell at home directory and type
import klein
it imports normally.
Now when I change directory to
/home/b/
and run the same (open python shell, import klein) while being in the same virtualenv, it gives me an error.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/a/.local/lib/python3.6/site-packages/klein/__init__.py", line 3, in <module>
from klein._plating import Plating
File "/home/a/.local/lib/python3.6/site-packages/klein/_plating.py", line 16, in <module>
from .app import _call
File "/home/a/.local/lib/python3.6/site-packages/klein/app.py", line 19, in <module>
from twisted.internet import endpoints, reactor
File "/home/a/.local/lib/python3.6/site-packages/twisted/internet/endpoints.py", line 58, in <module>
from twisted.protocols.tls import TLSMemoryBIOFactory
File "/home/a/.local/lib/python3.6/site-packages/twisted/protocols/tls.py", line 63, in <module>
from twisted.internet._sslverify import _setAcceptableProtocols
File "/home/a/.local/lib/python3.6/site-packages/twisted/internet/_sslverify.py", line 158, in <module>
verifyHostname, VerificationError = _selectVerifyImplementation()
File "/home/a/.local/lib/python3.6/site-packages/twisted/internet/_sslverify.py", line 141, in _selectVerifyImplementation
from service_identity import VerificationError
File "/home/a/.conda/envs/mm/lib/python3.6/site-packages/service_identity/__init__.py", line 7, in <module>
from . import cryptography, pyopenssl
File "/home/a/.conda/envs/mm/lib/python3.6/site-packages/service_identity/cryptography.py", line 16, in <module>
from .exceptions import SubjectAltNameWarning
File "/home/a/.conda/envs/mm/lib/python3.6/site-packages/service_identity/exceptions.py", line 21, in <module>
#attr.s
AttributeError: module 'attr' has no attribute 's'
Command "which python" gives same address at both location which is my virtualenv python address and that should be expected.
But what causes this weird python shell behavior.
Thank you
I solved it and a very shameful reason caused the error.
One of the modules Twisted uses is "attr" module. I had named one of my files attr.py and that is what was causing all the error.
I myself am not deleting this question if moderation has no problem, maybe somebody like me might be stuck at the same situation. It may help them.
Never name your python files same as that of any standard module unless overriding.
Also if your issue persists, then Jean's answer will definitely resolve it.
There can be multiple different Python packages that provide the same Python module. For example, there are at least two packages that provide the attr module:
https://pypi.org/project/attr/
https://pypi.org/project/attrs/
It's possible you've installed the wrong package based on the requirements. You can check what you have installed with pip freeze.

How to solve import CNTK library?

I have problem with linking CNTK library with Anaconda.
My PYTHONPATH: c:\repos\cntk\bindings\python;%PYTHONPATH%
My CNTK_PY_35: C:\local\Anaconda3-4.1.1-Windows-x86_64\envs\cntk-py35
And I have error:
Traceback (most recent call last):
File "C:/Users/szymo/PycharmProjects/ImageClassification/Main.py", line 1, in <module>
import cntk
File "c:\repos\cntk\bindings\python\cntk\__init__.py", line 11, in <module>
from .core import *
File "c:\repos\cntk\bindings\python\cntk\core.py", line 10, in <module>
from . import cntk_py
ImportError: cannot import name 'cntk_py'
Anyone knows how to solve this problem?
Best
As Nikos points, this is very likely due to mixing up of binary installation with the toolkit being built from the sources.
Please try to add something like --with-py36-path=/opt/anaconda/envs/cntk-py36 to your ./configure command options before building CNTK. Please note that you may need to modify the path and Python version according to your setting. The above option is for Arch Linux with Python 3.6 (Anaconda).

Categories