How to solve **TypeError : expected str, bytes or os.PathLike object, not WindowsPath** when importing matplotlib to python - matplotlib

I am trying use matplotlib within Autodesk Revit software from the PyRevit Project
In this project, Pyrevit brings an embedded CPython interpreter (3.8.5) to Revit.
Until now, I've managed to import some packages (e.g. numpy, pandas), but unfortunately import matplotlib has been a big challenge.
When I try to import matplotlib, I got the following error:
CPython Traceback:
TypeError : expected str, bytes or os.PathLike object, not WindowsPath
File "C:\Users\carlo\pyproj\bimenv\Lib\site-packages\matplotlib\__init__.py", line 886, in <module>
rcParamsDefault = _rc_params_in_file(
File "C:\Users\carlo\pyproj\bimenv\Lib\site-packages\matplotlib\__init__.py", line 789, in _rc_params_in_file
with _open_file_or_url(fname) as fd:
File "contextlib.py", line 113, in __enter__
File "C:\Users\carlo\pyproj\bimenv\Lib\site-packages\matplotlib\__init__.py", line 765, in _open_file_or_url
fname = os.path.expanduser(fname)
File "ntpath.py", line 293, in expanduser
...\matplotlib_init_.py
File ntpath.py
Doing the same, import matplotlib, from the Python 3.8.5 in the command line it works without any problem.
enter image description here
Thank you in advance for any suggestion from your side to solve this issue.

Related

Matplotlib.pyplot backend qt giving ValueError of image size

I am using Python 3.7. I would like to change my backend temporarily so that I can zoom in/out of my data. However, generating figures using qt backend gives me a ValueError, even if the figure is minimally simple.
Here is a minimum example, where I typed the following commands in the console one by one
[Input Commands]
import matplotlib.pyplot as plt
%matplotlib qt
plt.plot([1,2],[1,2])
[Output Error]
[<matplotlib.lines.Line2D at 0x1b2efda7648>]Traceback (most recent call last):
File "C:\Users\USER_NAME\anaconda3\lib\site-packages\matplotlib\backends\backend_qt.py", line 455, in _draw_idle
self.draw()
File "C:\Users\USER_NAME\anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py", line 431, in draw
self.renderer = self.get_renderer(cleared=True)
File "C:\Users\USER_NAME\anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py", line 447, in get_renderer
self.renderer = RendererAgg(w, h, self.figure.dpi)
File "C:\Users\USER_NAME\anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py", line 93, in __init__
self._renderer = _RendererAgg(int(width), int(height), dpi)
ValueError: Image size of 213120x159840 pixels is too large. It must be less than 2^16 in each direction.
I don't think this question is a duplicate of previous questions (here and here) because the error appears even for a very simple figure with no labels.
I thought I had accidentally overwritten some kind of setting file, so I tried re-installing matplotlib using
"conda install matplotlib" at the Anaconda prompt, but it didn't help. Changing back to my default backend "%matplotlib inline" generates figures normally.
How can I fix my qt backend?

Is there another library I have missed before running chatterbot?

I've tried to create a chatbot program using the chatterbot library. It produced some errors which beat all attempts to resolve. Finally, I went to the chatterbot documentation online and copy/pasted the first example chatterbot code and run it in my editor. This produced the same errors. So I want to know if there is some library I have missed perhaps, what that library is and the version of the library to install?
I've run the example code from https://chatterbot.readthedocs.io/en/stable/examples.html Simple Example (as found below). It has produced the same errors.
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
# Create a new chat bot named Charlie
chatbot = ChatBot('Charlie')
trainer = ListTrainer(chatbot)
trainer.train([
"Hi, can I help you?",
"Sure, I'd like to book a flight to Iceland.",
"Your flight has been booked."
])
# Get a response to the input text 'I would like to book a flight.'
response = chatbot.get_response('I would like to book a flight.')
print(response)
I expect to have a prompt at the terminal that allows input and produces a response. I got a list of error instead.
Traceback (most recent call last):
File "/Users/ikwame.amofa/PycharmProjects/trychat/main.py", line 3, in <module>
from chatterbot import ChatBot
File "/Users/ikwame.amofa/PycharmProjects/trychat/venv/lib/python3.7/site-packages/chatterbot/__init__.py", line 4, in <module>
from .chatterbot import ChatBot
File "/Users/ikwame.amofa/PycharmProjects/trychat/venv/lib/python3.7/site-packages/chatterbot/chatterbot.py", line 2, in <module>
from chatterbot.storage import StorageAdapter
File "/Users/ikwame.amofa/PycharmProjects/trychat/venv/lib/python3.7/site-packages/chatterbot/storage/__init__.py", line 1, in <module>
from chatterbot.storage.storage_adapter import StorageAdapter
File "/Users/ikwame.amofa/PycharmProjects/trychat/venv/lib/python3.7/site-packages/chatterbot/storage/storage_adapter.py", line 3, in <module>
from chatterbot.tagging import PosHypernymTagger
File "/Users/ikwame.amofa/PycharmProjects/trychat/venv/lib/python3.7/site-packages/chatterbot/tagging.py", line 4, in <module>
from chatterbot.tokenizers import get_sentence_tokenizer
File "/Users/ikwame.amofa/PycharmProjects/trychat/venv/lib/python3.7/site-packages/chatterbot/tokenizers.py", line 4, in <module>
from chatterbot.corpus import load_corpus, list_corpus_files
File "/Users/ikwame.amofa/PycharmProjects/trychat/venv/lib/python3.7/site-packages/chatterbot/corpus.py", line 5, in <module>
from chatterbot_corpus.corpus import DATA_DIRECTORY
ModuleNotFoundError: No module named 'chatterbot_corpus'
First of all, put your error message in a code tag,
Secondly, you forgot to install the library. Install it using:
pip install chatterbot-coprus

matplotlib savefig IO error

I am trying to use the matplotlib.pyplot.savefig() function to save some figures.
I am saving them to a directory, however I keep getting the error:
matplotlib.pyplot.savefig(savepath,dpi=dpi,size=size)
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 1476, in savefig
self.canvas.print_figure(*args, **kwargs)
File "C:\Anaconda\lib\site-packages\matplotlib\backends\backend_qt5agg.py", line 161, in print_figure
FigureCanvasAgg.print_figure(self, *args, **kwargs)
File "C:\Anaconda\lib\site-packages\matplotlib\backend_bases.py", line 2211, 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 2] No such file or directory:
Of course the file doesn't exist, as I am trying to save it now.
The directory does exist, I have checked repeatedly.
I am completely baffled, as this worked perfectly fine 2 days ago, but with no changes to the code it does not now. EDIT: I updated the version of the anaconda python distribution that I was using before, From 32 bit anaconda 2.0 to 64 bit 2.3, both for python 2.7.
Does anyone have any clue?
Thank you for reading my desperate plea for assistance!
EDIT:
I am also getting what I believe to be the same error from saving txt files in python now.
f = open(fname, 'w')
IOError: [Errno 2] No such file or directory: 'D:\\DropBox\\Dropbox\\abc\\Time resolved spectroscopy data\\LiHoF4\\High resolution 1cm\\power spectra\\Si\\RT\\25ns\\CUT POWER SPECTRUM LiHoF pumping 5G5 449.8nm DC si detector 1cm resolution 25ns data aquisition 2000 points 5AVG RT 450nmlongpassfilter.0.dpt_fitting_output.txt'
Could this be to do with the long filename?
I don't understand why there would be a problem with a file not existing when opening it to write to.

Python Matplotlib errors with savefig (newbie).

All parts of Python on my computer were recently installed from the Enthought academic package, but use Pyscripter for editing and running code. I'm very early in my learning curve, and so could very well be overlooking some obvious things here.
When I try to create a plot and save it like so:
import matplotlib.pylab as pl
pl.hist(myEst, bins=20, range=(.1,.60))
pl.ylabel("Freq")
pl.xlabel("Success Probability")
pl.title('Histogram of Binomial Estimator')
pl.axis([0, 1, 0, 500])
pl.vlines (.34,0,500)
pl.savefig('TestHist.png')
pl.show()
I get these errors:
Traceback (most recent call last):
File "<editor selection>", line 9, in <module>
File "C:\Python27\lib\site-packages\matplotlib\figure.py", line 1172, in savefig
self.canvas.print_figure(*args, **kwargs)
File "C:\Python27\lib\site-packages\matplotlib\backends\backend_wxagg.py", line 100, in print_figure
FigureCanvasAgg.print_figure(self, filename, *args, **kwargs)
File "C:\Python27\lib\site-packages\matplotlib\backend_bases.py", line 2017, in print_figure
**kwargs)
File "C:\Python27\lib\site-packages\matplotlib\backends\backend_agg.py", line 450, in print_png
filename_or_obj = file(filename_or_obj, 'wb')
IOError: [Errno 13] Permission denied: 'TestHist.png'
If I take out the pl.savefig('TestHist') line everything works fine, and I can see the plot I want, but when that line is in there I get the errors.
I've checked my backend version using pl.get_backend(), it returns 'WXAgg', which according to documentation should be able to use .png format.
I've also tried including an explicit format='png' and format=png within the savefig command, but still get errors.
Can anyone give me advice on how to proceed, or another approach for saving a plot?
There's nothing wrong with your code. I just tested it locally on my machine. The issue is this error:
IOError: [Errno 13] Permission denied: 'TestHist.png'
You are most likely trying to save the file somewhere that the Python process doesn't have permission to access. What OS are you on? Where are you trying to save the file?
If it helps others, I made the silly mistake of not actually designating a file name and as a result had returned the same error message that lead me to this question for review.
Here is the code that was generating the error:
plt.savefig('C:\\Users\\bwarn\\Canopy', format='png')
Here is my correction that resolved (you'll see I designated the actual file and name)
plt.savefig('C:\\Users\\bwarn\\Canopy\\myplot.png', format='png')
The following worked for me when I was running a neural network on my windows machine:
image_path = 'A:/DeepLearning/Padhai/MLFlow/images/%s.png' % (expt_id)
plt.savefig(image_path)
Or otherwise refer:
Using 'r' in front of the path

error importing numpy

I have strange error, when I try to import numpy:
Traceback (most recent call last):
File "/home/timo/malltul/mafet/src/mafet/core/pattern.py", line 7, in <module>
import numpy as np
File "/usr/lib/python2.6/dist-packages/numpy/__init__.py", line 147, in <module>
import ma
File "/usr/lib/python2.6/dist-packages/numpy/ma/__init__.py", line 44, in <module>
import core
File "/usr/lib/python2.6/dist-packages/numpy/ma/core.py", line 4850, in <module>
all = _frommethod('all')
File "/usr/lib/python2.6/dist-packages/numpy/ma/core.py", line 4824, in __init__
self.__doc__ = self.getdoc()
File "/usr/lib/python2.6/dist-packages/numpy/ma/core.py", line 4830, in getdoc
signature = self.__name__ + get_object_signature(meth)
File "/usr/lib/python2.6/dist-packages/numpy/ma/core.py", line 109, in get_object_signature
import inspect
File "/usr/lib/python2.6/inspect.py", line 39, in <module>
import tokenize
File "/usr/lib/python2.6/tokenize.py", line 38, in <module>
COMMENT = N_TOKENS
NameError: name 'N_TOKENS' is not defined
It seems that the cause of the problem is that my script is in my own package named core and whenever I try to import numpy there, I get the error. Importing works fine elsewhere.
The only solution I've got this far is to rename my 'core' package to something else. Why does this matter? Am I doing something wrong?
I'm using Python2.6 on Ubuntu 10.14 . Numpy version is 1.3.0 .
EDIT: Actually renaming my package does not fix it. Renaming token.py in my package fixes it. Sorry for the error.
I doubt this has anything to do with your core module or with numpy.
From the stack trace, it would appear that the problem is with the tokenize module, which is part of Python, not part of numpy. Tokenize does from token import * and then uses N_TOKENS that's defined in token.py.
First of all, I'd check that there's no stray module called token on your PYTHONPATH:
>>> import token
>>> token.__file__
'/usr/lib/python2.6/token.pyc'
If this picks up the above file yet you still get the problem, I'd suggest reinstalling Python.