matplotlib does not import in python3 [duplicate] - python

This question already has answers here:
Pelican 3.3 pelican-quickstart error "ValueError: unknown locale: UTF-8"
(6 answers)
Closed 6 years ago.
I have OSX El capitan, and get the following error when trying to import matplotlib in python 3:
>>> import matplotlib
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/__init__.py", line 1131, in <module>
rcParams = rc_params()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/__init__.py", line 975, in rc_params
return rc_params_from_file(fname, fail_on_error)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/__init__.py", line 1100, in rc_params_from_file
config_from_file = _rc_params_in_file(fname, fail_on_error)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/__init__.py", line 1018, in _rc_params_in_file
with _open_file_or_url(fname) as fd:
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/contextlib.py", line 59, in __enter__
return next(self.gen)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/__init__.py", line 1000, in _open_file_or_url
encoding = locale.getdefaultlocale()[1]
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/locale.py", line 559, in getdefaultlocale
return _parse_localename(localename)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/locale.py", line 487, in _parse_localename
raise ValueError('unknown locale: %s' % localename)
ValueError: unknown locale: UTF-8
I tried to delete the matplotlib folder, and reinstall it with pip install matplotlib, which it recreates it, but it ends up giving the same error.
I already spend half the weekend trying to find an answer but no luck - maybe somebody can help.
Best

Found a solution here. Basically what you have to do is add two lines to your ~/.bash_profile (and re-open terminal window):
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

Related

Error creating Satellite images from THREDDS server after MetPy 0.12->1.1 upgrade

Upgraded MetPy to 1.1 and my ability to produce satellite images from a THREDDS server is no longer working. I isolated the part that is causing me issues.
Example:
#!/usr/bin/python3
import metpy
from siphon.catalog import TDSCatalog
cat_e = TDSCatalog('https://thredds.ucar.edu/thredds/catalog/satellite/goes/east/products/CloudAndMoistureImagery/FullDisk/Channel09/current/catalog.xml')
nc_e = cat_e.datasets[0].remote_access(use_xarray=True)
data_e = nc_e.metpy.parse_cf('Sectorized_CMI')
yields the following output/error
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/metpy/xarray.py", line 785, in _rebuild_coords
var = var.metpy.convert_coordinate_units(coord_name, 'meters')
File "/usr/local/lib/python3.6/dist-packages/metpy/xarray.py", line 190, in convert_coordinate_units
data=self._data_array[coord].metpy.unit_array.m_as(units)
File "/usr/local/lib/python3.6/dist-packages/pint/quantity.py", line 386, in m_as
return self.to(units).magnitude
File "/usr/local/lib/python3.6/dist-packages/pint/quantity.py", line 591, in to
magnitude = self._convert_magnitude_not_inplace(other, *contexts, **ctx_kwargs)
File "/usr/local/lib/python3.6/dist-packages/pint/quantity.py", line 540, in _convert_magnitude_not_inplace
return self._REGISTRY.convert(self._magnitude, self._units, other)
File "/usr/local/lib/python3.6/dist-packages/pint/registry.py", line 925, in convert
return self._convert(value, src, dst, inplace)
File "/usr/local/lib/python3.6/dist-packages/pint/registry.py", line 1762, in _convert
return super()._convert(value, src, dst, inplace)
File "/usr/local/lib/python3.6/dist-packages/pint/registry.py", line 1376, in _convert
return super()._convert(value, src, dst, inplace)
File "/usr/local/lib/python3.6/dist-packages/pint/registry.py", line 958, in _convert
raise DimensionalityError(src, dst, src_dim, dst_dim)
pint.errors.DimensionalityError: Cannot convert from 'microradian' (dimensionless) to 'meter' ([length])
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/data/home/met_scheduler/scripts/satellite/satellite_metpy11.py", line 8, in <module>
data_e = nc_e.metpy.parse_cf('Sectorized_CMI')
File "/usr/local/lib/python3.6/dist-packages/metpy/xarray.py", line 774, in parse_cf
var = self._rebuild_coords(var, crs)
File "/usr/local/lib/python3.6/dist-packages/metpy/xarray.py", line 797, in _rebuild_coords
var = var.assign_coords(coords={coord_name: new_coord_var})
File "/data/home/met_scheduler/.local/lib/python3.6/site-packages/xarray/core/common.py", line 349, in assign_coords
data.coords.update(results)
File "/data/home/met_scheduler/.local/lib/python3.6/site-packages/xarray/core/coordinates.py", line 90, in update
priority_arg=1, indexes=self.indexes)
File "/data/home/met_scheduler/.local/lib/python3.6/site-packages/xarray/core/merge.py", line 362, in merge_coords
expanded = expand_variable_dicts(aligned)
File "/data/home/met_scheduler/.local/lib/python3.6/site-packages/xarray/core/merge.py", line 222, in expand_variable_dicts
var = as_variable(var, name=name)
File "/data/home/met_scheduler/.local/lib/python3.6/site-packages/xarray/core/variable.py", line 96, in as_variable
"variable %r has invalid type %r" % (name, type(obj)))
TypeError: variable 'coords' has invalid type <class 'dict'>
I appreciate any thoughts on how to get around this.
Can you double check that your Python installation with MetPy is correct? I was able to run your code fine on my local install of MetPy 1.1.0 with Python 3.9. Also, your traceback shows Python 3.6 being used; MetPy 1.1.0 only supports Python >=3.7. You should not have been able to install MetPy 1.1.0 with that version of Python.
You can confirm the version of MetPy being used by adding the following to your script:
print(metpy.__version__)

How to use dates in the yahoo_fin Python Package

I recently installed yahoo_fin and I tired the following example:
get_calls('NFLX' ')
It worked. I then tired the following:
get_calls('NFLX', '11/8/2019')
It failed. Here is what I got:
get_calls('NFLX', '11/8/2019')
Traceback (most recent call last):
File "", line 1, in
get_calls('NFLX', '11/8/2019')
File "C:\Users\rsher\Anaconda3\lib\site-packages\yahoo_fin\options.py", line 48, in get_calls
options_chain = get_options_chain(ticker, date)
File "C:\Users\rsher\Anaconda3\lib\site-packages\yahoo_fin\options.py", line 32, in get_options_chain
tables = pd.read_html(site)
File "C:\Users\rsher\Anaconda3\lib\site-packages\pandas\io\html.py", line 906, in read_html
keep_default_na=keep_default_na)
File "C:\Users\rsher\Anaconda3\lib\site-packages\pandas\io\html.py", line 743, in _parse
raise_with_traceback(retained)
File "C:\Users\rsher\Anaconda3\lib\site-packages\pandas\compat__init__.py", line 344, in raise_with_traceback
raise exc.with_traceback(traceback)
ValueError: No tables found
I am using version 3.6.3 of Python and I am also using Spyder.
Am I doing something wrong? Do you think I have found a bug?
I updated my version of yahoo_fin. Not really sure it was out of date. I now get the following error messages when I run the command: get_calls("nflx", "1/31/20")
Traceback (most recent call last):
File "", line 1, in
get_calls("nflx", "1/31/20")
File "C:\Users\rsher\Anaconda3\lib\site-packages\yahoo_fin\options.py", line 48, in get_calls
options_chain = get_options_chain(ticker, date)
File "C:\Users\rsher\Anaconda3\lib\site-packages\yahoo_fin\options.py", line 32, in get_options_chain
tables = pd.read_html(site)
File "C:\Users\rsher\Anaconda3\lib\site-packages\pandas\io\html.py", line 906, in read_html
keep_default_na=keep_default_na)
File "C:\Users\rsher\Anaconda3\lib\site-packages\pandas\io\html.py", line 743, in _parse
raise_with_traceback(retained)
File "C:\Users\rsher\Anaconda3\lib\site-packages\pandas\compat__init__.py", line 344, in raise_with_traceback
raise exc.with_traceback(traceback)
ValueError: No tables found
It should work the way you have it.
from yahoo_fin.options import get_calls
get_calls("nflx", "1/31/20")
Are you using the most recent version of yahoo_fin? It should be (as of this writing) version 0.8.4. Another possible issue is that there could have been a problem with Yahoo Finance's page for that option chain at that particular time.

Fixing subprocess.py file Error: File not found? [duplicate]

This question already has answers here:
What exactly is current working directory?
(5 answers)
Closed 27 days ago.
I am using a python package from Gray Arrow's Dejavu. I have installed all the required dependencies and other packages necessary to run my code. When running the following code:
import warnings
import json
warnings.filterwarnings("ignore")
from dejavu import Dejavu
from dejavu.recognize import FileRecognizer
with open('dejavu.cnf') as f:
config = json.load(f)
djv = Dejavu(config)
djv.fingerprint_file('mp3/Sean-Fournier--Falling-For-You.mp3')
song = djv.recognize(FileRecognizer, 'mp3/Sean-Fournier--Falling-For-
You.mp3')
print("from file recognized: {}\n".format(song))
I receive the following errors:
Traceback (most recent call last):
File "testing.py", line 13, in <module>
djv.fingerprint_file('mp3/Sean-Fournier--Falling-For-You.mp3')
File "D:\dejavu-master\dejavu\__init__.py", line 109, in
fingerprint_file song_name=song_name
File "D:\dejavu-master\dejavu\__init__.py", line 182, in
_fingerprint_worker channels, Fs, file_hash = decoder.read(filename,
limit)
File "D:\dejavu-master\dejavu\decoder.py", line 51, in read
audiofile = AudioSegment.from_file(filename)
File "C:\Program Files\Python37\lib\site-
packages\pydub\audio_segment.py", line 665, in from_file info =
mediainfo_json(orig_file)
File "C:\Program Files\Python37\lib\site-packages\pydub\utils.py",
line 263, in mediainfo_json res = Popen(command,
stdin=stdin_parameter, stdout=PIPE, stderr=PIPE)
File "C:\Program Files\Python37\lib\subprocess.py", line 775, in
__init__ restore_signals, start_new_session)
File "C:\Program Files\Python37\lib\subprocess.py", line 1178, in
_execute_child startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file
specified
I have done complete re-installations of python 3.7 and all of the packages and dependencies to attempt correction of this problem.
I do not understand what file cannot be found and have done research in hopes to find an answer to this issue. I truly appreciate any help and knowledge that can be shared with me on this topic.
Try and place "Sean-Fournier--Falling-For-You.mp3" file in the exact directory you have your script, I don't think the folder reference to the mp3 folder is being handled correctly :)
djv.fingerprint_file('mp3/Sean-Fournier--Falling-For-You.mp3')
song = djv.recognize(FileRecognizer, 'mp3/Sean-Fournier--Falling-For-
You.mp3')

Python: Spyder crashed and doesn't open anymore

I use Spyder to run my python codes in my Mac OS x.
It happens that spyder just crashed, and since them it doesn't open anymore.
When I try to open it directly from ~/anaconda/bin, the following error msg appears:
Image of the error
after I click in ok, I get back the lines of error:
[Julias-MBP-3:~/anaconda/bin] juliaroquette% ./spyder
2017-06-19 18:24:06.479 python[713:16813] modalSession has been exited prematurely - check for a reentrant call to endModalSession:
Traceback (most recent call last):
File "/Users/juliaroquette/anaconda/lib/python2.7/site-packages/spyder/app/mainwindow.py", line 3011, in main
mainwindow = run_spyder(app, options, args)
File "/Users/juliaroquette/anaconda/lib/python2.7/site-packages/spyder/app/mainwindow.py", line 2913, in run_spyder
main.setup()
File "/Users/juliaroquette/anaconda/lib/python2.7/site-packages/spyder/app/mainwindow.py", line 784, in setup
from spyder.plugins.help import Help
File "/Users/juliaroquette/anaconda/lib/python2.7/site-packages/spyder/plugins/help.py", line 33, in <module>
from spyder.utils.help.sphinxify import (CSS_PATH, generate_context,
File "/Users/juliaroquette/anaconda/lib/python2.7/site-packages/spyder/utils/help/sphinxify.py", line 29, in <module>
from docutils.utils import SystemMessage as SystemMessage
File "/Users/juliaroquette/anaconda/lib/python2.7/site-packages/docutils/utils/__init__.py", line 20, in <module>
import docutils.io
File "/Users/juliaroquette/anaconda/lib/python2.7/site-packages/docutils/io.py", line 18, in <module>
from docutils.utils.error_reporting import locale_encoding, ErrorString, ErrorOutput
File "/Users/juliaroquette/anaconda/lib/python2.7/site-packages/docutils/utils/error_reporting.py", line 47, in <module>
locale_encoding = locale.getlocale()[1] or locale.getdefaultlocale()[1]
File "/Users/juliaroquette/anaconda/lib/python2.7/locale.py", line 564, in getlocale
return _parse_localename(localename)
File "/Users/juliaroquette/anaconda/lib/python2.7/locale.py", line 477, in _parse_localename
raise ValueError, 'unknown locale: %s' % localename
ValueError: unknown locale: UTF-8
I tried to do what the error msg suggest, and use the option --reset, but the same error still exist after that.
Anyone has any idea on how to solve this issue?
It is known that the MacPorts version of Spyder is raising this error:
ValueError: unknown locale: UTF-8
which doesn’t let it start correctly.
To fix it you will have to set these environment variables in your .profile (or .bashrc) manually:
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8

save python plot with matplotlib fails under windows

I would like to save a python plot. With MacOS everything runs smoothly but it fails under Windows7.
The python-command looks like this:
savefig('C:\Users\xyz\AppData\Local\Temp\pyplot1468046843481608342.png')
The error messge is this one:
Traceback (most recent call last):
File "C:\Users\xyz\AppData\Local\Temp\analyze3744796441786382480.py", line 340, in <module>
savefig('C:\Users\xyz\AppData\Local\Temp\pyplot1468046843481608342.png')
File "C:\Anaconda\lib\site-packages\matplotlib\pyplot.py", line 577, in savefig
res = fig.savefig(*args, **kwargs)
File "C:\Anaconda\lib\site-packages\matplotlib\figure.py", line 1470, in savefig
self.canvas.print_figure(*args, **kwargs)
File "C:\Anaconda\lib\site-packages\matplotlib\backend_bases.py", line 2194, in print_figure
**kwargs)
File "C:\Anaconda\lib\site-packages\matplotlib\backends\backend_agg.py", line 526, in print_png
filename_or_obj = open(filename_or_obj, 'wb')
IOError: [Errno 22] invalid mode ('wb') or filename: 'C:\\Users\xyz\\AppData\\Local\\Temp\\pyplot1468046843481608342.png'
This is the Python I'm using:
Python 2.7.8 :: Anaconda 2.1.0 (32-bit)
Can anybody explain the cause of the problem?
I think you have to add r before your directory to convert your string into raw string. See this post
Unknown python expression filename=r'/path/to/file'

Categories