How do I get Pyinstaller to work with pyplot? - python

I am trying to turn a program, that uses the matplotlib.pyplot module, into an executable.
The script itself works as intended when run in the terminal, but when I use the executable made by pyinstaller, it shows - "Script failed to execute".
I have narrowed down the issue to the following code:
import matplotlib.pyplot as plt
The .exe will run fine with this code commented out, but that way I am not able to plot graphs using pyplot.
EDIT: I did some research on this and found out it's a known issue with Matplotlib 3.3, so I was able to get around it by uninstalling the current version and using:
pip install matplotlib==3.1.3

Related

How I do properly import serial data from arduino using pycharm

I am newbie in python.
After I installed python 3.8 latest version, libraries ...etc
Pycharm IDE, and for every time you create project you have to install packages so your code look nice with no red line under.
after I spent hours trying to find whats going on, Error, visual studio ++14 and not 15 or 19.
The code line, import serial doesn't seem recognized by pycharm or by python.
I have read many people having the same problem but few were successful to solve, other are still not.
My Problem is that I need to plot data from arduino using python over serial.
Anyone have an idea how to solve the import serial not working?
The code implemented:
import serial , error
import numpy as np package not found but managed to install it and have to install it again
import matplotlib.pyplot as plt
%matplotlib qt
ser = serial.Serial('COM4',9600) Syntax error
Just tried to install matplotlib package, I got this error
ModuleNotFoundError: No module named 'Cython'
An hour ago did work just fine downloading the package.
Any recommendation for another python IDE than pycharm ?
U can use anaconda navigator. It comes with many inbuilt libraries such as pandas numpy matlab etc according to me it is best to use anaconda navigator in which u can either use spyder ide or jyputer notebook do upvode if u found helpful

New To Visual Studio Code, having problems with pandas/numpy

I normally use PyCharm for python coding, but just for the heck of it, I tried to use Visual Studio Code today, and I'm having some problems.
So I followed the steps shown in the "Getting Started with Python in VS Code" page, and I copied this to my new python project:
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
x = np.linspace(0, 20, 100) # Create a list of evenly-spaced numbers over the range
plt.plot(x, np.sin(x)) # Plot the sine of each x point
plt.show() # Display the plot
just to test if it works well, and for some random reason, whenever I run this code through terminal/cmd in VSCode, I get this:
ImportError: Missing required dependencies ['numpy']
BUT when I use the Debug Mode, it seems to work perfectly fine.
similar thing happened when I tried to run my previous projects through VSCode. So I thought maybe it was just the problem with my environment, so I changed it to the one where I have my tools installed, but nope, I still got the error.
I tried uninstalling then installing again and that didn't work as well.
I Seriously don't know what's happening right now. Why does it work well in Debug Mode but not in terminal/cmd? does anyone know what to do in this situation?
Thanks!
...In Python, packages are how you obtain any number of useful code libraries, typically from PyPI. For this example you use the matplotlib and numpy packages to create a graphical plot as is commonly done with data science. (Note that matplotlib cannot show graphs when running in the Windows Subsystem for Linux as it lacks the necessary UI support.)
Return to the Explorer view (the top-most icon on the left side, which shows files), create a new file called standardplot.py, and paste in the following source code:
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
x = np.linspace(0, 20, 100) # Create a list of evenly-spaced numbers over the range
plt.plot(x, np.sin(x)) # Plot the sine of each x point
plt.show() # Display the plot
Tip: if you enter the above code by hand, you may find that auto-completions change the names after the as keywords when you press Enter at the end of a line. To avoid this, type a space, then Enter.
Next, try running the file in the debugger using the "Python: Current file" configuration as described in the last section. (If you still have "stopOnEntry": true in that configuration, you need to select the run command again to continue.)
Unless you're using an Anaconda distribution or have previously installed the matplotlib package, you should see the message, "ModuleNotFoundError: No module named 'matplotlib'". Such a message indicates that the required package isn't available in your system.
To install the matplotlib package (which also installs numpy as a dependency), stop the debugger and run Terminal: Create New Integrated Terminal from the Command Palette (⌃⇧(Windows, Linux Ctrl+Shift+))). This command opens a command prompt for your selected interpreter. Then enter the following commands as appropriate for your operating system (commands may require elevation if the Python interpreter is installed in a protected area of the file system):
Note: If you are unable to install the package or encounter other problems, please file an issue on GitHub so we can help you investigate.
# Don't use with Anaconda distributions because they include matplotlib already.
# macOS
sudo python3 -m pip install matplotlib
# Windows (may require elevation)
py -3 -m pip install matplotlib
# Linux (Debian)
sudo apt-get install python3-tk
python -m pip install matplotlib
from: https://code.visualstudio.com/docs/python/python-tutorial
Got this error and was able to fix it by running conda init in the Python Debug Console terminal, and then closing the terminal before starting a new debug session.
In VS Code undee Windows open Terminal -> New Terminal and run
pip3 install pandas
and then
pip3 install matplotlib
While installing pandas, numpy will be installed as well
I believe it may have something to do with the PATH variables. I just had the same problem in Windows 10 and found out that if I launch VS Code through an anaconda prompt it works just fine, no need to mess around with the PATH variables.
So, instead of opening VS Code through the start menu, just open an anaconda prompt (or anaconda shell), cd to your directory and type code .. That solved my issue.

Import package works in jupyter notebook but not in iPython and Anaconda cmd

When searching about this issue, I came across some questions asking the opposite, i.e., package opens in iPython but not in Jupyter Notebook. But in my case, its the opposite. That's why I posted this question.
I added path\to\anaconda3 and path\to\anaconda3\Lib\site-packages in the environment variable, but it doesn't solve the issue.
I can see the packages in the site-packages folder:
But I just can't import some of the packages in iPython:
or with python in the anaconda cmd:
But it works fine in Jupyter Notebook:
What do/can I do to fix this?
Here's some more info if it helps:
(base) C:\Users\h473>where python
C:\Users\h473\AppData\Local\Continuum\anaconda3\python.exe
(base) C:\Users\h473>where conda
C:\Users\h473\AppData\Local\Continuum\anaconda3\Library\bin\conda.bat
C:\Users\h473\AppData\Local\Continuum\anaconda3\Scripts\conda.exe
(base) C:\Users\h473>where pip
C:\Users\h473\AppData\Local\Continuum\anaconda3\Scripts\pip.exe
P.S.: It doesn't happen for all packages, only some packages, as shown for pandas, numpy and matplotlib in the screenshot below.
When you are using matplotlib (and seaborn is built on top of it) it needs to use a so called backend that is used to display the actual GUI with the plot in it once you execute for example matplotlib.pyplot.show().
When you are running a Jupyter Notebook with matplotlib in inline mode (default I think, but not sure), then a Jupyter specific backend is used (module://ipykernel.pylab.backend_inline). That makes sense, since the plots should not appear in separate windows, but be displayed inside the notebook itself.
When you are in an interactive python or iPython session however, Qt5 was used as
import matplotlib
print(matplotlib.rcParams["backend"]) # this prints the backend that would be loaded when trying anything with pyplot
has revealed. Since you get the error youa re getting, it looks like your QT5 installation is broken. You can try to reinstall them using the conda commands, but for now you could also fall back to using a different backend, that you need to specify before trying to load seaborn:
import matplotlib
matplotlib.use("TkAgg") #use backend TkAgg
import seaborn
You can also change the default backend being loaded to TkAgg by creating a matplotlibrc file in C:\Users\<your name>\.matplotlib\ with
backend : TkAgg
in it.

ipython import modules fine but python import fails

there.
I'm not quite sure what I have done recently that cause the problem as mentioned in the title. I usually run the simple code in ipython, and run longer scripts use python command. However, just now it seems that python somescript.py doesn't work well if some modules are imported in the somescript.py file. For example, say it contains import matplotlib.pyplot as plt, when I run python somescript.py in the terminal, I will get an error message:
ImportError: no module named matplotlib. This is weird since when I run import matplotlib.pyplot as plt under ipython, it works fine, no error.
And I checked that the python version and path is different from ipython. ipython has several more paths that python, and the version is 2.7.11, while python is 2.7.10. So, what should I do to permanently resolve this issue? Add the other paths to python? How can I make sure that they import the same modules each time? Thanks a lot.

Trouble in installing Basemap in matplotlib

I am trying to install Basemap, and beforehand I already have the prerequisite versions of Matplotlib, Python and Numpy working on my Mac. To install Basemap I am following the instructions in this website http://matplotlib.org/basemap/users/installing.html
I seemed to progress quite well with the steps, until the very end which is "Check your installation by running from mpl_toolkits.basemap import Basemap at the python prompt." I got "ImportError: cannot import name pyproj" from a line that reads "---> 30 from mpl_toolkits.basemap import pyproj".
Does anyone have any idea? Many thanks!
The compiled module could be accessible by python. To achieve that you should put the module into python path or you need to add the location of the module to PYTHONPATH environment variable.
To see the python installations paths you can write these lines to python shell:
import sys
for path in sys.path:
print(path)
The code will show the paths python is looking for modules.
After you compile the c library, you need to go to upper folder and run python setup.py install as it's said in the installation page. This will put python modules into one of your python paths.
I had the same problem installing basemap-1.0.7.
I found that the file
/usr/lib64/python2.6/site-packages/mpl_toolkits/basemap/init.py
had a reference to axes-grid1, but python lists only module axes_grid.
So I changed grid1 to grid and now basemap imports without error.
Had the same issue on OSX, found after much faffing that Basemap worked fine with a non-native version of python (for me 2.7.12 with everything under /opt/local/Library/Frameworks...) but not with the native version (for me 2.7.10 with most stuff under /System/Library/Frameworks... and Basemap under /Library/...). I did note also that under the native version there was already some mpl_tooklit content and without the permission to add Basemap there you end up with mpl_toolkit contents in multiple places. I wasn't sure if this was the problem specifically but as I said having it all under opt using non-native python was what solved this for me.

Categories