importing module in Spyder error - python

I am trying to use sklearn in Spyder. At the beginning when I tried to import it I was gettingImportError: No module named sklearn
Then I tied to set the PATH with PYTHONPATH manager and then use 'Update module names list' from tools menu.then restart the spydet but no success.
at the end I copied the sklearn folder to /Applications/Spyder.app/Contents/Resources/lib/python2.7
This is how I find the PATH of sklearn and copied in into this folder :
>>> import sklearn
>>> sklearn
<module 'sklearn' from '/Library/Python/2.7/site-packages/scikit_learn-0.12_git-py2.7-macosx-10.7-intel.egg/sklearn/__init__.pyc'>
>>>
and then I cd to the parent folder :
cp -r sklearn /Applications/Spyder.app/Contents/Resources/lib/python2.7
but when I try to import the sklearn to spyder like from sklearn import dataset :
Traceback (most recent call last):
File "/Users/mohsenjadidi/Documents/workspace/dsv/test.py", line 10, in <module>
from sklearn import dataset
File "/Applications/Spyder.app/Contents/Resources/lib/python2.7/sklearn/__init__.py", line 17, in <module>
from .base import clone
File "/Applications/Spyder.app/Contents/Resources/lib/python2.7/sklearn/base.py", line 11, in <module>
from .metrics import r2_score
File "/Applications/Spyder.app/Contents/Resources/lib/python2.7/sklearn/metrics/__init__.py", line 6, in <module>
from .metrics import confusion_matrix, roc_curve, auc, precision_score, \
File "/Applications/Spyder.app/Contents/Resources/lib/python2.7/sklearn/metrics/metrics.py", line 17, in <module>
from ..utils import check_arrays
File "/Applications/Spyder.app/Contents/Resources/lib/python2.7/sklearn/utils/__init__.py", line 9, in <module>
from .murmurhash import murmurhash3_32
File "numpy.pxd", line 151, in init sklearn.utils.murmurhash (sklearn/utils/murmurhash.c:4773)
ValueError: numpy.dtype has the wrong size, try recompiling
any idea?Thanks

Moj, Spyder MacOS X App uses its own (internal) Python interpreter, not the system one. This was the only way we (the devs) found to provide a self contained app with numpy, scipy, matplotlib and IPython, without messing with system Python.
Right now I think there is no way to add more packages to the app, but we'll try to improve the situation during the next couple of months.
The other alternative (the one we envisaged) is for users who want more packages to download and install latest EPD and change their Python interpreter in
Tools > Preferences > Console > Advanced Settings > Python Executable

I faced the same problem importing networkx. Drag-drop its folder into spyder's folder was enough for me.

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

Python3: import pandas_datareader ImportError

I'm pretty new to Python, but have Python 3.6 installed, and running a few other programs perfectly. I'm trying to pull data using the pandas_datareader module but keep running into this issue. Operating system: OSX.I've visited the other threads on similar errors and tried their methods to no avail.
Additional concern: When using Sublime Text, if I run it as a Python (instead of Python3) build, it funcitons fine, but all my other accompanying programs are written on Python3. Is there a way of making this work on 3.6 that I'm missing?
I have already visited the 'is_list_like' error question, and have changed the fred.py file to pandas.api.types in the import line.
Traceback (most recent call last):
File
"/Users/scottgolightly/Desktop/python_work/data_read_practice.py", line
3, in <module>
import pandas_datareader.data as web
File
"/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-
packages/pandas_datareader/__init__.py", line 2, in <module>
from .data import (DataReader, Options, get_components_yahoo,
File
"/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-
packages/pandas_datareader/data.py", line 14, in <module>
from pandas_datareader.fred import FredReader
File
"/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-
packages/pandas_datareader/fred.py", line 1, in <module>
from pandas.core.common import is_list_like
ImportError: cannot import name 'is_list_like'
As has been noted, is_list_like has been moved from pandas.core.common to pandas.api.types.
There are several paths forward for you.
My (highly) recommended solution: download Conda and set up an environment with a version of Pandas prior to v0.23.0.
You can install the development version of Pandas, with a patch in place:
pip install git+https://github.com/pydata/pandas-datareader.git
Since you say that you have a version of Pandas in a different environment that works, I suspect the Python calling it is version 2.X. If so, try using past.autotranslate to import the older version of Pandas.
If this working version of Pandas actually belongs to a Python 3.X site-packages, then you can manually import it using:
sys.path.insert(0, '/path/to/other/pandas')
Small workaround is to define it like this:
import pandas as pd
pd.core.common.is_list_like = pd.api.types.is_list_like
import pandas_datareader

Why aren't python packages from Anaconda being detected by Blender?

I've recently been using Blender to render 3D models of objects and training an SVM to recognize pictures of object taken from some perspective. To train said SVMs I need to use "sklearn", which comes by default with Anaconda.
Long story short, I want Blender (which runs Python 3.4.2) to use the packages and modules present in my anaconda installation (which runs Python 3.4.3).
I've tried a variety of things following this website:
https://www.blender.org/api/blender_python_api_2_60_1/info_tips_and_tricks.html#bundled-python-extensions
1.) I deleted the "python" folder within the Blender.app. According to the above website, Blender should fallback to using the version of python installed in the system (i.e. the one installed thru anaconda? Right?) but instead I get this:
2.) So I go onto option number 2. So, what I do is go to the anaconda folder and copy the two folders "bin" and "lib"
into Blender.app/Contents/Resources/2.76/python/ (replacing the lib and bin folders that are already there)
So far everything is good, I open Blender (no error) and then I write a simple script:
from sklearn import svm
When I try to run it I get the following error message:
Error: Python script fail, look in the console for now...
Traceback (most recent call last): File
"/Users/cusgadmin/...
Perception/blender-2.76b-OSX_10.6-x86_64/Scripts/MarkTwo.blend/SimulationMarkOne.py",
line 5, in File
"/Users/cusgadmin/...
Perception/blender-2.76b-OSX_10.6-x86_64/blender.app/Contents/Resources/2.76/python/lib/python3.4/site-packages/sklearn/svm/init.py",
line 13, in
from .classes import SVC, NuSVC, SVR, NuSVR, OneClassSVM, LinearSVC, \ File
"/Users/cusgadmin/...
Perception/blender-2.76b-OSX_10.6-x86_64/blender.app/Contents/Resources/2.76/python/lib/python3.4/site-packages/sklearn/svm/classes.py",
line 4, in
from .base import _fit_liblinear, BaseSVC, BaseLibSVM File "/Users/cusgadmin/...
Perception/blender-2.76b-OSX_10.6-x86_64/blender.app/Contents/Resources/2.76/python/lib/python3.4/site-packages/sklearn/svm/base.py",
line 9, in
from . import libsvm_sparse File "sklearn/svm/libsvm_sparse.pyx", line 5, in init
sklearn.svm.libsvm_sparse (sklearn/svm/libsvm_sparse.c:7612) File
"/Users/cusgadmin/...
Perception/blender-2.76b-OSX_10.6-x86_64/blender.app/Contents/Resources/2.76/python/lib/python3.4/site-packages/sklearn/utils/init.py",
line 16, in
from .class_weight import compute_class_weight, compute_sample_weight File
"/Users/cusgadmin/...
Perception/blender-2.76b-OSX_10.6-x86_64/blender.app/Contents/Resources/2.76/python/lib/python3.4/site-packages/sklearn/utils/class_weight.py",
line 7, in
from ..utils.fixes import in1d File "/Users/cusgadmin/...
Perception/blender-2.76b-OSX_10.6-x86_64/blender.app/Contents/Resources/2.76/python/lib/python3.4/site-packages/sklearn/utils/fixes.py",
line 318, in
from scipy.sparse.linalg import lsqr as sparse_lsqr File "/Users/cusgadmin/...
Perception/blender-2.76b-OSX_10.6-x86_64/blender.app/Contents/Resources/2.76/python/lib/python3.4/site-packages/scipy/sparse/linalg/init.py",
line 113, in
from .matfuncs import * File "/Users/cusgadmin/...
Perception/blender-2.76b-OSX_10.6-x86_64/blender.app/Contents/Resources/2.76/python/lib/python3.4/site-packages/scipy/sparse/linalg/matfuncs.py",
line 20, in
import scipy.misc File "/Users/cusgadmin/...
Perception/blender-2.76b-OSX_10.6-x86_64/blender.app/Contents/Resources/2.76/python/lib/python3.4/site-packages/scipy/misc/init.py",
line 44, in
from . import doccer ImportError: cannot import name 'doccer' Error: Python script fail, look in the console for now...
At which point I don't know what else to do. I used the Python console embedded in Blender to explore the sub-packages in sklearn and surprisingly I am getting non-matching sub-packages..
I am very confused and do not know how to proceed. Any help is greatly appreciated folks.
Best,
MrRed
Alright folks, I found a solution and I hope this will help future generations after me and so on ;)
Anyway, I managed to be able to import the libraries by doing the following:
Keep all the same files Blender came with (DO NOT delete the python folder as the website suggests!)
Go to the subfolder "site-packages" located at: "Blender.app/Contents/Resources/2.76/python/lib/python3.4/site-packages"
There, copy the contents of
"anaconda/lib/python3.4/site-packages" and paste them in the previously mentioned folder
(I copied everything within "anaconda/lib/python3.4/site-packages" except for the "numpy" and "requests" folders, and I also did not copy the file "numpy-1.9.1-py3.4.egg-info" over the folder.)
Hope this helps, and if it does, you're welcome :)

Categories