Python application not loading graph - python

When I run this Python script the percentage appears in my command line but the graph does not display. I'm running this on the Raspberry Pi. All I've done so far is:
sudo apt-get install update
sudo apt-get install python-numpy
sudo apt-get install python-scipy
sudo apt-get install python-matplotlib
sudo apt-get install git
sudo git clone https://github.com/scikit-fuzzy/scikit-fuzzy.git
cd scikit-fuzzy/
sudo python setup.py install
cd ..
python tipping.py
How do I get it to open?

I have first thought about adding he following line to the code:
plt.show()
However, it can also be that Python is storing the graph information directly in the memory but not showing it. In this case you must change the "Graph visualization" preferences in Python. If you are running Python through Spyder, then you must go to Tools>Preferences>Graph visualization and tick the option related to show the graph in a different window.

This was resolved by adding:
import matplotlib
matplotlib.use('TkAgg')
before:
import matplotlib.pyplot as plt

Related

Tkinter install in docker

I have a docker file that installs all the dependencies and creates an environment for the application, but there is one particular one that is giving me a hard time.
I am using this command to install tkinter in docker container
RUN apt-get install -y python3-tk
But this one gives a prompt to select for time zones and geography.
I am currently circumventing this by getting in the docker and install the same in container with
docker run -ti tag:latest /bin/sh
which isn't very neat, is there a way around this one, Either to do one of the following
Auto select the prompt (with something like expect and send)
Install tkinter without prompt maybe defaults.
Any suggestions that are not complete answers for similar problems are appreciated as well, we can also install it in a different way if possible without apt
I had the same problem, and I used this in my Dockerfile:
ARG DEBIAN_FRONTEND=noninteractive
ENV TZ=Europe/London
RUN apt-get install -y python3-tk
This is based on this answer: How to install tzdata on a ubuntu docker image?

Always installing vscode plugin in docker container doesnt work

I am using vscode with docker container. I have following entry in user settings.json.
"remote.containers.defaultExtensions": [
"ms-python.python",
"ms-azuretools.vscode-docker",
"ryanluker.vscode-coverage-gutters"
]
But when I build or rebuild container, these plugins don't get installed automatically inside container.
Am I doing something wrong ?
Modified
Here is how my dockerfile looks like
FROM ubuntu:bionic
RUN apt-get update
RUN apt-get install -y python3.6 python3-pip
RUN apt-get install -y git libgl1-mesa-dev
# Currently not using requirements.txt to improve caching
#COPY requirements.txt /home/projects/my_project/
#WORKDIR /home/projects/my_project/
#RUN pip3 install -r requirements.txt
RUN pip3 install torch pandas PyYAML==5.1.2 autowrap Cython==0.29.14
RUN pip3 install numpy==1.17.3 open3d-python==0.7.0.0 pytest==5.2.4 pptk
RUN pip3 install scipy==1.3.1 natsort matplotlib lxml opencv-python==3.2.0.8
RUN pip3 install Pillow scikit-learn testfixtures
RUN pip3 install pip-licenses pylint pytest-cov
RUN pip3 install autopep8
COPY . /home/projects/my_project/
This might be an old question, but to whomever it might concern, here is one solution. I encountered this problem, that particularly the Python extension from VS Code would not install itself inside my Docker container in VS Code. In order to get it to install the python extension (and for me anything else) you have to specify the Python version, like:
"extensions": [
"ms-azuretools.vscode-docker",
"ms-python.python#2020.9.114305",
"ms-python.vscode-pylance"
]
If you want to see this in action you can clone my repository. Simply open this repo in VS Code, install the extension Remote Container, and then it should start the docker container all by itself.

Install and using pip and virtualenv in Docker

I want to install Python, Pip and virtualenv in a Docker container which is Ubuntu, I create the image through Dockerfile:
FROM ubuntu:16.04
RUN apt-get update -y
RUN apt-get
RUN apt-get python3 -y
RUN apt-get install python3-pip -y
RUN pip install virtualenv
...
When it went here, it came out "/bin/sh: 1: pip: not found", but there's no exception showed in the installation process, is this mean I didn't install pip right? Or should I do anything else before I use pip order?
Then I changed the Dockerfile like this:
...
RUN apt-get python3 -y
RUN apt-get install python3-pip -y
RUN apt-get install python3-virtualenv -y
RUN virtualenv --no-stie-packages -p /path/python3 py3env
...
but it still saying /bin/sh: 1: virtualenv: not found, here is it
I also installed git, git clone order ran correctly, can be used. Where am I wrong, or how should I do?
On Debian-based platforms, including Ubuntu, the command installed by python3-pip is called pip3 in order for it to peacefully coexist with any system-installed Python 2 and its pip.
Somewhat similarly, the virtualenv command is not installed by the package python3-virtualenv; to get that, you need apt-get install -y virtualenv.
Note that venv is included in the Python 3 standard library, so you don't really need to install anything at all.
python3 -m venv newenv
Why would you want a virtualenv inside Docker anyway, though? (There are situations where it makes sense but in the vast majority of cases, you want the Docker container to be as simple as possible, which means, install everything as root, and rebuild the whole container if something needs to be updated.)
As an aside, you generally want to minimize the number of RUN statements. Making many layers while debugging is perhaps defensible, but layers which do nothing are definitely just wasteful. Perhaps also discover that apt-get can install more than one package at a time.
RUN apt-get update -y && \
apt-get install -y python3 python3-pip && \
...
The && causes the entire RUN sequence to fail as soon as one of the commands fails.
What sense to use virtual environment inside container? Virtualenv very helpful for local development, it allows you to use different versions of python interpreter and packages on a single machine. But in your docker container should be only one process (actually container it is process), and you can install all your requirements globally.
But if you really have strong reasons for that, you probably must use "python3 way" to create virtual env.
So your Dockerfile should look something like that:
FROM ubuntu:16.04
RUN apt-get update -y \
&& apt install python3 -y \
&& apt install python3-pip -y \
&& apt install python3-venv -y \
&& python3 -m venv venv
ENTRYPOINT bin/bash
You can build it with command
docker build -t ubuntu-python .
And run with
docker run --rm -it ubuntu-python
In container shell you could activate venv with command
source venv/bin/activate
then run python interpreter and check that it was run from venv:
>>> import sys
>>> sys.executable
it should print /venv/bin/python
I have no idea how to run container with preactivated (I don't know if this word really exists) virtual environment and I still think you actually don't need to use virtual environment in your container.
Also you'd better try ready python-images for docker, e.g. light alpine images instead of extending basic ubuntu image.
Excuse my terrible russian-english, I hope you'll understand my answer :)

Segmentation fault (core dumped) Error in PyQt5

I just installed PyQt 5.7.0 on my google compute engine machine which running on a ubuntu 16.04:
However, when I wanted to run PyQt and import some module, it produce Segmentation fault (core dumped) error as shown:
Can I know how do I solve it? I have been searching an answer for this for hours and still can't find an answer. Will be greatly appreciated if anyone could help.
You can try (as explained in the comments) to compile PyQt5.7 yourself, using a different version of Python (3.4.3 and 3.4.4 worked for me, everything above 3.5 did not). Note that I also compiled Qt5.7 myself, but you can use the one provided by the installer. Here is a short, hopefully exhaustive, set of commands to setup a virtual environment:
Install dependencies using apt:
sudo apt-get install -y build-essential libgl1-mesa-dev libx11-dev libxext-dev libxfixes-dev libxi-dev libxrender-dev libxcb1-dev libx11-xcb-dev libxcb-glx0-dev libfontconfig1-dev libfreetype6-dev libglu1-mesa-dev libssl-dev libcups2-dev python3-pip git
Install Python 3.4.4:
cd ~/Downloads
wget https://www.python.org/ftp/python/3.4.4/Python-3.4.4.tar.xz
tar xf Python-3.4.4.tar.xz
cd Python-3.4.4
./configure
sudo make altinstall
Create the virtual environment:
sudo pip3 install virtualenv
virtualenv -p /usr/local/bin/python3.4 ~/python34
source ~/python34/bin/activate
Install Qt:
cd ~/Downloads
git clone git://code.qt.io/qt/qt5.git
cd ~/Downloads/qt5
git checkout 5.7
./init-repository
./configure -prefix ~/Qt/5.7/gcc_64 -opensource -nomake examples -nomake tests -release -confirm-license
make -j 5
make install
Install SIP:
cd ~/Downloads
wget http://downloads.sourceforge.net/project/pyqt/sip/sip-4.18.1/sip-4.18.1.tar.gz
tar xf sip-4.18.1.tar.gz
cd sip-4.18.1
python configure.py
make
sudo make install
Install PyQt:
cd ~/Downloads
wget http://downloads.sourceforge.net/project/pyqt/PyQt5/PyQt-5.7/PyQt5_gpl-5.7.tar.gz
tar xf PyQt5_gpl-5.7.tar.gz
cd PyQt5_gpl-5.7
python configure.py --qmake ~/Qt/5.7/gcc_64/bin/qmake --disable QtPositioning --no-qsci-api --no-designer-plugin --no-qml-plugin --confirm-license
make -j 5
sudo make install
Caveats:
I'm going to assume that you can't run any GUI programs right now in your vm.
I'm not that familiar with GCE platform and have not tried what I'm going to propose
Follow the steps at Your desktop on Google Cloud Platform to install a desktop GUI manager in the GCE vm (it will be X11-based for Linux OS), as well as vnc server.
Once you are vnc'd in to your vm using realvnc or tightvnc viewer, a GUI app will likely run. You may have to change the X11 DISPLAY variable -- eventhough the above link does not discuss this -- because AFAIR *nix systems use a separate desktop session for VNC than the one currently logged in.
I doubt it will run if there is no user logged on to the GCE vm.
If you installed PyQt5 with apt or apt-get, now do also
sudo pip3 install pyqt5
You cannot run a GUI program (PyQt) remotely, unless both machines use X11 protocol with properly set DISPLAY variables and xhost permissions. I doubt that Google compute engine is configurable to run in the X11 mode.
When you import PyQt5, it will also import PyQt5.QtCore because PyQt5.QtCore is part of PyQt5.

Using matplotlib on headless Ubuntu 14.04 Server

I have a headless Ubuntu 14.04 Server that I connect to remotely using SSH. I want to use matplotlib and have plots appear at the ssh client. For example, I would connect using:
ssh -X name#server.com
And then from a Python console, I want this to produce a plot in a window:
import matplotlib.pyplot as plt
plt.plot(range(10))
plt.show()
I have installed matplotlib in my virtualenv, and I ran sudo apt-get install python-gtk2, but the plot still doesn't appear. I assume I'm missing lots of packages. What is a fairly minimal set of X-related packages I could install to make this work? I do NOT want to install ubuntu-desktop.
I got it working on Ubuntu 14.04.1 Server, but it was painful! The tricky part is definitely virtualenv. I finally had luck using the Qt4 backend, which I was only able to install via the Ubuntu package and then had to symlink it into my virtualenv. So here's the step-by-step process...
First install the pre-reqs and hack PyQt4 into your virtualenv:
$ sudo apt-get install xauth x11-apps python-qt4
$ ln -s /usr/lib/python2.7/dist-packages/PyQt4 /path/to/myvenv/lib/python2.7/PyQt4
Now manually download and install SIP (http://www.riverbankcomputing.com/software/sip/intro) with your venv activated, as follows:
$ tar xzf sip-4.16.4.tar.gz
$ cd sip-4.16.4
$ python configure.py
$ make
$ sudo make install
Next, download matplotlib source tarball and modify the setup configuration to force it to install Qt4 backend:
$ tar xzf matplotlib-1.4.2.tar.gz
$ cp matplotlib-1.4.2/setup.cfg.template matplotlib-1.4.2/setup.cfg
Now edit setup.cfg near line 68 to read:
qt4agg = True
Matplotlib will now install cleanly in your venv:
$ pip install -e matplotlib-1.4.2/
Now you can SSH using the -X flag and plots will load remotely!

Categories

Resources