3

I'm a newbie to Ubuntu, so I'm sorry if question is too dumb. How can I install Python package to already installed local version of Python?

Ubuntu 14.04, Python 2.7.10 /usr/local/bin/python2.7, package I need to install is zlib

Thanks in advance

SOLVED

  1. $ sudo apt-get install zlib1g-dev
  2. $ wget python.org/ftp/python/2.7.10/Python-2.7.10.tgz
  3. $ tar xfz Python-2.7.10.tgz
  4. $ cd Python-2.7.10/
  5. $ ./configure --prefix /path/to/python/ --enable-ipv6
    (in my case path was /usr/local )
  6. $ make
  7. $ sudo make install

Check:
$ python2.7 -c "import zlib; print(zlib.version)"

Grand thanks to all of you guys for helping with this problem!

olexa
  • 55
  • 1
  • 1
  • 5
  • Are you trying to run your own compile of Python, or just add the zlib library to Python for other packages and such to use it? – Thomas Ward Mar 03 '16 at 14:19
  • @Thomas W., just add the zlib library to locally installed version of Python – olexa Mar 03 '16 at 14:23

6 Answers6

4

As far as I know, there is no Python package that contains zlib because that's already included into the standard library.

Try the command below to see whether the zlib Python package is available and which version it has:

  • for Python 2.x:

    python -c "import zlib; print(zlib.__version__)"
    
  • for Python 3.x:

    python3 -c "import zlib; print(zlib.__version__)"
    

On my system, it outputs 1.0 for both Python versions.

Byte Commander
  • 105,631
  • 46
  • 284
  • 425
  • 1
    I get also 1.0 for both system versions of Python (2.7.6 and 3.4.3), but I need zlib for locally installed 2.7.10, /usr/local/bin/python2.7 – olexa Mar 03 '16 at 14:13
4

None of the existing answers is incorrect, but similarly don't explain why you're having the problem you are, or how to fix it. Let's clear up some things:

  • zlib is a builtin, not a packaged thing. Virtualenvs are great things but won't help here.
  • If you don't have it, it wasn't built when Python was built.
  • You need the zlib development libraries in order for Python to be linked to it. If the ./configure step can't find it, it'll disable it from your build.

So that having been said, sudo apt-get build-dep python2.7 will be the sanest, quickest way to get all the build dependencies for a "typical" Python build.

But then you need to reconfigure, recompile and reinstall your version of Python. Just installing the build requirements won't retroactively link it in.

Oli
  • 289,791
  • 117
  • 680
  • 835
2
  1. $ sudo apt-get install zlib1g-dev
  2. $ wget python.org/ftp/python/2.7.10/Python-2.7.10.tgz
  3. $ tar xfz Python-2.7.10.tgz
  4. $ cd Python-2.7.9/
  5. $ ./configure --prefix /path/to/python/ --enable-ipv6
    (in my case path was /usr/local )
  6. $ make
  7. $ sudo make install

check: $ python2.7 -c "import zlib; print(zlib.version)"

Grand thanks to all of you guys for helping with this problem!

olexa
  • 55
  • 1
  • 1
  • 5
0

I highly recommend using virtualenv for a local install of Python that has the same minor version as the one that comes with Ubuntu (2.7.x), I'm quite new myself and had a lot of issues attempting to install packages to a local version without it, whereas with it you can simply create and activate a new environment and install to your hearts content without changing the global package list.

User guide: http://virtualenv.readthedocs.org/en/latest/userguide.html

There's an answer here for taking an already installed python version and creating a virtualenv with that version. Once you activate the environment you should then be able to check if you have zlib with that version by default, and install it if you don't have it already. https://stackoverflow.com/questions/1534210/use-different-python-version-with-virtualenv

Cral
  • 11
  • 1
  • 4
  • Well, the matter is I can't create virtualenv for my locally installed Python 2.7.10 (system's one is 2.7.6): mkvirtualenv --python=/usr/local/bin/python2.7 familia Running virtualenv with interpreter /usr/local/bin/python2.7 Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/virtualenv.py", line 17, in import zlib ImportError: No module named zlib – olexa Mar 03 '16 at 14:14
  • Yeah I wasn't aware zlib was built into Python, so assumed your initial statement about needing to install it as a package was correct. – Cral Mar 03 '16 at 14:55
0

You need to install the zlib1g-dev package and then run: sudo apt-get build-dep python2.7

After installing the dev package, continue to install the zlib package.

If the above thing does not work:

  1. download the source package for your Python (in this case wget python.org/ftp/python/2.7.10/Python-2.7.10.tgz)
  2. run ./configure --prefix=/path/to/python
  3. make
  4. make install
olexa
  • 55
  • 1
  • 1
  • 5
Jay T.
  • 264
  • 1
  • 5
  • Thank you, @Jay T.! To install zlib I need to run sudo apt-get install zlib1g-dev python2.7 ? – olexa Mar 03 '16 at 14:28
  • No you need to run; `sudo apt-get isntall zlib1g-dev` – Jay T. Mar 03 '16 at 14:30
  • Did not wok for me.. python2.7 -c "import zlib; print(zlib.__version__)" Traceback (most recent call last): File "", line 1, in ImportError: No module named zlib – olexa Mar 03 '16 at 14:35
  • do you get any error during installation? Another way to install is just download the package and install it manually, I can help you with that as well if you want. – Jay T. Mar 03 '16 at 14:38
  • no, everything goes fine – olexa Mar 03 '16 at 14:38
  • Just to extend from this answer, @olexa, you need the zlib libraries installed *before* you compile and install Python. It seems likely that if you did not, Python would automagically disable zlib from your build. – Oli Mar 03 '16 at 14:38
  • @Oli, so there is no way to add zlib to my local Python without reinstalling it? – olexa Mar 03 '16 at 14:41
  • No, see my answer. – Oli Mar 03 '16 at 14:44
  • @olexa, I have made some changes to my answer, try that out and let me know – Jay T. Mar 03 '16 at 14:47
  • Recompiling `zlib1g-dev` does nothing to help the situation. It's Python that has to be built against zlib, not the other way around. – Oli Mar 03 '16 at 14:53
  • @Oli, I agree with you, but if you install the `zlib` package at the source of python and then built python again that should solve your problem. And I know you address this in your answer that's the reason I upvoted your answer – Jay T. Mar 03 '16 at 15:01
  • Just the headers (from -dev) should be enough to link against. – Oli Mar 03 '16 at 15:55
  • I believe you, currently I have no way to test this out for myself. – Jay T. Mar 03 '16 at 16:42
  • But I suppose you should make it more clear like: 1. download the source package for your Python (in this case wget https://www.python.org/ftp/python/2.7.10/Python-2.7.10.tgz) – olexa Mar 03 '16 at 17:10
0

This solution I found on python.org compilation page.

sudo apt-get build-dep python3.6

If that package is not available for your system, try reducing the minor version until you find a package that is available in your system’s package manager.

I added the detail instruction on a blog post.

Obscure
  • 123
  • 8