Build dependencies with apt-get before installing packages via pip? - python

Take this as an example:
I'm trying to get matplotlib properly installed inside a virtualenv located in my home directory. Should I do this:
sudo apt-get build-dep python-matplotlib
Prior to activating my virtualenv and doing this?
pip install matplotlib
I read somewhere that doing build-dep might be needed to install some headers used by pip to compile packages successfully. And, if this is true for matplotlib, is it for every other package, such as ipython or numpy?
Important: I'm using a separate version of Python (Python 3.3, compiled from source) for the virtualenv, not the system version, which is Python 2.7 in my Ubuntu 12.04 system.

That depends on what flags you've used to compile your Python version and what you're trying to get out of matplotlib. It is unlikely that you've done something so very different from the default that it would break when using the standard matplotlib. So, you should go ahead and just
pip install matplotlib
without bothering with
sudo apt-get build-dep python-matplotlib
If for some reason you need to compile matplotlib for your system (e.g. different architecture), then go ahead and use build-dep.

I have found the answer by SevakPrime to be incorrect, after experiences with installing numpy and matplotlib on three computers.
Enrique's plan works and not doing sudo apt-get build-dep python-matplotlib doesn't work. The same is true for numpy, for which the string for build-dep is python-numpy.
The following may appear: E: You must put some 'source' URIs in your sources.list. The cure for that was provided by Gunnar Hjalmarsson here. You will find Software & Updates on the System Settings panel.

Related

Can't fix "zipimport.ZipImportError: can't decompress data; zlib not available" when I type in "python3.6 get-pip.py"

I was trying to install Django. Turns out that course's teacher said that we will be working with Python 3.6
I install Python 3.6. Now it's my default, it somewhat replaced the last version I had; which is Python 3.5.
Everything ok until that. But when I want to install Django doing
"pip3 install django", it tells me that the module is already satisfied and therefore installed.
I run "python3" command into my terminal. It runs Python 3.6. I try to import Django, and boom... "No module named 'django'".
Then I realized pip3 was actually installing my modules into Python 3.5 and not 3.6. So what I do is to install pip in Python 3.6.
I download get-pip.py and proceed to execute it with Python 3.6 typing in "python3.6 get-pip.py".
Here is when the damn "zipimport.ZipImportError: can't decompress data; zlib not available" goes in. I've tried a ton of things and no one of them fixed the %^$! problem. I'm really tired.
What I have already tried:
python3.6 -m pip install django, which output is "/usr/local/bin/python3.6: No module named pip"
apt install zlib, which output is "E: Unable to locate package zlib"
apt install zlib1g-dev, which says that it's already installed; the problem persists though.
I also came across this problem (while creating a simple installer for pyenv). Here's how I solved it for Mac and Linux:
Ubuntu 20.04, 18.04
You need the zlib development files, and probably zlib itself too:
sudo apt install -y zlib1g-dev zlibc
If you're missing zlib, it's likely that the next problem you'll run into is with openssl, so it's probably best to get that now as well:
sudo apt install -y libssl-dev
sudo apt install -y libssl1.1 || sudo apt install -y libssl1.0
macOS
I believe this comes with XCode CLI Tools (or at least I didn't have to custom install it Big Sur):
xcode-select --install
For me it worked in RHEL:
$ yum install zlib-devel
Suggested solutions (installing zlib1g-dev or zlib-devel) seem to resolve the issue in most cases. Here is one edge case I've encountered recently: whatever you are trying to run might use zlib via symlink which might be broken.
In my case I was trying to run a build of a 3rd-party software which already had python and all necessary libs being emebedded into it. It was packaged as a tar.gz archive. Unpacking the archive on a Windows machine and then copying the contents to another linux machine destroyed all the symlinks (if you do ls -l in a folder with symlinks you would see that all of them have size 0 and do not point to anything). Copying tar.gz to the linux machine directly and unpacking it there resolved the issue.
P.S. I know it's an edge case scenario but it took me and one more developer quite a while to figure it out so I think it's worth mentioning here, just in case someone gets as unlucky as I got.
Its solves my issue for centos 7.6 :-
yum install zlib-deve

apt-get installing older version of packages (Ubuntu)

I'm trying to install pip and virtualenv on a server (running Ubuntu 12.04.4 LTS) on which I have access, but I can only do it with sudo apt-get install (school politics). The problem is that althought I have run the sudo apt-get update command to update the packages list, I think it keeps installing old ones. After doing sudo apt-get install python-pip python-virtualenv, I do pip --version on which I get the 1.0, and virtualenv --version on which I get 1.7.1.2. These two version are quite old (pip is already in 1.5.5 and virtualenv in 1.11.5). I read that the problem is that the packages list is not up-to-date, but the command sudo apt-get update should solve this, but I guess no. How can I solve this? Thanks a lot!
apt-get update updates packages from Ubuntu package catalog, which has nothing to do with mainstream versions.
LTS in Ubuntu stands for Long Term Support. Which means that after a certain period in time they will only release security-related bugfixes to the packages. In general, major version of packages will not change inside of a major Ubuntu release, to make sure backwards-compatibility is kept.
So if then only thing you can do is apt-get update, you have 2 options:
find a PPA that provides fresher versions of packages that you need, add it and repeat the update/install exercise
find those packages elsewhere, download them in .deb format and install.
If you really need to use the latest stable versions of Python packages, then do not use apt-get for installing Python packages and use pip instead. If you would use apt-get and later install the same packages by means of pip or (better not) easy_install or setup.py, you are likely to run into version conflicts wondering, why your python based commands are of unexpected versions, or even worse, why they do not work at all.
I try to follow this pattern:
1. system wide pip installation first
Using instructions from here: http://pip.readthedocs.org/en/latest/installing.html find get-pip.py script, download it and run as python script.
$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo python get-pip.py
$ rm get-pip.py
2. use pip to install virtualenv system wide
this shall be as easy as:
$ sudo pip install virtualenv
3. (optional) install virtualenvwrapper - system wide or to user profile
$ sudo pip install virtualenvwrapper
and follow instructions for configuring it.
4. Since now, install inside your virtualenv environments
This shall prevent conflicts between various versions of packages.
You are free to update particular virtualenvs as you need one by one independently.
5. (optional) Configure installation cache directories for installation speed
There are method how to speed up repeated installation of packages, what comes handy if you get used using virtualenv often. For details see my answer: https://stackoverflow.com/a/18520729/346478

'bz2 is module not available' when installing Pandas with pip in python virtual environment

I am going through this post Numpy, Scipy, and Pandas - Oh My!, installing some python packages, but got stuck at the line for installing Pandas:
pip install -e git+https://github.com/pydata/pandas#egg=pandas
I changed 'wesm' to 'pydata' for the latest version, and the only other difference to the post is that I'm using pythonbrew.
I found this post, related to the error, but where is the Makefile for bz2 mentioned in the answer? Is there another way to resolve this problem?
Any help would be much appreciated. Thanks.
You need to build python with BZIP2 support.
Install the following package before building python:
Red Hat/Fedora/CentOS: yum install bzip2-devel
Debian/Ubuntu: sudo apt-get install libbz2-dev
Extract python tarball. Then
configure;
make;
make install
Install pip using the new python.
Alternative:
Install a binary python distribution using yum or apt, that was build with BZIP2 support.
See also: ImportError: No module named bz2 for Python 2.7.2
I spent a lot of time on the internet and got a partial answer everywhere. Here is what you need to do to make it work. Follow every step.
sudo apt-get install libbz2-dev Thanks to Freek Wiekmeijer for this.
Now you also need to build python with bz2. Previously installed python won't work. For that do following:
Download stable python version from https://www.python.org/downloads/source/ then extract that Gzipped source tarball file. You can use wget https://python-tar-file-link.tgz to download and tar -xvzf python-tar-file.tgz to extract it in current directory
Go inside extracted folder then run following commands one at a time
./configure
make
make install
This will build a python file with bz2 that you previously installed
Since this python doesn't have pip installed, idea was to create a virtual environment with above-built python then install pandas using previously installed pip
You will see python file in the same directory. Just create a virtual environment.
./python -m env myenv (create myenv in the same directory or outside it's your choice)
source myenv/bin/activate (activate virtual environment)
pip install pandas (install pandas in the current environment)
That's it. Now with this environment, you should be able to use pandas without error.
pyenv
I noticed that installing Python using source takes a long time (I am doing it on i7 :/ ); especially the make and make test...
A simpler and shorter solution was to install another version of Python (I did Python 3.7.8) using pyenv, install it using these steps.
It not only saved the problem of using multiple Python instances on the same system but also maintain my virtual environments without virtualenvwrapper (which turned buggy on my newly setup ubuntu-20.04).

Matplotlib doesn't display graph in virtualenv

I have pip installed matplotlib in my virtualenv and I am trying to plot a simple graph. I use Eclipse and PyDev. When I run the script from Eclipse it doesn't display any graph at all. I have tried the suggestions proposed in other questions such as adding plt.ion() but that doesn't work either. I have also tried the same code in the console and again nothing. Is this a problem with my configuration? If so how can I fix it?
The failing code is:
import matplotlib.pyplot as plt
radius = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
area = [3.14159, 12.56636, 28.27431, 50.26544, 78.53975, 113.09724]
plt.plot(radius, area)
plt.show()
The code is not the problem. It works just fine out of virtualenv. The problem is when I use it in virtualenv. The problem is somehow related to my configuration. And just to clear things up I DON'T GET ANY error messages. It's just that the window wouldn't show up.
Thanks
Your code works inside my virtualenv on OSX 10.7 with Python 2.7:
What version of Python are you using inside your virtualenv? My guess is that either you have not installed a matplotlib dependency or your installation of an installed dependency was not properly performed. On Python 2.7 here is what I did to install matplotlib. Can you try these steps in a new virtualenv and see if it works for you?
pip install numpy
pip install scipy
easy_install matplotlib
I had the same issue, and installing matplotlib using easy_install instead of pip did not solve it. In the end, I found out that the problem was simply that matplotlib could not find any backend for plotting.
I solved it by doing the following (I am using Debian wheezy):
pip uninstall matplotlib
sudo apt-get install tcl-dev tk-dev
pip install matplotlib
First off, you might want to check out:
http://matplotlib.org/faq/installing_faq.html#matplotlib-compiled-fine-but-nothing-shows-up-when-i-use-it
To see what's going wrong, check out the matplotlib using git instead of either pip or easy_install. We're going to do a more manual install:
git clone git#github.com:matplotlib/matplotlib.git
cd matplotlib
python setup.py
This will print out the configuration of what pip or easy_install would have done. Look through the "OPTIONAL BACKEND DEPENDENCIES" and make sure that some of the ones that produce windows are enabled (Tkinter, Gtk+, Mac OS X native, Qt, Cairo, etc.). If you see that none of these are available, then you need to install some of these libraries for your operating system before you continue installing matplotlib.
After installing say, Tk (on ubuntu: sudo apt-get install tcl-dev tk-dev), then when you re-run
python setup.py
you will see that the Tk backend is enabled. Proceeding with
python setup.py build && python setup.py install
should get you up and running... but at that point you might even just delete the whole git clone directory and go back to installing with pip.
If you have a version of matplotlib installed on your system version of python, then you can link to the system version of matplotlib. On my machine I did the following:
cd $VIRTUAL_ENV/lib/python2.7/site-packages
ln -s /usr/lib/pymodules/python2.7/matplotlib .
ln -s /usr/lib/pymodules/python2.7/matplotlib-1.1.1rc .
This avoids many of the problems with getting matplotlib to work in the virtualenv but limits you to using the system version of matplotlib (which on this machine is not too bad).
This method also allows you to use the --no-site-packages, but still have matplotlib work for you.
I am using Ubuntu 12.04 and Python 2.7.3 on my computer and
when I use the matplotlib 1.2.0 in my virtualenv, the show() did not work until I upgrade it to the 1.2.1... All the bugs I had previous went away...
By doing this you will install all the dependencies of the matplotlib:
sudo apt-get build-dep python-matplotlib
To get the latest version of the matplotlib you can use:
pip install matplotlib
or upgrade it:
pip install matplotlib --upgrade

Ubuntu:What is the right way to reinstall from source after package was installed via apt?

I have a python package previously installed via apt(by default).
Now I want to install new version and compile it manually from the sources with all the required modules.
How can I do that?
I suppose that
apt-get purge python
And then install from sources is not possible because python have lots of dependencies and will uninstall all of them in this case.
What is the right way to do that?
Don't replace your system's default Python interpreter. It can break things which will be difficult to fix later. Instead you probably want to use virtualenv. You can then isolate any issues from your environment and have the added advantage of multiple Python installations which can tested independently.
Here is some worthwhile reading to get you started:
http://www.lorenzogil.com/blog/2010/10/29/python-deployment-tips/
http://mitchfournier.com/2010/06/25/getting-started-with-virtualenv-isolated-python-environments/
http://www.clemesha.org/blog/modern-python-hacker-tools-virtualenv-fabric-pip
sudo apt-get remove name-of-package
then
sudo apt-get autoclean
will remove the package and any dependencies that are no longer needed.

Categories